Use NUnit Assert.Throws method or ExpectedException attribute?

后端 未结 5 1262
温柔的废话
温柔的废话 2020-12-04 13:57

I have discovered that these seem to be the two main ways of testing for exceptions:

Assert.Throws(()=>MethodThatThrows());

[ExpectedExc         


        
5条回答
  •  时光说笑
    2020-12-04 14:22

    The main difference is:

    ExpectedException() attribute makes test passed if exception occurs in any place in the test method.
    The usage of Assert.Throws() allows to specify exact place of the code where exception is expected.

    NUnit 3.0 drops official support for ExpectedException altogether.

    So, I definitely prefer to use Assert.Throws() method rather than ExpectedException() attribute.

提交回复
热议问题