Unit test exception messages with xUnit

sduplooy picture sduplooy · Mar 21, 2011 · Viewed 16.5k times · Source

I'm currently converting my MsTest unit tests to xUnit. With xUnit, is there a way to test exception messages? Is it correct to test exception messages as opposed just the exception type?

Answer

the_joric picture the_joric · May 18, 2011

I think it is correct to test for both Exception type and message. And both are easy in xUnit:

var exception = Assert.Throws<AuthenticationException>(() => DoSomething());
Assert.Equal(message, exception.Message);