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

Notification

Icon
Error

"No Coverage" comments not working
worlebird
#1 Posted : Tuesday, December 13, 2016 12:17:11 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/13/2016(UTC)
Posts: 5
Location: United States of America

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
In a test class, I have a common piece of code that I use for setting up an object before multiple tests.
For some reason, NCrunch is marking this code (in a test class) with a black dot as "Not covered by any tests".
First off, this is nonsense, as I have at least a half dozen tests which call this method right at the beginning of the test.
However, this is kind of irrelevant, since this is code in my test class, and I'm not writing tests to test my tests.

I figured I could take care of this problem by putting comments around the method : "//ncrunch: no coverage start" and "//ncrunch: no coverage end", but no dice. The method is still marked as not covered.
Stopping and restarting the NCrunch engine makes no difference, and restarting Visual Studio also makes no difference.

What's going on here, and how can I fix it?
Also as side note, is there a good way to get NCrunch to just ignore my test projects altogether when calculating code coverage?
I would have thought that would be the default behavior, but I guess not.
Remco
#2 Posted : Tuesday, December 13, 2016 2:53:57 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,976

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Hi, thanks for posting!

There's a few issues you've covered here. I'll try to address them each individually.

Missing coverage:

When NCrunch tracks test coverage, it does so from the moment the test itself starts executing and it will only track the lines of code that are physically executed. It's worth being aware of the difference between 'physical' code coverage and 'logical' code coverage. The documentation has a good explanation of this here - http://www.ncrunch.net/documentation/troubleshooting_missing-code-coverage.

If NCrunch isn't tracking the coverage due to a problem in its index, you might want to try closing down VS and deleting the NCrunch cache file (stored under _NCrunch_SolutionName next to your solution file). This will reset NCrunch's knowledge of the testing state of your solution.


Coverage comments not working:

The coverage comment system works off a fairly simple mechanism that prevents NCrunch from instrumenting lines of code between the markers. It's not clear to me why this would fail for you. Are you able to provide any sample code that demonstrates this problem?


Excluding test projects from metrics:

There is an option on the Metrics Window toolbar to exclude all test projects from the metrics. You can also exclude them manually using a different option on this toolbar.
worlebird
#3 Posted : Tuesday, December 13, 2016 5:15:15 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/13/2016(UTC)
Posts: 5
Location: United States of America

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
Remco;9527 wrote:
It's worth being aware of the difference between 'physical' code coverage and 'logical' code coverage. The documentation has a good explanation of this here - http://www.ncrunch.net/documentation/troubleshooting_missing-code-coverage.


I did run across that document yesterday, and thought that I understood what it was saying.
Based on my experience here, perhaps I do not.

Remco;9527 wrote:
If NCrunch isn't tracking the coverage due to a problem in its index, you might want to try closing down VS and deleting the NCrunch cache file (stored under _NCrunch_SolutionName next to your solution file). This will reset NCrunch's knowledge of the testing state of your solution.


I have just tried this, to no effect.

Remco;9527 wrote:
The coverage comment system works off a fairly simple mechanism that prevents NCrunch from instrumenting lines of code between the markers. It's not clear to me why this would fail for you. Are you able to provide any sample code that demonstrates this problem?


Sure. Please take a look at the segment of code below. (Note that I am using xUnit for our testing framework here, hence the "[Fact]" decorators.)
This is the snippet of my test code that I am asking about - specifically, the CreateTradingPartnerService() method is being marked by NCrunch with a black dot and the message "No tests are covering this line."
This, despite the fact that #1, this code is surrounded by the appropriate comments, which should exclude it from coverage analysis, and #2 it is being called from both of the tests immediately below, as well as several more similarly structured tests I have not included.

Code:

	//ncrunch: no coverage start
	private TradingPartnerService CreateTradingPartnerService() {
		BCILibrary.Common.Utility.ILogger logger = new BCILibrary.Common.Utility.StubLogger();
		return new TradingPartnerService(
			new StubBusinessLogic(),
			logger,
			new SOAPExceptionHandler(logger)
		);
	}
	//ncrunch: no coverage end

	[Fact]
	public void nullMessage() {
		//Arrange
		TradingPartnerService wcf = CreateTradingPartnerService();
		//Act + Assert
		Assert.Throws<FaultException<RequestFault>>(() => wcf.GetPartnerData(null));
	}

	[Fact]
	public void populatedMessage() {
		//Arrange
		TradingPartnerService wcf = CreateTradingPartnerService();
		//Act
		PartnerDataResponse response = wcf.GetPartnerData(new GetPartnerDataRequest());
		//Assert
		Assert.NotNull(response.PartnerData);
	}

