Search code examples
unit-testingmstestxunit

Unit test exception messages with xUnit


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?


Solution

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