Python code coverage % calculation error

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 % ?

You can specify sonar.tests, which automatically excludes these files from code coverage calculation.

Thanks Colin. Set sonar.tests pointing to tests folder?

You’ve got it!

1 Like

I’ve added sonar.tests=tests to our pipeline and now results are disappearing from sonar portal.

sonar.projectKey=$(projectKey)
sonar.python.version="3.10"
sonar.python.coverage.reportPaths=coverage.xml
sonar.tests=tests

Further investigation in code shows only tests folder, but not source code.

sonar.sources should be defaulting to the current directory, but maybe there’s some interaction I don’t know about when only sonar.tests is set.

Try setting sonar.sources=src