This is an update to my earlier Unity×Sonarqube posts. I’m writing this in 2022 – things have changed since I wrote those. I’m keeping the old articles, but you’ll find a current version of the batch file I’m using, with the differences highlighted here.

I suggest reading the old posts first for explanations:

You’ll only find updates to those articles in this one.

Sonarqube

Sonar needs the .net SDK now. Get it: https://docs.microsoft.com/en-us/dotnet/core/sdk

Their documentation currently has some typos/outdated sections. The dotnet core SDK has its own CLI now, dubbed dotnet. You’ll use that to install a package:

dotnet tool install --global dotnet-sonarscanner

This should add dotnet-sonarscanner to your path, so you’ll call that instead of the previous SonarScanner.MSBuild.exe. The build process got easier, too, the hardcoded path now replaced with dotnet build.

Unity

Coverage is now out of beta, and is a package you need to add to your project. Use the package manager to find it.

There is a new switch called -debugCodeOptimization; this is recommended to get the most accurate coverage info.

I’m now running both Play Mode and Test Mode unit tests. This is specified with the -testPlatform switch. This will generate two xml files; we pass them to sonar using wildcards.

I’m also importing unit test results too. Sadly, sonar doesn’t do a lot with them, but you can see how many you have, and they may integrate them more deeply in the future, so fingers crossed for that. As with coverage, there will be two results for Play Mode and Edit Mode, and we need to parse them both.

The result

Here’s the updated .bat file. Again, change names to your project and your paths; pay attention to apostropes, because they can break your paths; and note that I’ve split commands into multiple lines by adding ^ to the end of the line and a leading space to the next line.

@echo off

SET "unity_project=C:\unity\sonar.test.project"
SET "solution=sonar.test.project"
SET "unity=c:\Program Files\Unity\Hub\Editor\2020.3.27f1\Editor\Unity.exe"
SET "coverage=\CodeCoverage\%solution%-opencov\**"
SET "sonarproject=YOUR_SONAR_PROJECT_NAME"
SET "sonartoken=YOUR_SONAR_PROJECT_TOKEN"

echo Sonar scanner firing up
dotnet-sonarscanner begin^
 /k:"%sonarproject%"^
 /d:sonar.login="%sonartoken%"^
 /d:sonar.cs.opencover.reportsPaths="%unity_project%%coverage%"^
 /d:sonar.cs.nunit.reportsPaths="%unity_project%\TestResult*.xml"

echo Rebuilding project
dotnet build "%solution%.sln" /t:Rebuild

echo Running Edit Mode tests
"%unity%"^
 -projectPath "%unity_project%"^
 -batchmode -runTests -debugCodeOptimization -enableCodeCoverage^
 -testPlatform editmode^
 -testResults "%unity_project%\TestResultEditMode.xml"^
 -coverageResultsPath "%unity_project%"^
 -coverageOptions "generateAdditionalMetrics;assemblyFilters:-*unity*"

echo Running Play Mode tests
"%unity%"^
 -projectPath "%unity_project%"^
 -batchmode -runTests -debugCodeOptimization -enableCodeCoverage^
 -testPlatform playmode^
 -testResults "%unity_project%\TestResultPlayMode.xml"^
 -coverageResultsPath "%unity_project%"^
 -coverageOptions "generateAdditionalMetrics;assemblyFilters:-*unity*"

echo Sonar scanner finished
dotnet-sonarscanner end /d:sonar.login="%sonartoken%"

I tried to keep things in their default locations as much as I could. The following files will be generated in your project root:

TestResultEditMode.xml
TestResultPlayMode.xml
CodeCoverage\projectname-opencov\EditMode\TestCoverageResults_0000.xml
CodeCoverage\projectname-opencov\PlayMode\TestCoverageResults_0000.xml

Now write tests and fix those code smells!