简体   繁体   中英

How Soft Assertions Work

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

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.

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

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM