I've got a set of test cases, some of which are expected to throw exceptions. Because of this, I have have set the attributes for these tests to expect exceptions like so:
[ExpectedException("System.NullReferenceException")]
When I run my tests locally all is good. However when I move my tests over to the CI server running TeamCity, all my tests that have expected exceptions fail. This is a known bug.
I am aware that there is also the Assert.Throws<>
and Assert.Throws
methods that NUnit offers.
My question is how can I make use of these instead of the attribute I'm currently using?
I've had a look around StackOverflow and tried a few things none of which seem to work for me.
Is there a simple 1 line solution to using this?
Assert. Throws "attempts to invoke a code snippet, represented as a delegate, in order to verify that it throws a particular exception." The () => DoSomething() syntax represents a lambda, essentially an anonymous method. So in this case, we are telling Assert. Throws to execute the snippet o.
Handling the Exception in Code If the processing required for an exception is too complex to express in the attribute declaration, the normal practice is to process it in the test code using a try/catch block. As an alternative, NUnit 2.4 allows designating a method that will be called to process the exception.
Assert. Throws returns the exception that's thrown which lets you assert on the exception. var ex = Assert.
Assertions are central to unit testing in any of the xUnit frameworks, and NUnit is no exception. NUnit provides a rich set of assertions as static methods of the Assert class. If an assertion fails, the method call does not return and an error is reported.
I'm not sure what you've tried that is giving you trouble, but you can simply pass in a lambda as the first argument to Assert.Throws. Here's one from one of my tests that passes:
Assert.Throws<ArgumentException>(() => pointStore.Store(new[] { firstPoint }));
Okay, that example may have been a little verbose. Suppose I had a test
[Test] [ExpectedException("System.NullReferenceException")] public void TestFoo() { MyObject o = null; o.Foo(); }
which would pass normally because o.Foo()
would raise a null reference exception.
You then would drop the ExpectedException
attribute and wrap your call to o.Foo()
in an Assert.Throws
.
[Test] public void TestFoo() { MyObject o = null; Assert.Throws<NullReferenceException>(() => o.Foo()); }
Assert.Throws
"attempts to invoke a code snippet, represented as a delegate, in order to verify that it throws a particular exception." The () => DoSomething()
syntax represents a lambda, essentially an anonymous method. So in this case, we are telling Assert.Throws
to execute the snippet o.Foo()
.
So no, you don't just add a single line like you do an attribute; you need to explicitly wrap the section of your test that will throw the exception, in a call to Assert.Throws
. You don't necessarily have to use a lambda, but that's often the most convenient.
Here's a simple example using both ways.
string test = null; Assert.Throws( typeof( NullReferenceException ), () => test.Substring( 0, 4 ) ); Assert.Throws<NullReferenceException>( () => test.Substring( 0, 4 ) );
If you don't want to use lambdas.
[Test] public void Test() { Assert.Throws<NullReferenceException>( _TestBody ); } private void _TestBody() { string test = null; test.Substring( 0, 4 ); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With