(5.6) NUnit's ExpectedException Attribute is gone?
I just upgraded to Unity 5.6, and to my surprise a ton of my unit tests no longer compiles. It looks as if the ExpectedException attribute can no longer be found.
error CS0246: The type or namespace name `ExpectedException' could not be found. Are you missing an assembly reference?
This is not only affecting my project, but some of my (more well tested) purchased plugins as well. I've gone through the change log for 5.6, but I can't find any changes whatsoever to NUnit or the test runner.
Whats going on?
Answer by Nerull22 · Mar 31, 2017 at 11:42 PM
In Unity 5.6 they updated to NUnit 3. And in NUnit 3, they decided to remove this attribute. They claim th at instead of having an attribute that expects an exception, that it should be a separately written unit test. I don't know if I necessarily agree with the decision, but this is what you're seeing.
You're right, they have updated to NUnit 3. Where did you get that information by the way? I can't remember reading it anywhere, and it would have been useful to know.
The solution is using the Throws functionality ins$$anonymous$$d.
Assert.That(()=> new SomethingThatCantHandleNull(null), Throws.ArgumentNullException);
Thank you!
You can find more information about the NUnit breaking changes here: https://github.com/nunit/docs/wiki/Upgrading
Answer by rus89 · Sep 22, 2017 at 11:41 AM
@LavaPatrik Replace whole method and all it's attributes with:
[Test]
public void ExpectedExceptionTest()
{
Assert.Throws<ArgumentException>(delegate()
{
throw new ArgumentException ("expected message");
});
}
Answer by samsmithnz · Jul 06, 2017 at 01:55 PM
The issue here, is that if you install the Unity Test Tools in 5.6, you are effectively installing an error, as this code is added by default:
[Test]
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "expected message")]
public void ExpectedExceptionTest()
{
throw new ArgumentException("expected message");
}
Answer by Paul_Bronowski · May 13, 2017 at 04:24 AM
Yes, they moved it... https://github.com/nunit/nunit/issues/113
To here... https://github.com/nunit/nunit-csharp-samples/tree/master/ExpectedExceptionExample
Your answer
![](https://koobas.hobune.stream/wayback/20220612114405im_/https://answers.unity.com/themes/thub/images/avi.jpg)