SonarQube Enterprise 9.8.0.63668
Trying to import test data output from Postman Lab’s tool: Newman. This is essentially a headless Postman instance that executes all tests in a Postman collection, and generates output in various formats. What is under test is an API either locally or remotely deployed based on the code in the repository. The postman collection itself is a JSON file.
Newman can generate JUnit format output, which I have tried to import using the sonar.junit.reportPaths
property. This does not appear to import as I see no count of unit tests on the front page of the project in SonarQube. An example output looks something like this:
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite id="0" hostname="XXXXX" package="Status" name="Successful" tests="2" failures="0" errors="0" timestamp="2023-01-28T16:09:01.929" time="0.175">
<testcase classname="Status / Successful" name="Status - Successful - Response Status Code is 200" time="0.087"/>
<testcase classname="Status / Successful" name="Status - FAIL - Response Content-Type Header is application/json" time="0.087">
<failure type="AssertionError" message="expected 'application/json' to include 'application/xml'">
<![CDATA[AssertionError: expected 'application/json' to include 'application/xml'
at Object.eval sandbox-script.js:2:1)]]>
</failure>
</testcase>
</testsuite>
...
(ignore the failure, I intentionally modified the test to get a failure to see what that would look like)
When I run SonarScanner with --debug
I see no mention of this file at all - so I can only assume it’s not being imported because there is no Java code in the repository. That’s ok I thought, I’ll write an output in Sonar’s generic test data format… Which I did and it gives me this:
<?xml version="1.0" encoding="UTF-8"?>
<testExecutions version="1">
<file path="tests/XXXXX.postman_collection.json">
<testCase name="GET XXXXX (Status - Successful - Response Status Code is 200)" duration="0.175"/>
<testCase name="GET XXXXX (Status - FAIL - Response Content-Type Header is application/json)" duration="0.175">
<failure message="expected 'application/json' to include 'application/xml'"><![CDATA[AssertionError: expected 'application/json' to include 'application/xml'
at Object.eval sandbox-script.js:2:1)]]></failure>
</testCase>
...
So far so good. I then pointed to the output XML file using the sonar.testExecutionReportPaths
property. I also have sonar.tests=tests
so that the Postman collection JSON is considered a test file. Running SonarScanner with --debug
I can see this file referenced now:
17:13:26.513 INFO: Sensor Generic Test Executions Report
17:13:26.514 INFO: Parsing C:\dev\apim-api-XXX\reports\sonarqube\TESTS-XXXXXX.postman_collection.xml
17:13:26.529 INFO: Imported test execution data for 0 files
17:13:26.529 INFO: Test execution data ignored for 1 unknown files, including:
tests/HD_Mobile_API.postman_collection.json
17:13:26.529 INFO: Sensor Generic Test Executions Report (done) | time=16ms
But, the Postman collection in JSON is an ignored file by SonarScanner, so the test results aren’t being tracked.
Is there any way to make this work?
Any and all help appreciated.