Build/Test Issues

Tests with custom ExpectedException attribute are failing

Started by Vlad on 8,047 views

Hello,

We tried to use NCrunch on our project and found that all our tests that use custom expected exception attribute fail in NCrunch. They succeed in MSTest just fine.

Here is test code:

[TestMethod]
[ExpectedMissingInputException()]
public void ExecuteTaskThrowsIfInputIsNull()
{
    CallMyMethod(null);
}

...
void CallMyMethod(string str)
{
    if (str==null) throw ArgumentNullException( "str");
}

where ExpectedMissingInputException attribute is defined like this:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public sealed class ExpectedMissingInputExceptionAttribute : ExpectedExceptionBaseAttribute
{
  public ExpectedMissingInputExceptionAttribute() {}

  protected override void Verify(Exception exception)
  {
    if (!(exception is ArgumentNullException))
    {
      throw new ArgumentException("ArgumentNullException is expected, but actual exception was {0}", exception.GetType().FullName);
    }
  }
}


Is it NCrunch limitation or something should be configured in our NCrunch workspace?
Hi Vlad,

Thanks for posting!

NCrunch uses its own custom-built test runner to execute MSTest tests, and at the moment this test runner does not support custom ExpectedException attributes. Where working with tests that require this behaviour in NCrunch, I recommend introducing the exception filter logic in the test itself, perhaps delegating it to a reusable block of code if you have many tests that do this.

Cheers,

Remco
Remco wrote:NCrunch uses its own custom-built test runner to execute MSTest tests, and at the moment this test runner does not support custom ExpectedException attributes. Where working with tests that require this behaviour in NCrunch, I recommend introducing the exception filter logic in the test itself, perhaps delegating it to a reusable block of code if you have many tests that do this.

We have a lot of tests like this (hindreds+) and changing all of them will be hard to justify. Using custom attribute makes test code cleaner and more readable, at least our team prefer it like this.
Do you know if there are plans to start supporting custom ExpectedException in the nearest releases of NCrunch?
Sorry, but right now it doesn't look likely that support for custom ExpectedException attributes will be added in the near future. Very few users make use of these, and the effort required to reliably implement them is considerable (they are very tightly integrated in the MSTest framework). I wish I could give you a better solution, but I'm afraid I can't.
Has this issue ever been addressed?
Sorry, it does not look custom MSTest ExpectedException attributes will ever be supported by NCrunch.

Post a reply

Log in to reply.