0
0
PyTesttesting~8 mins

Testpaths configuration in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Testpaths configuration
Folder Structure
tests/
├── unit/
│   ├── test_example1.py
│   └── test_example2.py
├── integration/
│   └── test_integration.py
├── e2e/
│   └── test_end_to_end.py
├── __init__.py
conftest.py
pytest.ini
Test Framework Layers
  • Test Files: Organized under tests/ by type (unit, integration, e2e).
  • Fixtures and Hooks: Defined in conftest.py for setup and teardown.
  • Configuration: pytest.ini controls test discovery and testpaths.
  • Utilities: Helper functions or modules can be placed in a utils/ folder if needed.
Configuration Patterns

Use pytest.ini to specify testpaths to tell pytest where to look for tests.

[pytest]
testpaths = tests/unit tests/integration

# This limits pytest to only look inside these folders for tests.

Other config options can include markers, addopts, and python_files patterns.

Use conftest.py for shared fixtures and environment setup.

Test Reporting and CI/CD Integration
  • Use pytest plugins like pytest-html or pytest-cov for HTML reports and coverage reports.
  • Configure CI pipelines (GitHub Actions, Jenkins, GitLab CI) to run pytest commands using the pytest.ini config.
  • Reports can be saved as artifacts and viewed after test runs.
  • Fail the build if tests fail to ensure quality gates.
Best Practices
  1. Organize tests by type: Keep unit, integration, and e2e tests in separate folders for clarity.
  2. Use testpaths in pytest.ini: This speeds up test discovery and avoids running unwanted files.
  3. Keep conftest.py clean: Only put shared fixtures and hooks here to avoid confusion.
  4. Use descriptive test file names: Start with test_ or end with _test.py for pytest to find them easily.
  5. Integrate reporting in CI: Always generate readable reports and fail builds on test failures.
Self Check

Where would you add a new test file for API integration tests in this framework structure?

Key Result
Use pytest.ini's testpaths to control where pytest looks for tests, organizing tests clearly by type.