有没有办法用ExpectedException属性提供失败消息?

时间:2011-08-03 10:56:11

标签: c# unit-testing nunit expected-exception

我想在断言失败时提供一条消息,但我想不出办法。

[Test]
public void CreateInstanceTest()
{
    SomeClass someClass = new SomeClass();
    Assert.IsNotNull(someClass, "Constructor could not return an instance");
    //provide failure message here
}

我如何在这个测试中做到这一点?

[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void EmptyNameInConstructorThrowsExceptionTest()
{
    SomeClass someClass = new SomeClass(null);
}

1 个答案:

答案 0 :(得分:2)

[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void EmptyNameInConstructorThrowsExceptionTest()
{
    SomeClass someClass = new SomeClass(null);
    Assert.Fail("Exception not thrown");
}