any alternate to SoftAssert.assertAll() calling in each test method

1,865 views
Skip to first unread message

sunny sachdeva

unread,
Dec 7, 2017, 5:49:27 AM12/7/17
to testng-users
HI All,

For folks who are using soft assertions in testNG, is there any way where we don;t have to call assertAll method every time in each @Test method.

Thanks
sunny Sachdeva

⇜Krishnan Mahadevan⇝

unread,
Dec 7, 2017, 6:00:03 AM12/7/17
to testng...@googlegroups.com
Quick and dirty way of doing it.

  1. Build a Test listener which implements IInvokedMethodListener and wire it in via a SPI model (service loaders in java)
  2. within the beforeInvocation() instantiate a softassert object and push it into the current test method's ITestResult object as an attribute via iTestResult.setAttribute()
  3. From within the @Test method, pop out the softassert object by querying Reporter.getCurrentTestResult().getAttribute() [ use casting as required ]
  4. collect all assertion failures by calling the different assertEquals/assertTrue etc.,
  5. Now from within the afterInvocation() of the listener built in (1) pop out the soft assert object from within the ITestResult object's attribute set. 
  6. Invoke assertAll() wrapping it within a try..catch block, catch the AssertionError, update the ITestResult object's throwable value and the status accordingly.
pitfall :
Test method doesn't appear to fail in the console but will appear as failed in the reports. [ I havent tried this out, but this theory should work ]

Option #2

If you have the luxury of defining a base class for all your test classes, then create a base class which implements org.testng.IHookable and within that you basically invoke the softassert.assertAll call 


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

sunny sachdeva

unread,
Dec 11, 2017, 12:16:30 AM12/11/17
to testng-users
Krishnan,

I tried option 2 since we are using BaseClass for running certain configurations.

My query is how would soft assert instance be called inside run method? Can you please help me in explaining this.

public class BaseClass implements org.testng.IHookable {
 
@BeforeSuite
 
public void beforeSuite() {
 
System.out.println("before suite");
 
}
 
public void run(IHookCallBack callBack, ITestResult testResult) {
 
// TODO Auto-generated method stub
 
System.out.println("Inside Call back");
 
Object[] parms = callBack.getParameters();
 
for (Object o: parms) {
 
System.out.println("---" + o);
 
}
 callBack
.runTestMethod(testResult);
 
SoftAssert s= new SoftAssert();
 s
.assertAll();
 
}
}
O

n Thursday, December 7, 2017 at 4:30:03 PM UTC+5:30, Krishnan Mahadevan wrote:
Quick and dirty way of doing it.

  1. Build a Test listener which implements IInvokedMethodListener and wire it in via a SPI model (service loaders in java)
  2. within the beforeInvocation() instantiate a softassert object and push it into the current test method's ITestResult object as an attribute via iTestResult.setAttribute()
  3. From within the @Test method, pop out the softassert object by querying Reporter.getCurrentTestResult().getAttribute() [ use casting as required ]
  4. collect all assertion failures by calling the different assertEquals/assertTrue etc.,
  5. Now from within the afterInvocation() of the listener built in (1) pop out the soft assert object from within the ITestResult object's attribute set. 
  6. Invoke assertAll() wrapping it within a try..catch block, catch the AssertionError, update the ITestResult object's throwable value and the status accordingly.
pitfall :
Test method doesn't appear to fail in the console but will appear as failed in the reports. [ I havent tried this out, but this theory should work ]

Option #2

If you have the luxury of defining a base class for all your test classes, then create a base class which implements org.testng.IHookable and within that you basically invoke the softassert.assertAll call 


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

On Thu, Dec 7, 2017 at 4:19 PM, sunny sachdeva <sunal...@gmail.com> wrote:
HI All,

For folks who are using soft assertions in testNG, is there any way where we don;t have to call assertAll method every time in each @Test method.

Thanks
sunny Sachdeva

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.

Krishnan Mahadevan

unread,
Dec 11, 2017, 9:47:58 PM12/11/17
to testng...@googlegroups.com

Sunny,

 

This is what I am talking about.

 

import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class SoftAssertsDemo implements IHookable {
   
private static final String SOFT_ASSERT = "softAssert";

   
@Override
   
public void run(IHookCallBack callBack, ITestResult testResult) {
        SoftAssert softAssert =
new SoftAssert();
        testResult.setAttribute(
SOFT_ASSERT, softAssert);
        callBack.runTestMethod(testResult);
        softAssert = get(testResult);
       
try {
            softAssert.assertAll();
        }
catch (AssertionError e) {
            testResult.setThrowable(e);
            testResult.setStatus(ITestResult.
FAILURE);
        }
    }

   
@Test
   
public void testMethod1() {
        SoftAssert softAssert = get();
        softAssert.assertEquals(
1, 2, "Int equality in testMethod1()");
        softAssert.assertEquals(
true, true, "Boolean equality in testMethod1()");
    }

   
@Test
   
public void testMethod2() {
        SoftAssert softAssert = get();
        softAssert.assertEquals(
2, 2, "Int equality in testMethod2()");
        softAssert.assertEquals(
true, true, "Boolean equality in testMethod2()");
    }

   
private static SoftAssert get() {
       
return get(Reporter.getCurrentTestResult());
    }

   
private static SoftAssert get(ITestResult result) {
        Object object = result.getAttribute(
SOFT_ASSERT);
       
if (object instanceof SoftAssert) {
           
return (SoftAssert) object;
        }
       
throw new IllegalStateException("Could not find a soft assertion object");
    }
}

 

 

 

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

 

sunny sachdeva

unread,
Dec 13, 2017, 11:25:17 PM12/13/17
to testng-users
Great learning for me Krishnan. Thanks for the insight. I never realized we could use setAttribute this way.

Thanks again and deeply appreciate your effort.

Sunny Sachdeva
Reply all
Reply to author
Forward
0 new messages