0
0
PyTesttesting~8 mins

setup.cfg configuration in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - setup.cfg configuration
Folder Structure
project-root/
├── tests/
│   ├── test_example.py
│   └── conftest.py
├── src/
│   └── app_code.py
├── setup.cfg
└── pytest.ini (optional)
  
Test Framework Layers
  • Tests: Located in tests/ folder, contains test scripts like test_example.py.
  • Fixtures & Config: conftest.py holds shared setup code and fixtures.
  • Application Code: In src/ folder, the code under test.
  • Configuration: setup.cfg file configures pytest options and plugins.
Configuration Patterns

The setup.cfg file centralizes pytest settings. It helps to:

  • Set test discovery rules (e.g., test file patterns).
  • Configure markers to label tests (e.g., slow, smoke).
  • Define command line options defaults (e.g., verbosity).
  • Manage plugin options (e.g., coverage, junitxml reports).

Example setup.cfg content:

[tool:pytest]
minversion = 7.0
addopts = -ra -q
testpaths = tests
python_files = test_*.py
markers =
    smoke: quick smoke tests
    slow: slow running tests
  

This file lives at the project root and is automatically read by pytest.

Test Reporting and CI/CD Integration

Use setup.cfg to configure reporting plugins like pytest-cov and pytest-junitxml.

[tool:pytest]
addopts = --cov=src --cov-report=term-missing --junitxml=reports/junit.xml
  

CI/CD pipelines can run pytest with these settings to generate coverage and XML reports for dashboards.

Reports help teams see test results clearly and catch failures early.

Best Practices
  • Centralize config: Keep pytest options in setup.cfg to avoid repeating CLI flags.
  • Use markers: Define and document test markers for easy test selection.
  • Keep config simple: Only add needed options to avoid confusion.
  • Version control: Commit setup.cfg so all team members share the same test setup.
  • Combine with conftest.py: Use setup.cfg for config and conftest.py for fixtures and hooks.
Self Check

Where in this framework structure would you add a new marker called regression to label regression tests?

Key Result
Use setup.cfg to centrally configure pytest options, markers, and plugins for a clean, maintainable test setup.