如何在测试中使用多个断言 [英] How to use multiple assert in a test

查看:416
本文介绍了如何在测试中使用多个断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在HTML页面上工作并验证结果(添加一些文本,从组合框中选择一些项目,单击某些按钮).

例如action1,action2和action3.

每个动作之后,我需要验证上一个动作的结果.在QTP中,我们将使用Report.Fail(通过,警告)并转到下一个操作.

在代码UI中,我使用的是类似

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

这在某种程度上有效,但并不完美.问题是,如果我从catch中删除Assert.Fail,则测试将通过!这很奇怪(即使先前的断言也导致进入catch语句).另一方面,如果将assert.fail保留在catch语句中,则测试将失败,但是屏幕截图不是正确的屏幕截图(屏幕截图将由assert.fail进行,而不是先前的屏幕截图)./p>

所以我的第一个问题是如何使用多个断言,如果其中一个失败,请以正确的方式关闭测试并将测试标记为失败"?

我的第二个问题是,是否可以使用assert进行逻辑运算?假设我们有一个条件,可以是A或B(取决于情况或运行时的情况).现在我应该如何确认条件呢?仅当条件为A或B时,它才应通过测试.在所有其他情况下,测试应失败.

谢谢

解决方案

编码的UI框架代码调用具有[TestMethod]属性的方法.在测试方法中,调用 assert 来报告测试失败,Assert引发框架捕获的异常.该框架在捕获异常后保存图像.如果您的代码捕获到异常,则故障不会传递到框架.如果您的代码从测试中的不同断言中捕获到多个异常,则框架将看不到这些异常.因此,您会看到这种行为.

编码的UI通常在失败的第一个条件(即第一个断言)下停止测试的基础上工作.如果要测试检查多个项目并报告所有失败的项目,则需要自己编写代码.通常,您将assert调用更改为简单的if测试,如果测试失败记录原因,则在测试即将结束时检查是否有任何测试失败.如果他们这样做了,则调用Assert.Fail并传递失败原因摘要.

要从故障点获取屏幕图像,可以混合使用以下类型的语句:

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 ... ) 

img.Save调用将为每个调用(包括通过测试的调用)创建一个文件. AddResultFile为那些失败的测试复制这些文件.

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

解决方案

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.

这篇关于如何在测试中使用多个断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