编码的用户界面-“失败时继续”;断言 [英] Coded UI - "Continue on failure" for Assertions
问题描述
我正在使用SpecFlow和编码UI为WPF应用程序创建自动化测试。
I'm using SpecFlow with Coded UI to create automated tests for a WPF application.
我在 Then步骤中有多个断言,其中有两个失败。断言失败时,测试用例失败并停止执行。我希望我的测试用例继续进行到执行结束,如果执行过程中出现任何失败的断言,则执行最后一步时,我要使整个测试用例都失败。
I have multiple assertions inside a "Then" step and a couple of them fails. When an assertion fails, the test case is failed and the execution is stopped. I want my test case to go ahead till the end with the execution and when the last step is performed if any failed assertions were present during the execution I want to fail the whole test case.
我只找到了部分解决方案:
I found only partial solutions:
try
{
Assert.IsTrue(condition)
}
catch(AssertFailedException ex)
{
Console.WriteLine("Assert failed, continuing the run");
}
在这种情况下,执行一直进行到最后,但是测试用例已标记
In this case the execution goes till the end, but the test case is marked as passed.
谢谢!
推荐答案
一种方法是添加声明一个 bool thisTestFailed
并将其初始化为false。在 catch
块内,添加语句 thisTestFailed = true;
,然后在测试末尾附近添加代码,例如:
One approach is to add declare a bool thisTestFailed
and initialize it to false. Within the catch
blocks add the statement thisTestFailed = true;
then near the end of the test add code such as:
if ( thisTestFailed ) {
Assert.Fail("A suitable test failed message");
}
另一种方法是转换一系列 Assert。 ..
语句进行一系列的 if
测试,然后执行一个 Assert
。有几种方法可以做到这一点。一种方法是:
Another approach is to convert a series of Assert...
statements into a series of if
tests followed by one Assert
. There are several ways of doing that. One way is:
bool thisTestFailed = false;
if ( ... the first assertion ... ) { thisTestFailed = true; }
if ( ... another assertion ... ) { thisTestFailed = true; }
if ( ... and another assertion ... ) { thisTestFailed = true; }
if ( thisTestFailed ) {
Assert.Fail("A suitable test failed message");
}
这篇关于编码的用户界面-“失败时继续”;断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!