TestCase and ExpectedException in nunit v3.

1,821 views
Skip to first unread message

constructor

unread,
Aug 11, 2015, 9:23:53 AM8/11/15
to NUnit-Discuss
Hi,

Please, could you help me to understand if next sample can be implemented in nunit v3?

        [Test]
        [TestCase(1, ExpectedException = typeof(Exception), ExpectedMessage = "bad parameter 1")]
        [TestCase(2, ExpectedException = typeof(Exception), ExpectedMessage = "bad parameter 2")]
        [TestCase(3)]
        public void TestSomething(int mode)
        {
           ...
        }


thank you,
Igor.

Rob Prouse

unread,
Aug 11, 2015, 9:35:14 AM8/11/15
to nunit-...@googlegroups.com
Ideally, I would break it into two tests, one for passing test cases and one for failing test cases. That also has the advantage of making the tests clearer. If it results in too much code duplication, you could always call a third private method.

Once you do that, make the exception and message parameters on the test.



        [TestCase(1, ExpectedException = typeof(Exception), ExpectedMessage = "bad parameter 1")]
        [TestCase(2, ExpectedException = typeof(Exception), ExpectedMessage = "bad parameter 2")]
        public void TestSomethingThatFails(int mode, Type expectedException, string expectedMessage)
        { 
           Assert.That(() => TestSomething(), Throws.Exception // ...
        }


        [TestCase(3)]
        public void TestSomethingThatPasses(int mode)
        {
           ...
        }
 

--
You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nunit-discus...@googlegroups.com.
To post to this group, send email to nunit-...@googlegroups.com.
Visit this group at http://groups.google.com/group/nunit-discuss.
For more options, visit https://groups.google.com/d/optout.



--

Rob Prouse

 

I welcome VSRE emails. Learn more at http://vsre.info/

Charlie Poole

unread,
Aug 11, 2015, 10:28:27 AM8/11/15
to NUnit-Discuss
Yes, I generally like to separate happy path tests from those that
throw exceptions.

However, he is asking about NUnit 3.0, which doesn't support
ExpectedException, so the ExpectedException and ExpectedMessage named
properties have to be removed.

Rob Prouse

unread,
Aug 11, 2015, 10:30:04 AM8/11/15
to nunit-...@googlegroups.com
At the end of my message, I suggested passing the exception and message into the test to use in an Assert.That( Throws ).

Rob Prouse

unread,
Aug 11, 2015, 10:31:21 AM8/11/15
to nunit-...@googlegroups.com
Opps, I forgot to adjust the TestCase when I copied and pasted his code :(


        [TestCase(1, typeof(Exception), "bad parameter 1")]
        [TestCase(2, typeof(Exception), "bad parameter 2")]

constructor

unread,
Aug 11, 2015, 10:45:06 AM8/11/15
to NUnit-Discuss

Does construction "Throws.Exception" support exception as type variable (expectedException)?

Charlie Poole

unread,
Aug 11, 2015, 11:40:58 AM8/11/15
to NUnit-Discuss
You can use Thows.[Exception.]TypeOf(type).[With.]Message.EqualTo(message);

I hope that compiles. :-)

Alternatively...

var ex = Assert.Throws(type);
Assert.That(ex.Message, Is.EqualTo(message));

Same hope as above. :-)

Which you use is a matter of style.

Charlie
Reply all
Reply to author
Forward
0 new messages