Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSTest Code Coverage

Tags:

Is there a way to test code coverage within visual studio if I'm using MSTest? Or do I have to buy NCover?

Is the NCover Enterprise worth the money or are the old betas good enough if Microsoft doesn't provide built in tools to do code coverage?

EDIT: Description of VS Products and which ones include code coverage https://www.visualstudio.com/vs/compare/

TestDriven.NET (http://testdriven.net/) can be used if your VS version doesn't support it.

like image 710
sontek Avatar asked Jan 06 '09 05:01

sontek


People also ask

What is MSTest used for?

MSTest is Microsoft's tool used to run tests of . NET applications (in particular, it can be used for unit testing). In TestComplete, you can integrate your MSTest tests to your test projects and run them as part of your automated testing process.

How do I check my code coverage?

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.

How do I get code coverage in Visual Studio code?

On the Test menu, select Analyze Code Coverage for All Tests. You can also run code coverage from the Test Explorer tool window. Show Code Coverage Coloring in the Code Coverage Results window. By default, code that is covered by tests is highlighted in light blue.

How do I generate code coverage in Runsettings?

Add a run settings file to your solution. In Solution Explorer, on the shortcut menu of your solution, choose Add > New Item, and select XML File. Save the file with a name such as CodeCoverage. runsettings.


2 Answers

MSTest includes code coverage, at least it does in the version of VS I have. However, you need to enable the instrumentation in the testrunconfig, which is just ugly and a major PITA.

A much easier option is to use TestDriven.NET, which can automate coverage, even for MSTest. And since it uses the MSTest core, you still get all the VS goodness such as colorization (red/blue lines for covered code). See here (including a screencast), or since an image says a thousand words:

alt text
(source: mutantdesign.co.uk)

like image 168
Marc Gravell Avatar answered Oct 13 '22 01:10

Marc Gravell


Yes, you can find code coverage information from within Visual Studio, provided that you have a version of Visual Studio that provides that functionality, such as the Team System. When setting up the unit tests in VS.NET, a localtestrun.testrunconfig file will be created and added as part of the solution. Double-click this file and find the option Code Coverage option on the left of the dialog. Select the assemblies for which you want to collect code coverage information and then re-run the unit tests. Code coverage information will be collected and is available. To get the code coverage information open the test results window and click on the code coverage results button, which will open an alternative window with the results.

like image 21
sduplooy Avatar answered Oct 13 '22 01:10

sduplooy