软断言的工作原理

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

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。

请帮助我理解柔软的工作流程。

码:

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"); 

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

谢谢Mahesh

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

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

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

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