How to use multiple assert in a test

2019-08-29 13:33发布

问题:

I have to work on a HTML page and verify the result (adding some texts, selecting some items from comboboxes, clicking on some button).

For example action1, action2 and action3.

After each action, I need to verify the result of the previous action. In QTP, we would use Report.Fail (Pass, warning) and go to the next action.

Here in code UI, I'm using something like

try{
  Action1;
  Assert //some kind of assert
  Action2;
  Assert //some kind of assert
  Action3;
  Assert //some kind of assert
}
Catch (exception ex)
{
  ...
  Assert.Fail()
}

This works somehow but not perfect. The problem is, if I remove the Assert.Fail from catch, the test will be passed! Which is odd (even the previous assert caused to get into catch statement). On the other hand, If I keep the assert.fail in the catch statement, the test will fail, but the screen capture is not the right one (the screen capture will be taken by assert.fail but not the previous ones).

So my 1st question is how can I use multiple asserts and if one of them fails, close the test in right way and mark the test as Failed?

my 2nd question is, is it possible to make a logical operation with assert? Lets assume that we have a condition which can be A or B (depending or run time case). Now how should I check with the assert the conditions? It should pass the test only, if the condition is A or B. In all other cases the test should fail.

Thanks

回答1:

The Coded UI framework code calls methods with a [TestMethod] attribute. Inside the test method an assert is called to report a test failure, the Assert throws an exception that the framework catches. That framework save an image after it catches the exception. If your code catches the exception then the failure is not passed to the framework. If your code catches several exceptions from different asserts in your test then the framework will not see those exceptions. Hence the behavior you see.

Coded UI generally works on the basis of stopping the test at the first condition (ie the first assertion) that fails. If you want a test to check multiple items and report all of them that fail then you need to code that yourself. Commonly you change the assert calls to simple if tests and, if the test fails record why, then near the end of the test check whether any of the tests failed. If they did then call Assert.Fail and pass a summary of the failure reasons.

For getting screen images from the points of failure you can use a mixture of these types of statement:

Image img1 = UITestControl.Desktop.CaptureImage();
Image img2 = this.UIMap.UIYourApplicationsWindow.CaptureImage();
Image img3 = this.UIMap.UIYourApplicationsWindow.UISubWindow.UISubSub.CaptureImage();
... followed by:
imgX.Save( ... filename ... ); // For various X
TextContext.AddResultFile(... filename ... ) 

The img.Save calls will create a file for each call including calls from tests that pass. The AddResultFile makes copies of those files for tests that fail.