0
0
Testing Fundamentalstesting~10 mins

Request and response validation in Testing Fundamentals - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test sends a request to a web API and checks if the response status and data are correct. It verifies that the server returns the expected success code and the right message in the response body.

Test Code - unittest
Testing Fundamentals
import requests
import unittest

class TestAPIRequestResponse(unittest.TestCase):
    def test_get_user(self):
        url = "https://api.example.com/user/123"
        response = requests.get(url)
        self.assertEqual(response.status_code, 200)
        data = response.json()
        self.assertIn('username', data)
        self.assertEqual(data['username'], 'john_doe')

if __name__ == '__main__':
    unittest.main()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsTest runner initialized, ready to execute test_get_user-PASS
2Sends GET request to https://api.example.com/user/123HTTP request sent, waiting for response-PASS
3Receives HTTP response with status code 200Response received with JSON body {"username": "john_doe", "id": 123}Check response.status_code == 200PASS
4Parses JSON response bodyParsed data contains keys: username, idCheck 'username' key exists in response JSONPASS
5Verifies username value is 'john_doe'username value is 'john_doe'Check data['username'] == 'john_doe'PASS
6Test ends successfullyAll assertions passed, test case completed-PASS
Failure Scenario
Failing Condition: Response status code is not 200 or username key missing or username value incorrect
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test check first after sending the request?
AThe HTTP status code is 200
BThe username value is 'john_doe'
CThe response time is less than 1 second
DThe response contains an error message
Key Result
Always validate both the HTTP status code and the response data to ensure the API behaves as expected and returns correct information.