In recent posts on code coverage, Alan discussed how to Enable Code Coverage in JCL and How to Capture Code Coverage without Starting IDz. In this post, I will show how to use the Headless Code Coverage collector to export in SonarQube format and display the code coverage result in SonarQube dashboard.
Installing the Headless Code Coverage Collector
To use this function, make sure to select the optional Headless Code Coverage for IBM z/OS Debugger feature when installing IDz. This will result in a headless-cc folder being installed.
Exporting code coverage data using Exportertype
Starting in IDz 14.1.2 (announced as part of IBM Application Delivery Foundation for z Systems V3.1.1),Â you can use theÂ new exportertype option to export code coverage data into different formats. You can specify exporting to multiple exporter types by separating them by a comma. The supported exporter types are:
- CCRESULT – generates the compressed format with the .cczip extension
- CCSONARQUBE – generates the SonarQube format with the .xml extension
- CCRAW – generates the raw format with the .zip extension
For example, run the following command in theÂ headless-cc folder to start the daemon on port 8006 and to export to all three exporter type:
Â Â Â Â Â Â Â codecov â€“startdaemon â€“port=8006 -exportertype=ccresult,ccsonarqube,ccraw
This new exportertype option enables you to export the code coverage data in a format that can be imported by other tools.
Exporting to SonarQube format
You can run the code coverage headless collector with exportertype set to ccsonarqube to export the result in SonarQube format. You should also specify the savesource option to save the source with the code coverage data. For example,
Â Â Â Â Â Â Â codecov â€“startdaemon â€“port=8006 â€“savesource -exportertype=ccsonarqube
After you start the daemon, run the JCL with settings that will connect to the headless code coverage collector. This include setting the machine specific TCPIP address, port number and specifying EQA_STARTUP_KEY=CC. Refer to steps in Enable Code Coverage in JCL andÂ How to Capture Code Coverage without Starting IDz for more details.
After you submitted the JCL with code coverage enabled, the exported XML file and source will be saved on the machine running the headless collector.
This is an example of the generated xml file with code coverage data in SonarQube format:
You can follow the instructions in SonarQube documentation Analyzing with SonarQube Scanner to setup the scanner that will import code coverage data into SonarQube. You can then see the code coverage result in the SonarQube dashboard.
Importing code coverage result into SonarQube
For the SonarQube Scanner to find and import the code coverage data, copy the exported code coverage XML file and the saved source into a directory (called ATMIMS in this example).
Then modify the sonar-project.properties file to point to the source directory and the XML file.
Next, run SonarQube Scanner to scan the code coverage data into SonarQube.
Â Â Â Â Â Â Â sonar-scanner.bat â€“Dâ€™sonar.projectBaseDirâ€™=ATMIMS
Displaying imported result in SonarQube dashboard
After running SonarQube Scanner, paste the project URL in a browser to bring up the SonarQube project dashboard.
Click on the coverage percentage on the project dashboard to get detail breakdown of code coverage by source file.
Click on a source file to bring up the source listing and see the coverage information right in the source. Lines that are covered are indicated by a green ruler while lines that are not covered are indicated by a red ruler.
Itâ€™s easy to export code coverage data
As you can see, it is really easy to set up a code coverage headless collector without a UI so that it can be run automatically, for example, as part of the build process. The code coverage result can be exported to different formats so that it can be consumed by different tools such as SonarQube. You can then fully utilize the capability of these tools to analyze the code coverage data.