0
0
PyTesttesting~8 mins

pytest.ini configuration - Framework Patterns

Choose your learning style9 modes available
Framework Mode - pytest.ini configuration
Folder Structure
project-root/
├── tests/
│   ├── test_example.py
│   └── conftest.py
├── src/
│   └── app_code.py
├── pytest.ini
└── requirements.txt
  
Test Framework Layers
  • Configuration Layer: pytest.ini file to set global pytest options and markers.
  • Test Layer: Test files inside tests/ folder containing test functions.
  • Fixtures Layer: conftest.py for shared setup and teardown code.
  • Application Layer: Source code under src/ folder.
  • Utilities Layer: Helper functions or modules imported by tests or fixtures.
Configuration Patterns

The pytest.ini file is used to configure pytest behavior globally for the project.

Common configurations include:

  • Markers: Define custom markers to categorize tests.
  • Test paths: Specify directories or file patterns to discover tests.
  • Command line options: Set default options like verbosity or capture mode.
  • Logging: Configure logging levels and formats.

Example pytest.ini content:

[pytest]
minversion = 7.0
addopts = -ra -q
testpaths = tests
markers =
    smoke: Quick smoke tests
    regression: Full regression suite
log_cli = true
log_cli_level = INFO

This setup helps run tests consistently across environments without repeating command line arguments.

Test Reporting and CI/CD Integration

pytest.ini can configure reporting options such as verbosity and logging to help understand test results.

For CI/CD pipelines:

  • Use addopts to include plugins like pytest-html for HTML reports.
  • Configure markers to selectively run tests (e.g., smoke tests on every commit).
  • Set logging to capture detailed info for debugging failures.
  • Integrate pytest commands in CI scripts referencing pytest.ini for consistent runs.

Example snippet for CI usage:

pytest --html=report.html --self-contained-html
Best Practices
  • Keep pytest.ini simple: Only add necessary global options to avoid confusion.
  • Use markers wisely: Define clear categories for tests to run subsets easily.
  • Version control pytest.ini: Keep it in the project root under version control for team consistency.
  • Document markers: Add comments or documentation for custom markers in pytest.ini.
  • Combine with conftest.py: Use pytest.ini for config and conftest.py for fixtures and hooks.
Self Check

Where in this framework structure would you add a new custom marker called slow to categorize tests that take longer to run?

Key Result
Use pytest.ini to centrally configure pytest options, markers, and logging for consistent test runs.