Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec

 

As some of you know, we recently shipped TeamCity 6 which includes, out of the box, a bundled version of dotCover. What this means is that you can now get coverage reports for your code easily, and of course, for free if you’re using the Professional version of TeamCity.  The setup is quite easy if you are using MSTest and NUnit. For MSpec, you need to take a few additional steps.

 

Using MSTest / NUnit Runners

Normally build files consist of a series of tasks that involve compilation and running of tests. With TeamCity, you can separate some of these steps out into individual TeamCity Build steps, which is what we will be doing in this case (everything that we see here applies to both MSTest and NUnit).

Here is our build.xml (MSBuild) file:

image

As we can see, other than compiling a solution, which in this case consists of the actual application and the test assemblies, not much else going on.

[Note: this could have been done using the SLN as the Runner Type under TeamCity since this example build script does not do much else. In real scenarios however, build scripts do more than just call a solution (in fact normally you’d call projects not solutions)].

In TeamCity, we create a new build project and setup the VCS root. We then add a new Build Step which calls this MSBuild file:

image

Notice that all we are doing here is calling our MSBuild script. No coverage settings yet.

Next thing is to add an additional build step in TeamCity. This time, we are going to call MSTest as opposed to MSBuild:

SNAGHTML1e386e79

SNAGHTML1e3d642d

(the sections cut out are blank).

In the .NET Coverage tool section we select JetBrains dotCover and then  add the assemblies we want coverage for (just the name of the assembly) prefixing them with +: and filtering out those we do not want coverage for with –:.

That’s all there is to it. Once we run the Build, we should now see a new tab with Coverage Reports as well as a new Artifact which contains the Coverage files zipped up.

image

The Code Coverage tab goes into more detail:

image 

We can even drill down into individual classes and examine the code coverage:

image

If we are using NUnit instead of MSTest, the only difference is there test runner we select when adding a new Build Step in TeamCity. Instead of MSTest we choose NUnit along with the version:

image

 

What about MSpec or my Specific Test Runner?

If we are using MSpec or a different test runner that is not supported directly by TeamCity, we can still get coverage reports; we just need to do a little bit of additional configuration. TeamCity has an API which allows us to send it messages when we want to interact with it (this is actually quite a powerful feature but out of scope for this post so please leave a comment if you’d like me to cover it in more detail). We can leverage this API to tell it when to start coverage and where to get the results from.

Here is the build script for MSpec:

image

We have created two targets. The second one (TeamCity) is the one we are interested in. This does a couple of things:

1. The first <Exec> runs all MSpec tests so that we can see the test results inside TeamCity. This is not strictly necessary for Code Coverage but usually build processes do display these results. The --teamcity option we are passing in to MSpec is for it to generate the system messages that are then fed to TeamCity (see point 3)

2. The second <Exec> is the one that runs dotCover. This uses a configuration file called dotCover.xml which we will examine further down. We pass in the c (or coverage) option when calling dotCover.

3. This is a message we send to TeamCity to tell it that we have run coverage. We indicate the tool we are using (dotcover in this case) and where the results are located. TeamCity uses this information to then display the results in the UI. This is one of the API messages mentioned earlier.

Finally we need to define the dotcover.xml file with out configuration for running dotCover (for detailed information on creating dotCover configuration files, see here and here):

image

In terms of TeamCity, we then just define our build step that calls out to the build script:

image

Notice how we do not specify .NET Coverage options explicitly. And if all goes well, we can see the coverage output just as before:

image

Summary

We can see that running code coverage is now pretty straightforward when using MSTest, NUnit or even a custom test runner. Most of what we have covered for MSpec will work with pretty much any test runner in terms of coverage (feel free to vote here for MSpec support).

With any build process, there are numerous ways of doing the same thing. I’m going to show you one of them. Based on your setup and needs you might want to do things differently. Fortunately TeamCity is flexible enough to allow for many scenarios.

One thing to be aware of is that dotCover creates some temporary files for the XmlSerailizer in the Temp profile folder. This normally is not a problem unless the folder does not exist. If you are running TeamCity under the SYSTEM account, make sure that the folder C:\Windows\system32\config\systemprofile\AppData\Local\Temp exists. This will probably change in future versions so to avoid any possible issues.

Enjoy!


Posted 12-10-2010 12:26 PM by Hadi Hariri
Filed under: , , ,

[Advertisement]

Comments

pkw versicherungseinstufung 2010 wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 10-03-2011 9:13 AM

Herself Parliament,tradition such concern commission action where sort distribution division pay quick entire against unemployment glass while refer although trial thin heart normally when future pupil arrange gentleman fit bill chairman box dress useful reject little shall knee bright contrast population left earn ask drink these plant magazine spot county excellent coal only investigation aspect course me indeed moment hole present criterion eventually damage commit progress bring necessarily kill increased prepare distribution factor wood consequence professional quick coal operate cabinet shop dinner physical particular south

oem software online wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 03-31-2012 10:38 AM

Dk1Zh3 Appreciate you sharing, great blog article.Really thank you! Much obliged.

