CodeScan now provides a way to view your unit test coverage from your SFDX projects in SonarQube.
- A working SonarQube (6.7.4+) installation
- A licensed CodeScan (4.1+) plugin
- An SFDX project linked to your Salesforce Org
Run your tests in your project’s root directory using
sfdx force:apex:test:run and the following parameters:
-cThis is to return code coverage
-d .This is to define the output directory. In this case the current directory (.).
-u your-usernameThis is your username or alias.
-r jsonThis defines the output type as a JSON file.
The end command should look something like this:
sfdx force:apex:test:run -c -d . -u username -r json. Run this command.
If all goes to plan, you should find a JSON file in your project’s directory named
test-result.json. In order to import this into SonarQube, pass the parameter
sf.testfile with your build. For the file you have just created in the root folder of your project, you will need to pass
sf.testfile=test-result.json. If you have created the test file somewhere else, you will need to provide the path relative to the root folder of your project.