...


Remco;9527 wrote:
There is an option on the Metrics Window toolbar to exclude all test projects from the metrics. You can also exclude them manually using a different option on this toolbar.


In the Metrics window, the test projects are being excluded appropriately, and percentages are being calculated correctly. Indeed, in the Metrics window, everything is correct even if I remove the "no coverage" comments. The only issue appears in the code itself, where NCrunch flags particular lines with a colored dot to indicate testing status. This inline indicator does not appear to respect the comments, nor does it recognize that the code is covered.
Remco
#4 Posted : Tuesday, December 13, 2016 10:49:11 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,976

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Thanks for sharing the code sample.

What you've described is definitely defective behaviour. You've used the coverage suppression comments correctly and there is no structure to this code that would cause ambiguity around whether the shared method is covered. Your clearing of the coverage index rules out any kind of intermittent indexing problems.

I'm wondering if I can get some more information about what is happening in this situation:

- Would you mind submitting a bug report using the NCrunch menu option? Make sure all your tests have run during the session in which you submit the report.
- Are you able to share a screenshot of the incorrect coverage results as they exist in the code window?
- Can you share any details on how the 'uncovered' lines respond to changes made to your code? For example, what happens if you change the name of the method, or add new lines to it? If you cut/paste the method to a different source file or different line, what happens?
- Can you confirm the exact message displayed by the line when you hover your cursor over it? NCrunch markers that show no coverage should say 'No tests are covering this line\r\n(Left click to show tests, Right-click for options)'
worlebird
#5 Posted : Wednesday, December 14, 2016 12:06:00 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/13/2016(UTC)
Posts: 5
Location: United States of America

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
Remco;9535 wrote:
- Would you mind submitting a bug report using the NCrunch menu option? Make sure all your tests have run during the session in which you submit the report.


I'll do that.

Remco;9535 wrote:
- Are you able to share a screenshot of the incorrect coverage results as they exist in the code window?


I've tried, but I can't see a way to attach a screenshot to this post. I also attempted to upload the screenshot to the "My Albums" page.
I first got an error that my image was too large (about 450K?), and then after cropping it down, upload attempts resulted in "cannot find image..." errors, with an odd path to C:\Inetpub\forum...

I can't upload to a common image sharing or file sharing site either, as those are blocked by company policy.

I may be stuck on this one, but I can describe it to you.