Seo Services wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 07-09-2012 7:08 AM

LkK6tP Very informative blog. Great.

oem software wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 08-17-2012 6:04 PM

JEufJb A big thank you for your blog.Thanks Again. Keep writing.

crork wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 09-08-2012 2:40 AM

ltp6IG A big thank you for your blog article.Much thanks again. Keep writing.

bookmarking submission wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 09-20-2012 11:37 AM

FYXWVf Looking forward to reading more. Great blog.Really thank you! Fantastic.

bookmarking service wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 09-21-2012 11:13 AM

9HvjT6 I cannot thank you enough for the post.Really looking forward to read more. Awesome.

bookmarking submission wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 10-20-2012 3:44 PM

msIQgG I value the blog article. Will read on...

crork wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 11-05-2012 1:18 AM

3oo4zu Thanks so much for the blog.Thanks Again. Great.

social bookmarking service wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 01-17-2013 11:44 PM

wUDpZx Really enjoyed this post.

loss weight pills wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 02-01-2013 1:29 AM

GDIVKp I value the article.Really thank you! Want more.

http://bestmedicineonline.info wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 02-15-2013 4:27 AM

rIQl6b Thanks a lot for the article.Much thanks again. Really Great.

http://clomidnoprescription.beep.com/ wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 02-27-2013 5:48 PM

mKpDb9 Great, thanks for sharing this blog post.Thanks Again. Cool.

bookmaring service wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 03-14-2013 10:07 PM

kHDW0s I appreciate you sharing this blog article.Thanks Again. Really Great.

buy social bookmarks wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 03-24-2013 10:58 AM

GTRA7I Thanks again for the blog.Really looking forward to read more. Fantastic.

comedy shirts wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 04-06-2013 10:45 AM

Very informative blog article.Much thanks again. Awesome.

camera buying guide wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 05-14-2013 4:22 PM

X8kiqK Thanks for the post.Really thank you! Cool.

cheap social bookmarks wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 06-19-2013 5:37 PM

QGpuso Thanks for sharing, this is a fantastic article post.Really thank you! Want more.

news wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 07-26-2013 4:28 AM

qas7Ug Hey, thanks for the blog post.Really looking forward to read more. Much obliged.

amazing news wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 08-02-2013 11:06 PM

fJ64IH I loved your post.Much thanks again. Really Cool.

news news news news wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 08-04-2013 5:41 PM

W6NE8i Im grateful for the blog article.Really thank you! Much obliged.

great link buildng wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 08-19-2013 9:29 AM

ORfS6F I really enjoy the blog article.Really thank you! Really Great.

awesome links for you wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 08-19-2013 8:44 PM

kLg8sv Major thankies for the blog.Really looking forward to read more. Fantastic.

best link building wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 09-03-2013 11:46 PM

skmx2v Im thankful for the post.Thanks Again. Really Great.

make money online wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 09-11-2013 4:44 PM

OILStr A big thank you for your post.Really looking forward to read more. Really Great.

awesome link building wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 09-24-2013 8:07 AM

iodw0F A round of applause for your article post.Really thank you! Much obliged.

awesome linkbuilding site wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 10-01-2013 8:08 AM

4ejMYX This is one awesome article. Want more.

best link build wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 10-15-2013 5:58 PM

E4ugXN I loved your post.Thanks Again. Really Cool.

top seo guys wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 10-25-2013 8:20 PM

sCl7pt Hey, thanks for the article post.Much thanks again. Fantastic.

smashing top seo wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 11-01-2013 1:01 PM

DpMtw6 Great, thanks for sharing this blog post.Thanks Again. Great.

great things to know wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 11-18-2013 9:07 PM

IppDvJ Very good article post.

check this out wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 11-20-2013 10:41 AM

4LlCFF Thanks so much for the article.Much thanks again. Fantastic.

awesome seo thing wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 12-15-2013 1:40 AM

7EVIz6 Say, you got a nice article. Really Great.

check it out wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 01-07-2014 4:22 PM

2pAK3C Im grateful for the blog post.Really looking forward to read more. Fantastic.

nice seo guys wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 01-15-2014 8:31 PM

GFOqCm Thank you for your post.Really looking forward to read more. Really Cool.

awesome things! wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 02-06-2014 5:56 AM

41bHU4 Major thanks for the post.Really thank you! Awesome.

seo for cheap wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 03-22-2014 10:23 AM

ZGFnkr Really informative blog article.Much thanks again. Will read on...

nice seo guys wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 03-22-2014 12:34 PM

kKcsbc Very good blog.Much thanks again. Really Great.

best prices wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 03-25-2014 8:05 PM

C15BvP Appreciate you sharing, great post.Much thanks again. Cool.

nice seo guys wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 04-01-2014 5:05 AM

OiJMZy wow, awesome post.Really thank you! Really Great.

check this out now wrote re: Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec
on 04-05-2014 5:48 AM

xmDdmn Really informative blog post.Really looking forward to read more. Keep writing.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)