0
0
Testing Fundamentalstesting~10 mins

API test automation concepts in Testing Fundamentals - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - unittest
Testing Fundamentals
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()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsTest runner initializes the test class and method-PASS
2Sends GET request to https://jsonplaceholder.typicode.com/users/1HTTP request sent to API server-PASS
3Receives HTTP response from APIResponse object contains status code and JSON dataCheck response.status_code == 200PASS
4Parses JSON response bodyJSON data loaded into a dictionaryCheck data["id"] == 1PASS
5Verifies 'username' key exists in response dataResponse JSON contains expected fieldsCheck 'username' in dataPASS
6Test ends successfullyAll assertions passed, test completes-PASS
Failure Scenario
Failing Condition: API returns a status code other than 200 or missing expected data fields
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify first after sending the GET request?
AThe response time is less than 1 second
BThe HTTP status code is 200
CThe response contains a 'username' field
DThe API endpoint URL is correct
Key Result
Always verify both the HTTP status code and the response content to ensure the API behaves as expected.