import unittest
import requests
class TestGetUserAPI(unittest.TestCase):
def test_get_user(self):
url = "https://jsonplaceholder.typicode.com/users/1"
try:
response = requests.get(url, timeout=5)
self.assertEqual(response.status_code, 200, "Status code is not 200")
data = response.json()
self.assertEqual(data.get('id'), 1, "User ID is not 1")
except requests.RequestException as e:
self.fail(f"Request failed: {e}")
if __name__ == '__main__':
unittest.main()This test uses Python's unittest framework and requests library to automate the manual test case.
We send a GET request to the API endpoint and check the status code is 200, which means success.
Then we parse the JSON response and verify the id field equals 1, matching the expected user.
We use a try-except block to catch any network errors and fail the test with a clear message.
This keeps the test simple, readable, and reliable.