Skip to content

TestNG callback reporting example

Ivan edited this page Nov 27, 2019 · 4 revisions

TestNG callback reporting example

Usage of callback reporting is represented in this test class.

Basic steps to use callback reporting feature in your test classes:

You can either provide storage for retrieved TestItemLeaf like in JUnit4 example or send requests right after each test method execution using the TestNG @AfterMethod annotation passing ITestResult as an argument to the marked method:

@AfterMethod
public void after(ITestResult testResult) {
	TestItemTree.TestItemLeaf testResultLeaf = ItemTreeUtils.retrieveLeaf(testResult, ITEM_TREE);
	if (testResultLeaf != null) {
		sendLog(testResultLeaf);
		sendFinishRequest(testResultLeaf);
	}

}

TestItemLeaf can be retrieved using ItemTreeUtils util.

Send Log using ItemTreeReporter sendLog method:

private void sendLog(TestItemTree.TestItemLeaf testResultLeaf) {
	ItemTreeReporter.sendLog(REPORT_PORTAL.getClient(),
			"ERROR",
			"Callback log",
			Calendar.getInstance().getTime(),
			ITEM_TREE.getLaunchId(),
			testResultLeaf
	);
}

Send finish request for TestItem using ItemTreeReporter finishItem method:

private void sendFinishRequest(TestItemTree.TestItemLeaf testResultLeaf) {
	FinishTestItemRQ finishTestItemRQ = new FinishTestItemRQ();
	finishTestItemRQ.setStatus("FAILED");
	finishTestItemRQ.setEndTime(Calendar.getInstance().getTime());
	ItemTreeReporter.finishItem(REPORT_PORTAL.getClient(), finishTestItemRQ, ITEM_TREE.getLaunchId(), testResultLeaf)
			.cache()
			.blockingGet();
}

TestItem finish method returns Maybe<String> so you should either provide consumer for result or use blockingGet to provide request sending before test run finish (application termination).