问题
Hi I know that what I am about to ask has been asked before by others however I am still unclear about those that has been posted online hence I am posting this Question to clarify my doubts. Hope that you guys can help me out with it.
Currently I am using Microsoft Visual Studio 2013 Premium. I am using the record and play functions. I record some actions and a few verification points. Now the script will immediately stop when an verification point fails. However I want the script to continue running even though some point has failed. And I read of a few option online however I have no idea on where should I place those at my script. I saw this post Coded UI - "Continue on failure" for Assertions However I am not using SpecFlow is that still applicable for me? Also which part should I place those code in? Inside my method? Create a new method? Or?
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");
}"
回答1:
An assertion, does not imply using Assert.SomeCondition()
. Just use simple conditionals to set thisTestFailed
. The Assert class throws an AssertFailedException
to signal a failure. This exception should not be captured. This exception is handled by the unit test engine to indicate an assert failure.
bool thisTestFailed = false;
if ( {someCondition} ) { thisTestFailed = true; }
if ( !{someOtherConditionWhichShouldBeFalse} ) { thisTestFailed = true; }
if ( {yetAnotherCondition} ) { thisTestFailed = true; }
if ( thisTestFailed ) {
Assert.Fail("A suitable test failed message");
}"
Or even easier:
bool thisTestFailed = {someCondition} ||
!{someOtherConditionWhichShouldBeFalse} ||
{yetAnotherCondition}
Assert.IsFalse(thisTestFailed, "A suitable test failed message");
This way you get all your "tests", but only one Assert.Fail
.
回答2:
The linked question does mention Specflow, but the details of the question and the accepted answer do not depend on Specflow.
Typically an assertion method created by Coded UI record and generate is a bit like:
void AssertionMethod1()
{
... get the values of fieldOne, fieldTwo, etc
Assert.Equals(fieldOne, fieldOneExpectedValue);
Assert.Equals(fieldTwo, fieldTwoExpectedValue);
Assert.Equals(fieldThree, fieldThreeExpectedValue);
... more calls of assert methods.
}
The assertion method used and the mechanism of accessing the field values will probably be more complex.
Each of the Assert.Equals(...)
is implemented with code similar in purpose to:
void Assert.Equals(string actual, string expected)
{
if ( actual == expected )
{
// Pass. Nothing more to do.
}
else
{
// Fail the test now.
throw AnAssertionFailureException(...);
}
}
The linked question is thus suggesting changing the call to the recorded assertion method (ie the call of AssertionMethod1
from CodedUItestMethod1
) to be something like:
... get the values of fieldOne, fieldTwo, etc
if(fieldOne != fieldOneExpectedValue) { thisTestFailed = true; }
if(fieldTwo != fieldTwoExpectedValue) { thisTestFailed = true; }
if(fieldThree != fieldThreeExpectedValue) { thisTestFailed = true; }
... etc.
Although an easier way may be to copy the AssertionMethod1
into another file and then modify it to be like the above. The UIMap editor in Coded UI has a "Move code to UIMap.cs" command (accessed by context menu or by command icon) that moves a method into the uimap.cs
file, thus allowing you to edit it as needed. The auto-generated code goes into the uimap.designer.cs
file and this file should not be edited. Both it and the uimap.cs
file have partial class UIMap
near their tops so their combined contents makes the class. The uimap.cs
file is for your additions to the UI Map.
来源:https://stackoverflow.com/questions/28011305/coded-ui-continue-on-failure-assertions