Use pytest to generate a coverage report in coverage.xml and enable Sonarqube to read the report.

Ignoring directory/ files1

In .coveragerc:

[run]
omit = tests/*

Generate your coverage report called coverage.xml:

pytest --cov-config=.coveragerc
       --cov-report xml:path/to/coverage.xml
       --cov=myproj
       myproj/tests/

--cov-report xml generate coverage report in xml --cov-report xml:/path/to/store/coverage.xml

Both approaches mentioned below work2.

Approach 1 using pytest and specify the coverage.xml path**

pytest --cov-config=.coveragerc
       --cov-report xml:path/to/coverage.xml
       --cov=myproj
       myproj/tests/

Approach 2 using coverage xml -i to format for SonarQube

pytest --cov-config=.coveragerc
       --cov-report xml
       --cov=myproj
       myproj/tests/
coverage xml -i

sonar-project.properties3

To get SonarQube to the read the coverage report include the following in the properties file.

sonar.python.coverage.report-path=path/to/coverage.xml