How to capture "Expected " value in generateReport method ?

450 views
Skip to first unread message

SSR

unread,
May 22, 2016, 7:44:59 AM5/22/16
to testng-users
Hello,
I am trying to capture all the suite information in public void generateReport(List xmlSuites, List suites, String outputDirectory) method.
Not sure how to collect information about "Expected" value ?

for example, if my testcase has

assertEquals(String actual,String expected)


I would like to store both actual and expected value in database.



Thanks




⇜Krishnan Mahadevan⇝

unread,
May 22, 2016, 11:17:50 PM5/22/16
to testng...@googlegroups.com
You can build your own version of Hard and soft Asserts [ which would internally use Asserts and SoftAssertion ] wherein you can capture these values and add them into a list of attributes to the ITestResult and later query them no ?

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...@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.

SSR

unread,
May 23, 2016, 5:16:09 AM5/23/16
to testng-users
Do you have any example ?

sunny sachdeva

unread,
May 23, 2016, 1:52:49 PM5/23/16
to testng-users
HI SSR,

As explained My Krishnan, you can create your own custom sortAssert class by extending it from TestNG Soft Assert and then on AssertFailure and onAssertSuccess you can user Reporter.log to print actual as well as expected values

public class MySoftAssert extends SoftAssert{

@Override
public void executeAssert(IAssert a) {
super.executeAssert(a);
}

@Override
public void assertAll() {
super.assertAll();
}

@Override
protected void doAssert(IAssert assertCommand) {
super.doAssert(assertCommand);
}

@Override
public void onAssertSuccess(IAssert assertCommand) {
              Reporter.log("EXPECTED VALUE :: "+assertCommand.getExpected());
              Reporter.log("ACTUALVALUE :: "+assertCommand.getActual());
super.onAssertSuccess(assertCommand);
}

@Override
public void onAssertFailure(IAssert assertCommand) {
super.onAssertFailure(assertCommand);
}

@Override
public void onAssertFailure(IAssert assertCommand, AssertionError ex) {
super.onAssertFailure(assertCommand, ex);
}

@Override
public void onBeforeAssert(IAssert assertCommand) {
super.onBeforeAssert(assertCommand);
}

@Override
public void onAfterAssert(IAssert assertCommand) {
super.onAfterAssert(assertCommand);

SSR

unread,
May 24, 2016, 5:31:03 AM5/24/16
to testng-users
Thanks Sunny for your suggestion. But I am not sure how can I capture these value in my listener.

we are overriding  generateReport(List xmlSuites, List suites, String outputDirectory) method in our listener by implementing IReporter
we collect all pass, fail and skipped tests.
Iterate over each collection to get information about each test and store in database.

So, I am not sure how your solution will help me. sorry I am new to TestNg and Selenium.

Thanks


On Sunday, May 22, 2016 at 6:44:59 AM UTC-5, SSR wrote:

⇜Krishnan Mahadevan⇝

unread,
May 24, 2016, 6:19:23 AM5/24/16
to testng...@googlegroups.com
Sneha,
By default whenever you do Reporter.log() it always adds it to the current method's ITestResult object. 

So you could very easily call Reporter.getOutput(ITestResult) inside your Reporter listener to extract out these "test specific" logs.

If you are looking for something more sophisticated, I would suggest that you build your own implementation of Asserts [ both soft and hard ] wherein for every assertion, you add it to the current test method's ITestResult as an attribute.



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/

SSR

unread,
May 24, 2016, 9:01:59 AM5/24/16
to testng-users
Thanks Krishnan for your quick reply. I am leaning towards your second option of adding to the current test method's ITestResult as an attribute so that I can get this value in generateReport method . Do you have an example ? I am not sure how to do that.

Thanks

On Sunday, May 22, 2016 at 6:44:59 AM UTC-5, SSR wrote:

⇜Krishnan Mahadevan⇝

unread,
May 24, 2016, 11:23:14 PM5/24/16
to testng...@googlegroups.com
Sneha,

The below example should help you get started.

The Custom Assertion class [ This is incomplete. So feel free to add more as needed ]

public class CustomAssertions {
public static final String ASSERTIONS = "assertions";

public static void assertTrue(boolean condition, String message) {
AssertionInfo info = new AssertionInfo(true, condition, message);
try {
Assert.assertTrue(condition, message);
} finally {
List<AssertionInfo> assertions =
(List<AssertionInfo>) Reporter.getCurrentTestResult().getAttribute(ASSERTIONS);
if (assertions == null) {
assertions = new LinkedList<>();
}
assertions.add(info);
Reporter.getCurrentTestResult().setAttribute(ASSERTIONS, assertions);
}
}

public static class AssertionInfo {
private Object expected;
private Object actual;
private String message;

public Object getExpected() {
return expected;
}

public Object getActual() {
return actual;
}

public String getMessage() {
return message;
}

public AssertionInfo(Object expected, Object actual, String message) {
this.expected = expected;
this.actual = actual;
this.message = message;
}
}

}

A sample Test class which shows how to use the above assertion and a simple TestNG listener which shows the assertion information.

@Listeners (CustomAssertionsDemo.LocalListener.class)
public class CustomAssertionsDemo {
@Test
public void testMethod() {
CustomAssertions.assertTrue(false, "This is a failure");

}

@Test
public void anotherTestMethod() {
CustomAssertions.assertTrue(true, "This is a pass.");
}

public static class LocalListener implements ITestListener {
@Override
public void onFinish(ITestContext context) {
Set<ITestResult> allResults = new HashSet<>(context.getPassedTests().getAllResults());
allResults.addAll(context.getFailedTests().getAllResults());
allResults.addAll(context.getSkippedTests().getAllResults());
for (ITestResult method : allResults) {
List<CustomAssertions.AssertionInfo> assertions =
(List<CustomAssertions.AssertionInfo>) method.getAttribute(CustomAssertions.ASSERTIONS);
if (assertions == null || assertions.isEmpty()) {
continue;
}
System.err.println("Printing assertions for " + method.getMethod().getMethodName() + "()");
for (CustomAssertions.AssertionInfo assertion : assertions) {
String msg =
String.format("Expected: [%s], Actual[%s], Message: [%s]", assertion.getExpected().toString(),
assertion.getActual().toString(), assertion.getMessage());
System.err.println("Assertions ===>" + msg);
}
}

}

@Override
public void onTestStart(ITestResult result) {}

@Override
public void onTestSuccess(ITestResult result) {}

@Override
public void onTestFailure(ITestResult result) {}

@Override
public void onTestSkipped(ITestResult result) {}

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {}

@Override
public void onStart(ITestContext context) {}
}
}

Here's a sample output of the execution :

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running organized.chaos.testng.CustomAssertionsDemo
Printing assertions for testMethod()
Assertions ===>Expected: [true], Actual[false], Message: [This is a failure]
Printing assertions for anotherTestMethod()
Assertions ===>Expected: [true], Actual[true], Message: [This is a pass.]
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.589 sec <<< FAILURE! - in organized.chaos.testng.CustomAssertionsDemo
testMethod(organized.chaos.testng.CustomAssertionsDemo)  Time elapsed: 0.007 sec  <<< FAILURE!
java.lang.AssertionError: This is a failure expected [true] but found [false]
at organized.chaos.testng.CustomAssertionsDemo.testMethod(CustomAssertionsDemo.java:17)


Results :

Failed tests:
  CustomAssertionsDemo.testMethod:17 This is a failure expected [true] but found [false]

Tests run: 2, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------













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/

SSR

unread,
May 25, 2016, 6:34:30 AM5/25/16
to testng-users
Thanks Krishnan !!!


On Sunday, May 22, 2016 at 6:44:59 AM UTC-5, SSR wrote:
Reply all
Reply to author
Forward
0 new messages