Coded UI - “Continue on failure” for Assertions

2019-07-06 04:33发布

问题:

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.