Thursday, 21 September 2017

Creating a Code Coverage badge from TeamCity

Introduction 

Badges have become increasingly popular with developers and have been adapted to provide a range of data. The advantage of them is that they are simple and easy to understand. The colour coding can help with this too.

Background

I have a Teamcity CI server and use it for most of my code. I wanted to have some sort of dashboard for each application, this consists of a simple web page overview so that users can easily see what the state of the application is. In order to do this I decided to add some badges. Teamcity has it's own built in badge for build status and this is what I'm using this in a dashboard .

Creating the badge

TeamCity gathers a lot more information than just build status and I wanted to show some more of this information in my dashboard, in particular I wanted an overall code coverage figure. I'm assuming that if you are here you have some basic familiarity with TeamCity.

The first step is to make sure you are calculating code coverage. I use the built in DotCover, which is setup in the Unit Test build step
This generates the Code Coverage report that you can see part of below.

The figure I wanted was the All Classes percentage.

Now that we have the coverage report setup, we need to look at the TeamCity Api and determine where to find our coverage. Fortunately this isn't too tricky but it does require two calls. From looking at the API documentation we need the BuildTypeId in order to access the coverage report.

The first step is to determine our BuildType ID. This can easily be found by navigating the teamcity interface. If we go to the details of any particular build then look at the url you will see the buildTypeId as a parameter
TeamCity exposes it's build information via the rest API at
 http://<TeamCityServer>/app/rest/builds/
if you then add your buildTypeId as a parameter you will get information specific to your build e.g.
http://<TeamCityServer>/app/rest/builds/buildType:MyBuildTypeId/
 - obviously you will replace <TeamCityServer> with your servers url and MyBuildTypeId with your BuildTypeId. This gives you a whole slew of information about that particular build, however we just want the Id of the most recent build as that gives us access to our code coverage data. In order to access this we add /id to the route e.g.
 http://<TeamCityServer>/app/rest/builds/buildType:MyBuildTypeId/id
 which if you open in your browser should give a single number which is the build id we want.

We can now get the statistics for our build using
http://<TeamCityServer>/app/rest/builds/89650/statistics/
 which gives a list of all the statistics we might want.
As you can see the number we want is called CodeCoverageC so we simply append that to our route to get just the value we need ie.
http://<TeamCityServer>/app/rest/builds/89650/statistics/CodeCoverageC

 Fortunately I had already created some other badges using shields.io.This is a pretty simple process as you can see from the bottom of the home page, so I won't go into too much detail other than to note from the url that I had to url encode the space and the percent sign I wanted.
 This is the result and I'm pretty pleased with it - though I do need to write more tests!




No comments:

Post a Comment