|
I would like to store both actual and expected value in database. Thanks |
--
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.
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;
}
}
}
@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) {}
}
}