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!




Tuesday, 11 July 2017

What's this all about

My intention is that this is an occasional blog where I put code snippets techniques etc as I come across them in the hope they will be useful for others.

For some more background I use the following software

Visual Studio (2017) - as I have an msdn subscription I can take full advantage of this version and I already like it more then previous versions, for me, so far it's been more stable

NCrunch - Fantastic Continuous testing tool, worth every penny and if you have access to a server with spare capacity it can make use of that too.

Resharper Surely this needs no explanation other than to say that if you have the all singing all dancing version then it can do some of the same things that NCrunch does as well as all the rest of its magic

ServiceStack Calling it an awesome framework for creating web services, massively undersells it. I expect much of this blog will be related to this in one way or another.


VS Code  This has become my go to text editor, not forgetting the insiders edition which can be installed alongside. Microsoft at it's finest again.

Notepad++ A long standing favourite, though I find myself using it less now that I have VS code

Mercurial I chose this over Git and others some time ago and still have no compelling reason to change, it does everything I want, I also find it has less of a steep learning curve than Git which makes it easier to persuade non developers to use it

TortoiseHG One of the best Gui's for Mercurial, I still have found nothing comparable for Git, just a shame that it doesn't have HGFlow integration

SourceTree Nice looking Mercurial/Git gui but I use it mostly for the HGFlow integration

YouTrack Jetbrains issue tracker. Love the integration with version control and CI

UpSource Jetbrains Code review tool. Love the integration with issue tracking and CI

TeamCity Jetbrains CI server, love the integration with the issue tracker and Code review and slack

Slack Team messaging hub of choice

OneNote As I have a surfacebook I find this a really useful tool

Browsers

Chrome, Chrome Canary, FireFox Developer, FireFox nightly, IE11. I can't include Edge as it just hangs every time I try to use it.



On the hardware side I have a Macbook Pro and a SurfaceBook Pro. For anyone who is interested the Macbook is permanently sitting in my bag (sorry Apple fans). Both are fantastic bits of kit, but for me overall the Surfacebook wins out.