gpt4 book ai didi

c# - 在测试失败时运行额外的清理功能(当断言失败时)

转载 作者:行者123 更新时间:2023-11-30 23:31:33 25 4
gpt4 key购买 nike

我目前有一个使用 Nunit 和 C# 以及使用断言的 Selenium WebDriver 的测试套件,有没有办法让函数仅在断言失败时运行。通常这将继续进行拆解,然后进行下一个测试,但是如果上一个测试失败,我需要在测试中重置某些参数。执行此操作的最佳方法是什么?

最佳答案

您可以扩展 TestActionAttribute 来处理这个问题。在 AfterTest() 覆盖中,您可以检查 TestContext.CurrentContext.Result.Status == TestStatus.Failed 并进行适当处理。例如,

public class ClassTest
{
[Test]
[OnFailure] // << your custom attribute here
public void MyTest()
{
Assert.Fail("test failed");
}
}

[AttributeUsage(AttributeTargets.Method)]
public class OnFailureAttribute : TestActionAttribute
{
public override void AfterTest(TestDetails testDetails)
{
if(TestContext.CurrentContext.Result.Status == TestStatus.Failed)
{
// TODO: your error handling
Console.WriteLine(testDetails.FullName + " failed");
}
}
}

关于c# - 在测试失败时运行额外的清理功能(当断言失败时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34552629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com