Bird
Raised Fist0
PyTesttesting~8 mins

API client testing in PyTest - Framework Patterns

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Framework Mode - API client testing
Folder Structure
api-client-testing/
├── tests/
│   ├── test_users.py
│   ├── test_orders.py
│   └── conftest.py
├── api_client/
│   ├── __init__.py
│   ├── client.py
│   └── endpoints.py
├── utils/
│   ├── __init__.py
│   └── helpers.py
├── config/
│   └── config.yaml
├── requirements.txt
└── pytest.ini
Test Framework Layers
  • API Client Layer: Contains client.py with methods to call API endpoints and endpoints.py defining URL paths.
  • Test Layer: Located in tests/, includes test files like test_users.py that use pytest to verify API responses.
  • Fixtures & Setup: conftest.py provides reusable pytest fixtures for setup like authentication tokens or base URLs.
  • Utilities: Helper functions for common tasks like data formatting or response validation in utils/helpers.py.
  • Configuration: Centralized settings such as environment URLs and credentials stored in config/config.yaml.
Configuration Patterns
  • Environment Management: Use config.yaml to define base URLs for dev, staging, and production environments.
  • Credentials: Store API keys or tokens securely in environment variables or encrypted files, loaded by conftest.py.
  • pytest.ini: Configure pytest options like markers and test paths.
  • Dynamic Config Loading: Fixtures read config files and environment variables to provide test code with current settings.
Test Reporting and CI/CD Integration
  • pytest Reports: Use built-in pytest output with verbosity and --junitxml=report.xml for XML reports.
  • Plugins: Integrate pytest-html for readable HTML reports and pytest-cov for coverage reports.
  • CI/CD: Configure pipelines (GitHub Actions, Jenkins, GitLab CI) to run tests on push or pull requests, fail builds on test failures.
  • Notifications: Send test results via email or chat tools after CI runs.
Best Practices
  • Use Page Object Model for APIs: Encapsulate API calls in client classes to keep tests clean and reusable.
  • Isolate Tests: Each test should be independent and not rely on others or shared state.
  • Use Fixtures for Setup: Manage authentication tokens and environment setup with pytest fixtures.
  • Validate Responses Thoroughly: Check status codes, response schema, and data correctness.
  • Keep Config Separate: Avoid hardcoding URLs or credentials in test code; use config files and environment variables.
Self Check

Where in this framework structure would you add a new API endpoint method for the "Products" resource?

Key Result
Organize API client tests with clear layers: client code, tests, fixtures, utilities, and config for maintainability and clarity.

Practice

(1/5)
1. What is the main purpose of API client testing in pytest?
easy
A. To verify that the code correctly communicates with the API
B. To check the user interface layout
C. To test database schema changes
D. To measure application performance speed

Solution

  1. Step 1: Understand API client testing goal

    API client testing ensures the code sends requests and receives responses correctly from an API.
  2. Step 2: Compare options with API client testing focus

    Only To verify that the code correctly communicates with the API relates to verifying communication with the API, others focus on unrelated areas.
  3. Final Answer:

    To verify that the code correctly communicates with the API -> Option A
  4. Quick Check:

    API client testing = verify API communication [OK]
Hint: API client testing checks API communication correctness [OK]
Common Mistakes:
  • Confusing API testing with UI testing
  • Thinking it tests database directly
  • Mixing performance testing with API client testing
2. Which pytest code snippet correctly asserts that an API response status code is 200?
easy
A. assert response.statusCode == 200
B. assert response.status == 200
C. assert response.code == 200
D. assert response.status_code == 200

Solution

  1. Step 1: Identify correct attribute for status code in response

    In most Python HTTP clients, the status code is accessed via response.status_code.
  2. Step 2: Verify assertion syntax

    The assertion assert response.status_code == 200 is syntactically correct and checks the status code properly.
  3. Final Answer:

    assert response.status_code == 200 -> Option D
  4. Quick Check:

    Correct attribute is status_code [OK]
Hint: Use response.status_code to check HTTP status [OK]
Common Mistakes:
  • Using incorrect attribute names like status or code
  • Missing assert keyword
  • Using camelCase instead of snake_case
3. Given the pytest test below, what will be the output if the API returns JSON {'success': true}?
def test_api_response(client):
    response = client.get('/status')
    data = response.json()
    assert data['success'] is True
medium
A. Test fails due to assertion error
B. Test fails due to KeyError
C. Test passes because 'success' is True
D. Test raises a syntax error

Solution

  1. Step 1: Analyze the API response and JSON parsing

    The API returns JSON with key 'success' set to true, which maps to Python True after parsing.
  2. Step 2: Check the assertion logic

    The assertion checks if data['success'] is True, which matches the parsed value, so it passes.
  3. Final Answer:

    Test passes because 'success' is True -> Option C
  4. Quick Check:

    JSON true = Python True, assertion passes [OK]
Hint: True in JSON becomes True in Python, assertion passes [OK]
Common Mistakes:
  • Confusing JSON true with string 'true'
  • Expecting KeyError when key exists
  • Misreading assertion logic
4. Identify the error in this pytest API test code:
def test_get_user(client):
    response = client.get('/user/1')
    assert response.status_code = 200
    assert response.json()['id'] == 1
medium
A. Using single equals '=' instead of double '==' in assertion
B. Missing parentheses in response.json call
C. Incorrect endpoint URL format
D. No error, code is correct

Solution

  1. Step 1: Check assertion syntax

    The line assert response.status_code = 200 uses single '=' which is assignment, not comparison.
  2. Step 2: Confirm correct assertion operator

    Assertions require '==' to compare values, so it should be assert response.status_code == 200.
  3. Final Answer:

    Using single equals '=' instead of double '==' in assertion -> Option A
  4. Quick Check:

    Use '==' for comparison in assert [OK]
Hint: Use '==' in assert, not '=' [OK]
Common Mistakes:
  • Confusing assignment '=' with comparison '=='
  • Forgetting parentheses in method calls
  • Assuming no syntax error in assert
5. You want to test an API client method that sends a POST request with JSON data and expects a 201 status code. Which pytest test code correctly performs this check?
hard
A. def test_create_item(client): response = client.post('/items', data={'name': 'book'}) assert response.status_code == 201
B. def test_create_item(client): response = client.post('/items', json={'name': 'book'}) assert response.status_code == 201
C. def test_create_item(client): response = client.post('/items', json='name=book') assert response.status_code == 201
D. def test_create_item(client): response = client.post('/items', json={'name': 'book'}) assert response.status == 201

Solution

  1. Step 1: Identify correct way to send JSON data in POST request

    Using json={'name': 'book'} sends JSON properly; data= sends form data, which is incorrect here.
  2. Step 2: Verify correct status code assertion

    Use response.status_code == 201 to check for created resource status; response.status is invalid.
  3. Final Answer:

    def test_create_item(client): response = client.post('/items', json={'name': 'book'}) assert response.status_code == 201 -> Option B
  4. Quick Check:

    Use json= for JSON, assert status_code == 201 [OK]
Hint: Use json= dict and assert status_code == 201 [OK]
Common Mistakes:
  • Using data= instead of json= for JSON payload
  • Passing JSON as string instead of dict
  • Using response.status instead of response.status_code