-
Notifications
You must be signed in to change notification settings - Fork 55
JUnit5 callback reporting example
Ihar Kahadouski edited this page Feb 27, 2020
·
3 revisions
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 JUnit5 @AfterEach
method passing TestInfo
as argument.
@AfterEach
void afterMethod(TestInfo testInfo) {
TestItemTree.TestItemLeaf testItemLeaf = ItemTreeUtils.retrieveLeaf(testInfo, TEST_ITEM_TREE);
if (testItemLeaf != null) {
attachLog(testItemLeaf);
finishWithStatus(testInfo.getDisplayName().contains("someTest") ? "FAILED" : "PASSED", testItemLeaf);
}
}
TestItemLeaf
can be retrieved using ItemTreeUtils util.
Send Log
using ItemTreeReporter sendLog
method:
private static void attachLog(TestItemTree.TestItemLeaf testItemLeaf) {
ItemTreeReporter.sendLog(REPORT_PORTAL.getClient(),
"ERROR",
"Error message",
Calendar.getInstance().getTime(),
testItemLeaf
);
}
Send finish request for TestItem
using ItemTreeReporter
finishItem
method:
private void finishWithStatus(String status, TestItemTree.TestItemLeaf testItemLeaf) {
FinishTestItemRQ finishTestItemRQ = new FinishTestItemRQ();
finishTestItemRQ.setStatus(status);
finishTestItemRQ.setEndTime(Calendar.getInstance().getTime());
ItemTreeReporter.finishItem(REPORT_PORTAL.getClient(), finishTestItemRQ, TEST_ITEM_TREE.getLaunchId(), testItemLeaf)
.cache()
.subscribe();
}
Adding attributes (Saucelabs job id)
private static void addSaucelabsAttribute(TestItemTree.TestItemLeaf leaf) {
FinishTestItemRQ request = new FinishTestItemRQ();
request.setStatus("passed");
request.setEndTime(Calendar.getInstance().getTime());
request.setAttributes(Sets.newHashSet(new ItemAttributesRQ("SLID", "0586c1c90fcd4a499591109692426d54")));
ItemTreeReporter.finishItem(REPORT_PORTAL.getClient(), request, TEST_ITEM_TREE.getLaunchId(), leaf).cache().subscribe();
}
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).