--
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.
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();
}
}
OQuick and dirty way of doing it.
- Build a Test listener which implements IInvokedMethodListener and wire it in via a SPI model (service loaders in java)
- within the beforeInvocation() instantiate a softassert object and push it into the current test method's ITestResult object as an attribute via iTestResult.setAttribute()
- From within the @Test method, pop out the softassert object by querying Reporter.getCurrentTestResult().getAttribute() [ use casting as required ]
- collect all assertion failures by calling the different assertEquals/assertTrue etc.,
- 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.
- 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 #2If 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.Thankssunny 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.
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/