Framework Mode - pytest-cov setup
Folder Structure
project-root/ ├── tests/ │ ├── test_example.py │ └── __init__.py ├── src/ │ └── app_code.py ├── .coveragerc ├── pytest.ini ├── requirements.txt └── README.md
Jump into concepts and practice - no test required
project-root/ ├── tests/ │ ├── test_example.py │ └── __init__.py ├── src/ │ └── app_code.py ├── .coveragerc ├── pytest.ini ├── requirements.txt └── README.md
tests/ folder, contains pytest test functions.src/ folder, the code under test.pytest.ini and .coveragerc files configure pytest and coverage behavior.tests/ or a separate tests/utils/ folder.Use pytest.ini to configure pytest options and enable coverage plugin:
[pytest] addopts = --cov=src --cov-report=term-missing
Use .coveragerc to customize coverage settings, for example:
[run]
branch = True
source = src
[report]
exclude_lines =
if __name__ == "__main__":
pragma: no cover
Manage environment variables or credentials using pytest fixtures or external files, keeping secrets out of code.
--cov-report=xml to generate XML reports for CI tools.--cov-fail-under=80.if __name__ == '__main__'.Where would you add a new fixture that sets up a test database connection for coverage-instrumented tests?
pytest-cov in testing?pytest --cov=my_module if all code in my_module is covered by tests?pytest --cov=my_module --cov-report=html but no HTML report is generated. What is the most likely cause?mod1 and mod2 and generate both terminal and HTML reports. Which command is correct?