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

Notification

Icon
Error

How to get 100% code coverage with a throw inside cath?
IlyaC
#1 Posted : Tuesday, April 21, 2020 10:09:31 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 2/21/2019(UTC)
Posts: 7
Location: Norway

I have a code like this:

Code:
try
{
   DoSomething();
}
catch
{
   LogSomething();
   throw;
}


Now when Ncrunch calculates metrics for code coverage, the last line is always "not covered". I assume it is not called in a way, but it's frustrating to not get 100% coverage because of that.
I know I can do comments, but I'd like not to have custom ncrunch comments in my code.

Is there any workaround for this issue?
Remco
#2 Posted : Tuesday, April 21, 2020 10:30:02 AM(UTC)
Rank: NCrunch Developer

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

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
Hi, thanks for posting.

Structurally, what you are attempting to do is impossible without code coverage suppression (i.e. ncrunch comments, ExcludeFromCoverageAttribute, etc).

This is because a throw statement physically causes the runtime to return early from a method, leaving any code after it to be left unexecuted. As methods usually have debug information for NOP statements and other material at the end of the method, this results in code that is physically unexecuted by NCrunch.

It could technically be possible for NCrunch to ignore the unexecuted code and simply mark it as 'not code', thereby removing the black marks and allowing you to achieve 100% coverage with such a structure without the need for suppression comments. However, we do not do this for the following reasons:

1. It's a dirty lie. There is unexecuted code in your codebase. You may not consider this code important, and probably it's completely unreachable. But it's still unexecuted code, and that's still information. For NCrunch to cover this up would be for it to hide this information from you. This goes against the main goal of this project which is to inform people of how their code is behaving at runtime.

2. It's very hard for us to reliably determine which code in the method is unreachable when we instrument your code. We support 3 different compilers under NCrunch, and they all have their own weird patterns and edge cases. Any approach we take to do this would run the risk of unexpected behaviour under unforeseen edge cases. Given the sheer volume of code NCrunch executes for everyone, someone is bound to encounter something we haven't thought of and end up with a very strange result.

3. The instrumentation is a performance critical step. For us to determine whether the code is reachable would require additional parsing and complex processing of every method in your codebase. This would significantly impact build speeds for everyone using the product, regardless of whether they were interested in having this behaviour. We could do it with a config setting, but then there's a whole new setting when we already have far too many. I think also that most people would be far less interested in such an option if it made their builds run 40% slower.

As such, code coverage suppression comments would be the best solution I can offer given your situation.
IlyaC
#3 Posted : Tuesday, April 21, 2020 10:36:16 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 2/21/2019(UTC)
Posts: 7
Location: Norway

Thanks for the answer,

I understand it might be challenging since it is unreachable code in reality, though it's of course expected to be unreachable by design.

Interestingly enough if I add one more catch before a generic one that does this:
Code:

catch when ()...
{  
.....
   throw new Exception(e);
} // This line does not show in coverage
catch
{
}


The last line of first code is treated as not a code line by Ncrunch, there is no circle to the left. I don't know why, but this seems like an ideal solution, though I see it might not be easy to implement.
I guess I'll have to live with 99% coverage then :)
Remco
#4 Posted : Tuesday, April 21, 2020 10:39:05 AM(UTC)
Rank: NCrunch Developer

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

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
IlyaC;14646 wrote:

The last line of first code is treated as not a code line by Ncrunch, there is no circle to the left. I don't know why, but this seems like an ideal solution, though I see it might not be easy to implement.
I guess I'll have to live with 99% coverage then :)


This is interesting. Probably it's something to do with the IL patterns being kicked out of the compiler. You might find it is also different between versions of C# (we've seen a lot of that sort of thing).

I'm glad you found an approach that works for you.
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.064 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download