Testing your .NET code without paying a gazillion dollars for analysis software - Part II: Getting Started with SonarQube
SonarQube is an open-source quality analysis platform that supports more than 20 programming languages. C# is one of them.
NOTE: SonarQube requires the Java JDK to run, which you can get here.
Download the base installer and Sonar Runner from http://www.sonarqube.org/downloads/. Unzip them wherever you like. I put mine at D:\Tools\SonarQube and D:\Tools\SonarRunner.
Create a text file called sonar-project.properties in the same directory as your project or solution. A basic example is below to get you started:
# Required metadata
sonar.projectKey=test:myproject
sonar.projectName=MyTestProject
sonar.projectVersion=1.0
# Specify the source code location (e.g. "./source/project")
sonar.sources=.
# Encoding of the source code
sonar.sourceEncoding=UTF-8
Fire up StartSonar.bat. Since I have a 64-bit machine, my batch file lives at \SonarQube\bin\windows-x86-64. Open a web browser and navigate to http://localhost:9000, which is the default URL served up by SonarQube.
Login at the top right using admin/admin as the credentials. Go to Settings > Update Center, scroll down to languages and click C#, then Install:
You'll need to restart SonarQube for that to take effect. Restart it by hitting CTRL+C in the command prompt window hosting the session to kill the job, then run StartSonar.bat again.
Now that SonarQube is set up and our properties file is in place, we just need to run the analysis. The easiest way to do that is to add the path to SonarRunner/bin to your PATH environment variable so you can run it from anywhere.
Then, it's just a matter of opening a prompt, going to the directory where the .properties file is, and entering:
sonar-runner
Go back to the SonarQube web dashboard, and it will be populated with data. Subsequent runs of the analysis will show trends - like a downward arrow if your complexity is lower.
Changing the metadata in the sonar-project.properties file will impact how the data appears in the dashboard. Use a different project key and name for each project to be able to see all of them at a glance.
Like most analysis tools, there are default rules that you likely won't care about, but this is an excellent tool to target areas for refactoring as well as a starting point for a code review.