我使用 NUnit 3.0 来运行测试。
我的问题是下一个:我有带有测试的类,并且在此类的构造函数中有一个在所有测试之前运行的方法(因为它在构造函数中)。当此方法正在执行时,发生错误,然后我使用CleanUp
方法处理它。在CleanUp
方法中,我使用 TestContext.CurrentContext.Result.Outcome.Status
属性来查看我运行的测试的状态。
问题是测试根本没有运行(因为前提条件方法失败)。那么为什么此属性告诉我测试已通过?
这是我的一段代码:
public class DepositSuccessTests
{
//attributes
public DepositSuccessTests(string device, WayToPay merchant)
{
SetUp();
try
{
Deposit_Money(merchant); // this method invokes an exception
}
catch (Exception e)
{
CleanUp();
}
}
[Test]
//Test that I run but that is not executed because of exception in the constructor
public void Check_Transaction_Merchant_In_Transactions()
{
_orderVerifications.CheckTransactionMerchantInTransactions_Back(_merchant);
}
[TearDown]
public void CleanUp()
{
//In the next line my test result is "Passed"
if (TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed)
{
//Do something
}
_driver.Dispose();
}
}
Deposit_Money(merchant);
行导致了异常,您也已经捕获了它,因此就运行器而言,没有问题,所有代码都经过了没有错误。你知道有问题,我知道有问题,但跑步者没有。
如果您在捕获中显式失败,那么它会将其标记为未通过。
try
{
Deposit_Money(merchant); // this method invokes an exception
}
catch (Exception e)
{
Assert.Fail(); /// Explicitly fail the test
CleanUp();
}
您需要允许在测试中抛出异常,因为严格来说,您的测试确实已通过。
删除测试中的错误检查,允许引发异常,应会看到测试结果为失败。