2

My team is creating an ASP.NET MVC application. We're using scaffolding to help us in creating our entities. The scaffolder will create the Model, the Repository, the Controller and some other classes that we have. It also scaffolds a basic test suite for the entity. All of the scaffolded code is currently being tested.

What I would like, is to have a unit test that fails if the code coverage for a certain class is below a certain value. For example, the Controller and the Repository need coverage as close to 100% as possible. These classes will also be developed further, and I want a check in place to make sure that the code that is added to them is being tested.

I'm using Xunit and Moq for my testing and mocking respectively.

How can I implement a test for code coverage?

Richard C
  • 2,176
  • 5
  • 30
  • 40
  • ru using a build/CI server? eg teamcity? – wal Jan 27 '16 at 10:57
  • We are doing CI via Visual Studio Online. – Richard C Jan 27 '16 at 10:58
  • ok, you can do this kind of thing in TeamCity (see http://stackoverflow.com/questions/10296753/how-can-i-fail-a-teamcity-build-if-dotcover-doesnt-report-a-high-enough-result if interested) altho given you are on VS online I dont know what is possible – wal Jan 27 '16 at 11:01
  • That is certainly an interesting way of doing it. However, because we're going to be adding a lot of code via scaffolding, it would mean that we would regularly need to update dotCover with the new classes that we've added and tell it what the expected code coverage is. If we can implement this as an actual unit test, then the scaffolder can add it and we don't need any manual intervention. – Richard C Jan 27 '16 at 11:04
  • Just a pointer to point in direction https://msdn.microsoft.com/en-us/library/jj159524.aspx – Preet Singh Jan 27 '16 at 11:28
  • @PreetSingh, the page you linked to is using Team Foundation Build, and not VS Online. Edit and creating build definitions is done in the browser, not in VS; and the pages look completely different. I could not find the Process page, or the option to turn on code coverage. Thanks for the input! – Richard C Jan 27 '16 at 11:39
  • Builds should work the same with vs online. So should be the same procedure. – Preet Singh Jan 27 '16 at 11:56
  • @RichieACC I can document how to enable code coverage in vnext builds, but not the failing if the coverage drops portion. Would an answer along those lines be of interest? – NikolaiDante Jan 27 '16 at 17:04
  • https://msdn.microsoft.com/en-us/library/dd537628.aspx please check this..but its not available in all visual studio versions – Mahesh Malpani Jan 28 '16 at 04:46
  • @MaheshMalpani Thanks for the link. However, I need this implemented in Visual Studio Online, which we're using for our Continuous Integration server. The link you sent deals with Visual Studio, and is not automated. – Richard C Jan 28 '16 at 16:11
  • @NikolaiDante, Since we're not using vNext, I don't see that being of any benefit. Thanks, though. – Richard C Jan 28 '16 at 16:22
  • Ah, I was under the impression that vnext builds were the ones in VSO (instead of the xaml based ones) – NikolaiDante Jan 28 '16 at 16:23

1 Answers1

0

We recently implemented a similar use case on our TFS 2015 server. Its a little more crude in that we aren't measuring coverage for specific namespaces we just test to make sure coverage doesn't decrease overall. For us, this ensures that if a developer adds any code it must be covered otherwise the overall metric decreases.

Our ALM group wrote a PowerShell script to read the coverage numbers from TFS and compare the current build's coverage from the previous builds. If its lower it fails the build. They add the script to the post-test script path to run it during the build.

chief7
  • 14,263
  • 14
  • 47
  • 80