我使用 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();
}
}