Introduction
Testing an API manually can be slow and error-prone, especially when changes happen often. Automating API tests helps catch problems quickly and keeps software reliable without repeating boring tasks.
Imagine a restaurant kitchen where chefs (software parts) pass dishes (data) through a window (API). Testing the window means checking if orders come through correctly and dishes are passed without mistakes. Automating this check is like having a smart camera that watches the window and alerts if something goes wrong.
┌─────────────────────────────┐ │ API Test Automation │ ├─────────────┬───────────────┤ │ Test Types │ Tools │ │ ┌─────────┐ │ ┌───────────┐ │ │ │Functional│ │ │ Postman │ │ │ │Negative │ │ │ REST Assured││ │ │Performance││ │ SoapUI │ │ │ │Security │ │ └───────────┘ │ │ └─────────┘ │ │ ├─────────────┴───────────────┤ │ Best Practices │ │ - Clear cases │ │ - Isolated tests │ │ - Realistic data │ │ - Continuous integration │ └─────────────────────────────┘
import requests def test_get_user(): url = 'https://jsonplaceholder.typicode.com/users/1' response = requests.get(url) assert response.status_code == 200 data = response.json() assert data['id'] == 1 assert 'username' in data if __name__ == '__main__': test_get_user() print('API test passed')
status_code.response.status_code == 200 uses the correct attribute and syntax.import requests
response = requests.get('https://api.example.com/data')
print(response.json()['status']){"status": "success", "data": []}?['status'] returns the string "success" without quotes in print output.response = requests.post('https://api.example.com/login', data={'user': 'admin', 'pass': '1234'})
assert response.status_code = 200response.status_code to 200.Pass the test and this pattern moves unknown