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!