Coded UI - “Continue on failure” for Assertions

半城伤御伤魂 提交于 2019-12-12 13:20:04

问题


I'm using SpecFlow with Coded UI to create automated tests for a WPF application.

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.

Thanks!


回答1:


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");
}

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");
}



回答2:


Make a List of Exceptions. Whenever an exception is encountered, catch it and put it in the list.

Create a method with attribute AfterScenario and see if the list contains Exceptions. If true, Assert a fail with a message the stringyfied list of exceptions. Now you don't lose valuable Exception information and the check on Exceptions always happens on the end because of the AfterScenario attribute.



来源:https://stackoverflow.com/questions/17924717/coded-ui-continue-on-failure-for-assertions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!