0
0
Testing Fundamentalstesting~15 mins

API test tools overview in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify GET request to retrieve user data using Postman
Preconditions (2)
Step 1: Open Postman application
Step 2: Create a new GET request
Step 3: Enter the URL https://jsonplaceholder.typicode.com/users/1
Step 4: Click the Send button
Step 5: Observe the response status code and body
✅ Expected Result: Response status code is 200 OK and response body contains user data with id 1
Automation Requirements - Python requests with unittest
Assertions Needed:
Verify response status code is 200
Verify response JSON contains 'id' field with value 1
Best Practices:
Use requests library for HTTP calls
Use unittest framework for assertions
Handle exceptions for network errors
Keep test code simple and readable
Automated Solution
Testing Fundamentals
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.

Common Mistakes - 3 Pitfalls
Not checking the response status code before parsing JSON
Hardcoding API URLs without using variables
Not handling exceptions for network failures
Bonus Challenge

Now add data-driven testing with 3 different user IDs (1, 2, 3) to verify their data

Show Hint