Code Coverage with TCT

In order to obtain the code coverage of your unit tests, you can use the Test Coverage Tool that can be found on InterSystems Open Exchange.

1. Setup your environment

1.1 Download the Test Coverage Tool from Intersystems Open Exchange

1.2 Follow instructions from previous link to write and run your unit tests

1.3 Be sure to follow section "Reporting on results in Cobertura format", as you need to generate the Cobertura file

2. Define source and test paths

Option 1. Define the path of source files and test files in properties file

/opt/sonarqube/sonar-scanner/conf/sonar-scanner.properties
sonar.sources=/opt/jenkins/home/workspace/myproject/src
sonar.tests=/opt/jenkins/home/workspace/myproject/test

Option 2. Use parametrization on command line execution

sonar-scanner {...} -Dsonar.sources=./src -Dsonar.tests=./test

Option 3. Define test packages on your ZPM file

<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25">
  <Document name="config-api.ZPM">
    <Module>
      <Name>config-api</Name>
      <Version>1.1.2</Version>
      <Packaging>module</Packaging>
      <SourcesRoot>src</SourcesRoot>
      <Resource Name="Api.Config.PKG"/>
      <UnitTest Name="/tests" Package="UnitTests.Api.Config" Phase="test"/>
      <Dependencies>
        <ModuleReference>
          <Name>io-redirect</Name>
          <Version>0.4.1</Version>
        </ModuleReference>
      </Dependencies>
    </Module>
  </Document>
</Export>

3. Add Code Coverage on Jenkins task

Option 1. Define the report paths in the properties file

/opt/sonarqube/sonar-scanner/conf/sonar-scanner.properties
sonar.objectscript.cobertura.reportPaths=codecoverage/codecoverage.xml

Option 2. On the sonar-scanner execution, add the parameter -Dsonar.objectscript.cobertura.reportPaths:

cd "/opt/sonarqube/cachedb-import/output/$JOB_NAME"
/opt/sonarqube/sonar-scanner/bin/sonar-scanner \
      -Dsonar.projectKey="$JOB_NAME" \
      -Dsonar.projectName="$JOB_NAME" \
      -Dsonar.sources=./src \
      -Dsonar.tests=./test \
      -Dsonar.projectVersion=1.0 \
      -Dsonar.objectscript.cobertura.reportPaths="$WORKSPACE/codecoverage/codecoverage.xml"
The sonar.objectscript.cobertura.reportPaths is a comma-separated list of paths