我是 NUNIT 的新手,我遇到了以下奇怪的行为。
在我的测试项目中,我有这个测试类:
namespace Tests
{
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
Assert.Pass("PASSED");
}
}
}
目前只包含 Test1() 方法,每次执行时都应该通过该方法(因为我使用的是 Assert.Pass() 方法。
如果我运行这个测试它工作正常并且测试通过(Visual Studio 测试资源管理器中的绿色条)。
但是如果我尝试调试此方法(在 Assert.Pass("PASSED"); 行上放置一个断点,它会给我这个尝试执行 Pass() 方法的异常:
Exception thrown: 'NUnit.Framework.SuccessException' in nunit.framework.dll
An exception of type 'NUnit.Framework.SuccessException' occurred in nunit.framework.dll but was not handled in user code
PASSED
反正测试好像是通过了。但是为什么我在 Debug模式下会得到这个异常?
最佳答案
Assert.Pass
确实抛出一个异常,它允许您向测试运行器发送消息。您很少需要使用Assert.Pass
,因为您不需要为了测试通过而这样做。您可以让测试完成。
显然,您的实际测试应该断言某事,但通常您会检查一个值是否符合预期。到时候也不异常(exception)。
来自documentation ...
Since it causes an exception to be thrown, it is more efficient to simply allow the test to return
关于c# - 为什么我在尝试调试 NUNIT 测试时获得此 "NUnit.Framework.SuccessException"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53957539/