Build/Test Issues

Bug with combination of catch-when and params arguments

Started by jarlelin on 6,213 views

I was getting the "System.InvalidProgramException : Common Language Runtime detected an invalid program" in some of my tests when running ncrunch, but I don't get that problem when using other test runners.

Disabling "Instrument Output Assembly" works and the tests run, but I lose my code coverage visibility and exception inspection via ncrunch pills.

I could not find any information on this issue, but I have narrowed the problem down to a comination of the catch() when() syntax and using the params keyword (if you pass more than 1 item to the params array). This is particularly a problem because we often want to log something in our catches and the logger often uses params for templates.


//THIS TEST PASSES
[Test]
public void NoCatchWhen()
{
try
{
throw new ApplicationException("1");
}
catch (ApplicationException e) //when (e.Message == "1")
{
Console.WriteLine("Testing problem with {0} in {1}", "clr", "ncrunch");
}
}

//THIS TEST PASSES
[Test]
public void CatchWenAndNoLoggingOfParamArgs()
{
try
{
throw new ApplicationException("1");
}
catch (ApplicationException e) when (e.Message == "1")
{
Console.WriteLine("logstatement");
}
}

//THIS TEST PASSES
[Test]
public void WithCatchWhenAndLoggingOfASingleParamArgument()
{
try
{
throw new ApplicationException("1");
}
catch (ApplicationException e) when (e.Message == "1")
{
Console.WriteLine("Testing problem with {0}", "ncrunch-clr");
}
}

//THIS TEST FAILS
[Test]
public void WithCatchWhenAndLoggingOfAMultipleParamArgument()
{
try
{
throw new ApplicationException("1");
}
catch (ApplicationException e) when (e.Message == "1")
{
//attempting to pass 2 or more arguments into the params array in any method results in the whole method returning CLR-error
Console.WriteLine("Testing problem with {0} in {1} because {2}", "clr", "ncrunch", "resons");
}
}



Is there a better fix to this than disabling ncrunch instrumentation?
I'm running nCrunch 2.23.0.2 with Visual Studio 2015 Update 3 and using NUnit for my testing needs.
Hi,

Thanks for sharing this issue. I'm fairly certain this is being caused by an IL sequence that is being affected by NCrunch's instrumentation. You can work around the problem by placing code coverage suppression comments around the code block, for example:

//THIS TEST FAILS
[Test]
public void WithCatchWhenAndLoggingOfAMultipleParamArgument()
{
try
{
throw new ApplicationException("1");
}//ncrunch: no coverage start
catch (ApplicationException e) when (e.Message == "1")
{
//attempting to pass 2 or more arguments into the params array in any method results in the whole method returning CLR-error
Console.WriteLine("Testing problem with {0} in {1} because {2}", "clr", "ncrunch", "resons");
}//ncrunch: no coverage end
}

I've been having trouble reproducing this problem myself. I'm wondering if you can share some more details about your environment. Which version of .NET are you running under? Which O/S are you on?
Using ncrunch-specific code suppression comments is not going to cut it I'm afraid.

The projects are running .NET 4.6.1, but I tried a couple more just to check.
I'm running windows 10 (Ent). I'm afraid I won't be able to easily test this on other OSes.
I tried to reproduce it in a new solution and couldn't. Not until I turned ON "Instrunment assembly outbut", but OFF "Analyse line execution times".

So with those settings changed I managed to reproduce the failure of the last test in my example in a completely fresh solution on windows 10 under .net 4.6.1.
Ok, that did the trick. I can reproduce this problem now. Thanks for letting me know about it. I'll see what I can do about getting it fixed. The following options can be used as workarounds:

- Code coverage suppression comments around the 'when'
- Turning off instrumentation
- Turning on Analyse line execution times
I also ran into this issue but was only able to test my project by turning off instrumentation. I would really like to use the coverage feature to help debug test failures in this code.
A fix for this problem is being released today with V3.
Awesome. Have a gif: http://i.imgur.com/fcI43vR.gifv !

Post a reply

Log in to reply.