Importing python test run information into sonarqu

2019-07-15 02:30发布

问题:

I have a python project with structure like this and I'm trying to import it into SonarQube:

root
    sonar-project.properties
    src
    test
        nosetests.xml

All test files are in test folder. nosetests.xml is xml report generated with nose(tests) (1.3.7).

My sonar-scanner.properties file looks like this

sonar.projectKey=python-sonar
sonar.projectName=python-sonar
sonar.projectVersion=1.0
sonar.sources=src
sonar.tests=test
sonar.language=py
sonar.sourceEncoding=UTF-8
#I want the details so this is false.
sonar.python.xunit.skipDetails=false
sonar.python.xunit.reportPath=test/nosetests.xml
sonar.python.coverage.itReportPath=test/cover.xml

When trying to import project into SonarQube I get following error for all the tests and no test information is import into SonarQube.

WARN: The resource for 'unittests.testcase' is not found, drilling down to the details of this test won't be possible.

Digging into details I found out the problem is that PythonXUnitSensor looks for test files on path that reflects test case name. For example test lies at path test/unitests/testcase and if it has name test.unitests.testcase in XML report it is imported. However nosetests names it unitests.testcase so it doesn't get imported.

I am looking for a clean way how to solve this so it can be use in CI and reused. I don't want to edit reports, I'm looking for a way to generate them properly or to import them the way they are.

回答1:

This seems best addressed at the nosetest level i.e. getting nosetests.xml to report the fully qualified path. That will let SonarPython (and potentially other tools) do the matching properly.

Simple approach to that: add an empty __init__.py under test and rerun your tests. That'll make test part of the classpath, and nosetests.xml should report on test.unitests.testcase .