Java 软断言的工作原理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19091526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Soft Assertions Work
提问by Shetty's
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.
即使一个或多个断言在 TestNG 中失败,我也开始了解继续测试。为了在我的项目中实现软断言,我参考了以下链接。
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
But i am not understanding the flow of code execution, like function calls, FLOW.
但我不理解代码执行的流程,比如函数调用,FLOW。
Kindly help me to understand the work flow of the soft asserions.
请帮助我了解软断言的工作流程。
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:Execution Continues even though above assertion fails
注意:即使上述断言失败,执行继续
Thanks Mahesh
谢谢马赫什
回答by Shadow Man
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.
软断言通过将故障存储在本地状态(可能stderr
在遇到它们时将它们记录到)来工作。测试完成后,它需要检查任何存储的故障,如果遇到任何故障,则在该点上使整个测试失败。
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.
我相信 TestNG 的维护者所想到的是myAssertion.assertAll()
在测试结束时调用 ,Assert.fail()
如果之前的任何软断言检查失败,它将运行并使测试失败。
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()
在您的软断言对象上运行该方法,从而自己实现这一点。
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.
请注意,此@Before
/@After
方法使您的测试非线程安全,因此每个测试都必须在测试类的新实例中运行。assertAll()
如果您的测试需要线程安全,则最好在测试方法本身内部创建软断言对象并在方法结束时运行检查。TestNG 的一个很酷的特性是它能够运行多线程测试,所以在你实现这些软断言时要注意这一点。