'How to discover which test unit checks which lines of code?
I was fooling around the NUint, hoping to discover a way to realize which line of code passes in which test.
Imagine I have a method for which I have 3 tests. Is there any way to find out which test checks which line of code?
Having used NCover, I know you can find out which lines have been tested and which have not. However, you really can't see which unit checked that code.
It can be really useful when dealing with tons of tests...
Solution 1:[1]
Hi as most people have responded the only way to do this at the moment with most coverage tools is to run each test by itself and then view the coverage for each test.
However it is possible, but would require a bit of effort, using OpenCover. If you captured and stored all the sequence points from the tests and the targets in the order they arrive and then analyse these results on a test by test basis, this is one of the original aims of OpenCover - https://github.com/sawilde/opencover/wiki. Currently OpenCover just aggregates these results and then throws the visitation data away but they could be stored. Of course you would have to be aware of any parallel running tests should they exist. The amount of data can be daunting and is on of the reasons it has not yet been implemented.
Solution 2:[2]
You can debug your testing. If you are using NUnit you can attach the process to your VS.
Solution 3:[3]
NCover is a tool you can use to calculate coverage of your unit tests. See NCover.com.
Solution 4:[4]
The easiest way is to download and install a personal edition of TestDriven.NET which runs your unit tests, and has an option to run and report the code coverage using NCover. Mind you newer NCover versions are commercial but the one included in TD.NET is a little bit older but free to use.
Solution 5:[5]
I don't know of anything that directly achieves matches what you are looking for. I recently did a project on automated fault localization where we needed this exact functionality, and the only solution we came up with was to roll our own test runner that also gathered coverage information for each method.
There may be an indirect way to achieve this using Visual Studio 2010 (Premium and Ultimate), which introduced a Test Impact Analyzer. This allows you to determine which tests are affected changes to your source code. However, this only works for MSTest. You can use the technique in this blog post to allow your NUnit tests to run under MSTest.
Solution 6:[6]
The term you are looking for is "Code coverage".
I don't have any experience how to do this with NUnit but there seems to be a codeplex project making this available for NUnit: http://codecoveragerunner.codeplex.com/
if you want to see which test is testing which line of code you could put a breakpoint on the line of code and debug your tests? When you hit your breakpoint check which test is
when having other tests throwing exceptions you can disable the break on exception option:
- in visual studio in the menu go to "Debug" -> "Exceptions" and uncheck the break on exceptions. then you dont have to stop at every exception.
Solution 7:[7]
The simple answer is, "run each test by itself and collect test coverage data for that test".
How you organize that may depend on the specific test coverage tool, and how you choose to run your tests.
For our Test Coverage tools, there is an explicit "TestCoverageDump" method added to your software by the test coverage instrumentation step. Normally a call to this method is inserted in the "main" program of your application so that when it exits, you get test coverage data for whatever tests you have run.
For your task, you want to modify your unit test running code to make an explicit call, after each test, on "TestCoverageDump" followed by "TestCoverageReset" (also inserted), so that each test gets its own vector. How you choose to associate the name of the test with the vector is completely under you control, at the price of a tiny bit of adjustment to the "TestCoverageDump" code supplied in source.
Our test coverage display tool can easily combine all the individual vectors to give you an overall view. Or you can view the coverage for any particular vector. The display tool will also let you compare coverage from different tests (where do they intersect? what does one test, that the other does not?)
Having this per-test coverage data also lets you determine which tests you need to run again. If you modify the code, and re-run the instrumenter, it will tell you which test coverage vectors (e.g., which tests) need to be run again based on what modified code that the vector previously covered.
Solution 8:[8]
If you are using Visual Studio, you should check out the extension "Run Coverlet Report" by Chris Dexter. https://marketplace.visualstudio.com/items?itemName=ChrisDexter.RunCoverletReport
Apart from the above comprehensive report, it can also do code highlighting where you can see which code lines are fully covered, partially covered and which are not.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shaun Wilde |
| Solution 2 | Peyman |
| Solution 3 | thekip |
| Solution 4 | Hadi Eskandari |
| Solution 5 | Ryan Gross |
| Solution 6 | |
| Solution 7 | |
| Solution 8 | Ε Г И І И О |


