Test Overview
This test sends a GET request to a sample API endpoint and verifies the response status code and content. It checks that the API returns the expected data, ensuring the API works correctly.
This test sends a GET request to a sample API endpoint and verifies the response status code and content. It checks that the API returns the expected data, ensuring the API works correctly.
import requests import unittest class TestSampleAPI(unittest.TestCase): def test_get_user(self): url = "https://jsonplaceholder.typicode.com/users/1" response = requests.get(url) self.assertEqual(response.status_code, 200) data = response.json() self.assertEqual(data["id"], 1) self.assertIn("username", data) if __name__ == "__main__": unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner initializes the test class and method | - | PASS |
| 2 | Sends GET request to https://jsonplaceholder.typicode.com/users/1 | HTTP request sent to API server | - | PASS |
| 3 | Receives HTTP response from API | Response object contains status code and JSON data | Check response.status_code == 200 | PASS |
| 4 | Parses JSON response body | JSON data loaded into a dictionary | Check data["id"] == 1 | PASS |
| 5 | Verifies 'username' key exists in response data | Response JSON contains expected fields | Check 'username' in data | PASS |
| 6 | Test ends successfully | All assertions passed, test completes | - | PASS |