NUnit expected exceptions

52,959

Solution 1

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.

Solution 2

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 );
}

Solution 3

By default, TeamCity uses NUnit 2.2.10, which doesn't have ExpectedException. Check the TeamCity "NUnit for NAnt" docs to see how to change it to something more modern, including the specific list of releases TeamCity provides.

Solution 4

NUnit has added a new Record.Exception method.

If you prefer to separate Acts and Asserts then

Act:

ex = Record.Exception(()={throw new Exception()} 

Assert:

Assert.NotNull(ex);
Share:
52,959
Martin
Author by

Martin

Updated on April 11, 2020

Comments

  • Martin
    Martin about 4 years

    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?