软断言的工作原理 [英] How Soft Assertions Work

查看:455
本文介绍了软断言的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道继续测试,即使一个或多个断言在TestNG中失败。
我在下面提到了链接,以便在我的项目中实现软断言。

I got to know continuing tests even the one or more assertions get fails in TestNG. I referred below links in order to implement soft assertion in my project.

http://beust.com/weblog/2012/07/29/reinventing-assertions/

http://seleniumexamples.com/blog/guide/ using-soft-assertions-in-testng /

http://www.seleniumtests.com/2008/09/soft-assertion-is-check-which-doesnt.html

但我不理解代码执行的流程,比如函数调用,FLOW。

But i am not understanding the flow of code execution, like function calls, FLOW.

请帮助我了解软生命的工作流程。

Kindly help me to understand the work flow of the soft asserions.

代码:

import org.testng.asserts.Assertion;
    import org.testng.asserts.IAssert; 

    //Implementation Of Soft Assertion 
    public class SoftAssertions extends Assertion{   
    @Override public void executeAssert(IAssert a){ 
    try{ a.doAssert(); } 
    catch(AssertionError ex){ 
    System.out.println(a.getMessage()); } } } 

    //Calling Soft Assertion
 SoftAssertions sa = new SoftAssertions(); 
 sa.assertTrue(actualTitle.equals(expectedTitle),
"Login Success, But Uname and Pwd are wrong"); 

注意:即使上述断言失败,执行也会继续

Note:Execution Continues even though above assertion fails

谢谢
Mahesh

Thanks Mahesh

推荐答案

软断言通过将故障存储在本地状态(可能将它们记录到<遇到code> stderr 。当测试完成时,它需要检查是否存在任何存储的故障,如果遇到任何故障,则在那时失败整个测试。

Soft assertions work by storing the failure in local state (maybe logging them to stderr as they are encountered). When the test is finished it needs to check for any stored failures and, if any were encountered, fail the entire test at that point.

我相信TestNG的维护者是什么我想到的是在测试结束时调用 myAssertion.assertAll()将运行 Assert.fail()

I believe what the maintainer of TestNG had in mind was a call to myAssertion.assertAll() at the end of the test which will run Assert.fail() and make the test fail if any previous soft-assertion checks failed.

您可以通过添加 @Before 初始化本地软断言对象的方法,在测试中使用它并添加 @After 方法来运行 assertAll( )软断言对象上的方法。

You can make this happen yourself by adding a @Before method to initialize your local soft-assertion object, use it in your test and add an @After method to run the assertAll() method on your soft-assertion object.

请注意这个 @Before / @After 方法使您的测试非线程安全,因此每个测试必须在测试类的新实例中运行。如果您的测试需要是线程安全的,那么在测试方法本身内创建软断言对象并在方法结束时运行 assertAll()检查是可取的。 TestNG的一个很酷的功能是它能够运行多线程测试,因此在实现这些软断言时要注意这一点。

Be aware that this @Before/@After approach makes your test non-thread-safe so each test must be run within a new instance of your test class. Creating your soft-assertion object inside the test method itself and running the assertAll() check at the end of the method is preferable if your test needs to be thread-safe. One of the cool features of TestNG is its ability to run multi-threaded tests, so be aware of that as you implement these soft-asserts.

这篇关于软断言的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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