If you look at the code snippet I shared in my previous post, the line "private TradingPartnerService CreateTradingPartnerService() {" has a black dot next to it ("No tests are covering this line", and the lines "public void nullMessage() {" and "public void populatedMessage() {" each have green carets next to them ("Test starts on this line").

Remco;9535 wrote:
- Can you share any details on how the 'uncovered' lines respond to changes made to your code? For example, what happens if you change the name of the method, or add new lines to it? If you cut/paste the method to a different source file or different line, what happens?


I tried a few things - changing the name of the method, allowing intellisense to highlight areas that were calling it, and then changing it back.
No reaction from NCrunch on that one.
I changed the method from private to public, but again, no reaction from NCrunch.
I deleted the method altogether, and the black dot went away. Then I pasted it back in, and NCrunch worked for a few seconds (presumably re-compiling and running the tests), after which the black dot came back.

Remco;9535 wrote:
- Can you confirm the exact message displayed by the line when you hover your cursor over it? NCrunch markers that show no coverage should say 'No tests are covering this line\r\n(Left click to show tests, Right-click for options)'


Yes, that is exactly the tooltip message I get when I hover over the black dot.

I'll note that in the rest of my code (outside of this test module), NCrunch is correctly identifying code that is and is not covered by tests.
It just seems to be getting confused by this method that is being called within the test code.
Remco
#6 Posted : Wednesday, December 14, 2016 12:13:54 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,976

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Sorry about the screenshot upload. I think we should be OK without it. I mostly just wanted to confirm that these were in fact NCrunch markers. The tooltip you've given me has confirmed that.

I've never seen a problem like this before. Something very interesting is happening with this method. To avoid me asking a never-ending list of questions about this, I'm wondering if there's any way you can produce a working sample of this that you can send to me via the NCrunch contact form. I think that the PDB/IL structure of this method is somehow different. With a code sample reproducing the problem, I should be able to get to the bottom of it.
worlebird
#7 Posted : Wednesday, December 14, 2016 6:29:08 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/13/2016(UTC)
Posts: 5
Location: United States of America

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
Remco;9538 wrote:
To avoid me asking a never-ending list of questions about this, I'm wondering if there's any way you can produce a working sample of this that you can send to me via the NCrunch contact form. I think that the PDB/IL structure of this method is somehow different. With a code sample reproducing the problem, I should be able to get to the bottom of it.


AH HA!

So in the process of trying to create a code sample that would reproduce this issue, I finally figured out what is causing it.
It's still a bug, but at least I know what the problem is.

I created a little HelloWorld console app with a test project that should have reproduced the problem, but was unable to. The code looked exactly the same, but NCrunch was working just fine. So, I started adding everything I could think of that my previous code was using, trying to get the error to happen again.
This finally led me to the thing that caused the issue. Since I nailed it down, I will just describe the steps to reproduce it below, rather than submitting a code sample as you requested. Now that I know, it's pretty easy to reproduce.
Oh, also note, I did submit a bug through the menu option in Visual Studio on this already as well. The bug references this forum thread, if that helps you to find it.
Fair warning: Skip to the last paragraph for the TL;DR; summary - I get a bit long-winded here describing the source of the issue. *chuckle*

We just purchased NCrunch recently. Prior to that, we were just using Visual Studio's built-in test runner platform to run our tests, and the built-in code analysis functionality to determine test coverage (System.Diagnostics.CodeAnalysis). That package will recognize and ignore test using Microsoft's testing libraries, but it doesn't know to ignore xUnit tests (which is odd, since it recognizes them as tests for code coverage purposes *sigh*).
In any case, we were having a similar issue with our xUnit tests at that time - namely, the test methods were showing up as not being covered by tests, which brought our code coverage percentage down (slightly different from NCrunch, which reported the coverage correctly, but marked the code inline as not being covered).
In any case, the solution to this problem is similar to the way NCrunch uses the "no coverage" comment markers. Instead, you can use a decorator on your code (by method or by class) to tell the analysis engine to ignore it: "[ExcludeFromCodeCoverage]". Because we had been using MS's built-in functionality, these decorators were still on all of my test classes.

TL;DR; - The problem seems to be caused by a conflict between NCrunch and the [ExcludeFromCodeCoverage] decorator in the System.Diagnostics.CodeAnalysis library.
To reproduce the issue, just create a test project in Visual Studio, add a simple test that calls a set-up method that is not decorated as an independent test, and enable NCrunch. (Note that while we are using xUnit, I tried it with the Microsoft.VisualStudio.TestTools.UnitTesting framework, and it had the same effect; so xUnit is not related to this issue at all.) You will see green in-line dots and caret as you would expect, indicating that this is a test and it is fully covered.
Now, add a using clause for System.Diagnostics.CodeAnalysis and put the [ExcludeFromCodeCoverage] decorator above your test class. You will see the NCrunch dots change.

Here is a code sample:

Code:

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject2 {

	[ExcludeFromCodeCoverage]
	[TestClass]
	public class UnitTest1 {

		private void DoSomething() {

		}

		[TestMethod]
		public void TestMethod1() {
			DoSomething();
		}

	}
}


I think that covers it. Now I know how to avoid the problem on my side, and hopefully you have enough information on your side to address the issue in NCrunch (or at least document it).
Please let me know if you need any additional information from me.

Thanks so much for your help!
Remco
#8 Posted : Wednesday, December 14, 2016 11:12:50 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,976

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Wow, I just got it to happen for me. This is pretty crazy.

I'll need to take a deeper look at what's happening here, but I think this is being caused by some fallback code used to mark the first lines of test methods to identify test entry points. In which case, it should just be a visual anomaly.

Thanks for all your help in digging into this issue. I'll see what I can do about getting it fixed :)
1 user thanked Remco for this useful post.
worlebird on 12/15/2016(UTC)
worlebird
#9 Posted : Thursday, December 15, 2016 1:38:24 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/13/2016(UTC)
Posts: 5
Location: United States of America

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
Remco;9544 wrote:
Wow, I just got it to happen for me. This is pretty crazy.

I'll need to take a deeper look at what's happening here, but I think this is being caused by some fallback code used to mark the first lines of test methods to identify test entry points. In which case, it should just be a visual anomaly.

Thanks for all your help in digging into this issue. I'll see what I can do about getting it fixed :)


No problem, and thanks for your help as well!
I really appreciate your quick responses.

I'm guessing the issue hasn't come up before because if you're using NCrunch, then it's unlikely you would also be using Visual Studio's test coverage analysis as well.
In my case, it was a holdover from before we got NCrunch, so it was easy enough to just remove.

Anyway, thanks again!
1 user thanked worlebird for this useful post.
Remco on 12/15/2016(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.089 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download