Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Code Coverage % Metrics
qujck
#1 Posted : Saturday, April 19, 2014 10:32:24 PM(UTC)
Rank: Member

Groups: Registered
Joined: 4/19/2014(UTC)
Posts: 17
Location: United Kingdom

Thanks: 9 times
Was thanked: 2 time(s) in 2 post(s)
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.

Code:

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
Code:
get { throw new NotImplementedException(); }
is not supposed to be called and should be discounted?
Remco
#2 Posted : Saturday, April 19, 2014 11:40:45 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Hi, thanks for posting!

The best way to do this is to make use of code coverage suppression comments.

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.
qujck
#3 Posted : Sunday, April 20, 2014 12:05:00 AM(UTC)
Rank: Member

Groups: Registered
Joined: 4/19/2014(UTC)
Posts: 17
Location: United Kingdom

Thanks: 9 times
Was thanked: 2 time(s) in 2 post(s)
Thanks @Remco

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.
Remco
#4 Posted : Sunday, April 20, 2014 12:07:12 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
NCrunch will also respect the [ExcludeFromCodeCoverage] attribute, if you'd prefer to use this - http://msdn.microsoft.com/en-us/library/system.diagnostics.codeanalysis.excludefromcodecoverageattribute.aspx.

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.
qujck
#5 Posted : Sunday, April 20, 2014 12:18:16 AM(UTC)
Rank: Member

Groups: Registered
Joined: 4/19/2014(UTC)
Posts: 17
Location: United Kingdom

Thanks: 9 times
Was thanked: 2 time(s) in 2 post(s)
Cool. That's not working in this example

Code:
[ExcludeFromCodeCoverageAttribute]
public DateTime SystemDate
{
    get { throw new NotImplementedException(); }
}
Remco
#6 Posted : Sunday, April 20, 2014 12:21:44 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
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?
qujck
#7 Posted : Sunday, April 20, 2014 12:30:05 AM(UTC)
Rank: Member

Groups: Registered
Joined: 4/19/2014(UTC)
Posts: 17
Location: United Kingdom

Thanks: 9 times
Was thanked: 2 time(s) in 2 post(s)
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.
Remco
#8 Posted : Sunday, April 20, 2014 12:32:06 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Ok - I understand.

I'm sorry, but in this case, code coverage suppression comments and Metrics Windows metrics exclusions are the only solutions I can suggest.
1 user thanked Remco for this useful post.
qujck on 4/20/2014(UTC)
qujck
#9 Posted : Sunday, April 20, 2014 2:46:09 PM(UTC)
Rank: Member

Groups: Registered
Joined: 4/19/2014(UTC)
Posts: 17
Location: United Kingdom

Thanks: 9 times
Was thanked: 2 time(s) in 2 post(s)
I've marked the entire Mock class with `[ExcludeFromCodeCoverageAttribute]` and this gives me what I need.

Fine product by the way - very impressive.

Cheers
1 user thanked qujck for this useful post.
Remco on 4/21/2014(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.052 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download