在NUnit中,如何明确地使测试失败

dom*_*lao 7 nunit

例如下面的代码,

[Test()]
public void Test( )
{
   try{
      GetNumber( );
   }
   catch( Exception ex ){
       /* fail here */
   }

   ...
}
Run Code Online (Sandbox Code Playgroud)

当GetNumber方法抛出异常时,我想失败我的测试.

请指教.

非常感谢.

ale*_*exn 23

您不需要在try/catch中包装GetNumber().如果GetNumber()抛出,您的测试将失败.

如果需要显式失败,请使用Assert.Fail();

  • 在这种特殊情况下,你应该使用`Assert.DoesNotThrow(()=> GetNumber())` (2认同)