0
0
PyTesttesting~8 mins

Why integration tests verify components together in PyTest - Framework Benefits

Choose your learning style9 modes available
Framework Mode - Why integration tests verify components together
Folder Structure
project-root/
├── src/
│   └── app_code.py
├── tests/
│   ├── unit/
│   │   └── test_module_unit.py
│   ├── integration/
│   │   └── test_module_integration.py
│   ├── conftest.py
│   └── fixtures.py
├── pytest.ini
└── requirements.txt
    

This structure separates unit tests and integration tests clearly. Integration tests live in tests/integration/ to verify how components work together.

Test Framework Layers
  • Test Layer: Contains unit and integration test scripts using pytest.
  • Application Layer: The source code modules under src/.
  • Fixtures & Utilities: Shared setup code in conftest.py and fixtures.py to prepare test data and environment.
  • Configuration Layer: pytest.ini and environment variables to control test runs.
Configuration Patterns

Use pytest.ini to set default options like test paths and markers.

[pytest]
markers =
    integration: mark test as integration test
    unit: mark test as unit test
    

Use environment variables or conftest.py fixtures to configure database URLs, API endpoints, or credentials for integration tests.

Example fixture in conftest.py:

import pytest

@pytest.fixture(scope='session')
def db_connection():
    # Setup connection to test database
    conn = connect_to_test_db()
    yield conn
    conn.close()
    
Test Reporting and CI/CD Integration

Use pytest built-in reporting with options like --junitxml=report.xml to generate XML reports.

Integrate with CI/CD pipelines (GitHub Actions, Jenkins, GitLab CI) to run tests automatically on code changes.

Example GitHub Actions snippet:

name: Python Tests

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.12'
      - name: Install dependencies
        run: pip install -r requirements.txt
      - name: Run tests
        run: pytest --junitxml=report.xml
      - name: Upload test report
        uses: actions/upload-artifact@v3
        with:
          name: test-report
          path: report.xml
    
Best Practices for Integration Testing Framework
  • Clear Separation: Keep unit and integration tests in separate folders for clarity.
  • Use Fixtures: Share setup and teardown code with pytest fixtures to avoid duplication.
  • Test Real Interactions: Integration tests should verify how components communicate, not just isolated behavior.
  • Isolate External Systems: Use test databases or mocks for external services to keep tests reliable.
  • Mark Tests: Use pytest markers to easily select or skip integration tests during runs.
Self Check

Where in this folder structure would you add a new integration test for a feature that combines two modules?

Key Result
Integration tests verify how multiple components work together by testing their real interactions in a shared environment.