I’m using pytest to run python tests and calculate code coverage.
Sonar configuration:
sonar.projectKey=$(projectKey)
sonar.python.version="3.10"
sonar.python.coverage.reportPaths=coverage.xml
Pytest command used to run tests:
poetry run pytest --cov-config=.coveragerc --cov=src --cov-report xml -vv -x --durations=0 tests/unit_tests
Repository structure:
├── __pycache__
├── docs
├── src
└── tests
If I execute the pytest locally, coverage % is reported as 98%. But the same test, when executed in a pipeline and viewed in sonar cloud portal, sonarcloud is reporting 45%. After investigating more, I found that Sonar is considering the python code in tests
directory as source code and expecting it to be covered which shouldn’t be the case.
-------------------------------------------------------------------------------
TOTAL 2134 40 98%
How do I instruct sonar to use the coverage.xml produced by pytest and ignore any code present in tests
direcotry while calculating coverage % ?