如何使用软断言来避免测试运行执行失败

2023-12-22

我想继续测试运行执行,即使一个或多个断言失败TestNG.

我引用了下面的链接来实施soft assertion在我的项目中。

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

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

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

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

请帮助我了解该公司的工作流程soft assertions.

Code:

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:

即使上述断言失败,执行仍继续


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

我相信 TestNG 的维护者的想法是呼吁myAssertion.assertAll()在将运行的测试结束时Assert.fail()如果之前的任何软断言检查失败,则使测试失败。

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用软断言来避免测试运行执行失败 的相关文章

随机推荐