I always choose to mock my own abstractions and I've noticed that the parts of the abstraction that I do not need in a particular unit test are affecting the Code Coverage metrics. E.g.
private class MockDateProvider : IDateProvider
{
private readonly DateTime businessDate;
public MockDateProvider(DateTime businessDate)
{
this.businessDate = businessDate;
}
public DateTime BusinessDate
{
get { return this.businessDate; }
}
public DateTime SystemDate
{
get { throw new NotImplementedException(); }
}
}
I know the fact I can ask this question means my class may be violating SRP, but is there some way to let NCrunch know that for the purposes of this set of tests the line of code
get { throw new NotImplementedException(); }
is not supposed to be called and should be discounted?
If the the class resides within a test project, it may also be worthwhile to simply exclude the whole project from the metrics calculation, as its component of the code coverage % calculation is probably not very relevant. This will allow you to keep the inline code coverage, which is useful for other purposes.
2 things
- the code coverage suppression option is not transportable - i.e. it only works for NCrunch so it's basically a hard coded solution (being ignored is not the same as being flexible). And starting a line of code with // is not automatically reusable.
- I do not wish to exclude the test project to satisfy NCrunch - it has 253 passing unit tests so #fail to your answer.
By excluding the project, I don't mean ignoring it entirely. Inside the NCrunch Metrics Window, there is the option to 'exclude' a project from metrics calculation. The code coverage will still exist and the tests will still run, but it will not contribute to the code coverage percentage shown in the Metrics Window. I suggested this as I am assuming your reason for wanting to 'turn off' this code coverage is because you are concerned about its impact on your overall code coverage percentage.
No, you're right .. probably it wouldn't in the case of structures that are compiler generated (i.e. lambda, get/set properties). Other coverage tools also share this problem.
Do you mind if I ask what you are trying to achieve by excluding this line from coverage?
In that example I get 98.98% coverage - but my tests cover 100%. So I want NCrunch to say 100%. That's all. And this is just an example - my method of manually mocking is the root cause of these unnecessary problems.