If you’re utilizing JUnit5 in your Automation and want to submit your take a look at execution outcomes straight from CI/CD to your Take a look at Administration System through Zephyr API, it’s attainable to take action through JUnit5 Take a look at Execution Listeners.
Customized Listener
You may create a customized Listener in response to your wants. In our case, the API wants the ends in a particular format and zipped. We must add customized logic for that and ship the Take a look at case identify and key together with the execution outcome for a take a look at. you are able to do this by utilizing the next Listener strategies.
@Override
public void beforeTestExecution(ExtensionContext context) {
testCaseDataMap.clear();
String testCase = TestUtil.getTestCaseName(context);
String key = TestUtil.getTestCaseKey(context);
testCaseDataMap.put("testCaseKey", key);
testCaseDataMap.put("testCaseName", testCase);
}
@Override
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
String testCaseKey = testCaseDataMap.get("testCaseKey");
String testCaseName = testCaseDataMap.get("testCaseName");
if (!isTestCaseExecuted(testCaseKey)) {
ObjectNode execution = objectMapper.createObjectNode();
execution.put("result", getResultString(testExecutionResult.getStatus()));
execution.put("source", testCaseName);
ObjectNode testCase = objectMapper.createObjectNode();
testCase.put("key", testCaseKey);
execution.set("testCase", testCase);
executions.add(execution);
}
}
We are going to create a map earlier than Take a look at Execution begins with “testCaseKey” and “testCaseName” after which as soon as the execution is completed we seize the values from take a look at strategies and put them into the map together with the execution outcomes standing.
Now, as soon as we now have the information we’d like, we will format the information within the anticipated format of API.
@Override
public void testPlanExecutionFinished(TestPlan testPlan)
{
if (postResults) {
root.put("version", 1);
root.set("executions", executions);
writeResultsToJsonFile();
strive {
zipJsonFile();
} catch (IOException e) {
throw new RuntimeException("Failed to zip json file", e);
}
}
}
personal void writeResultsToJsonFile() {
strive {
ObjectWriter author = objectMapper.writerWithDefaultPrettyPrinter();
String json = author.writeValueAsString(root);
Recordsdata.writeString(Paths.get("output.json"), json);
} catch (IOException e) {
e.printStackTrace();
}
}
personal void zipJsonFile() throws IOException {
String jsonFilePath = "output.json";
String zipFilePath = "output.zip";
strive (FileOutputStream fos = new FileOutputStream(zipFilePath);
ZipOutputStream zos = new ZipOutputStream(fos);
FileInputStream fis = new FileInputStream(jsonFilePath)) {
ZipEntry zipEntry = new ZipEntry(jsonFilePath);
zos.putNextEntry(zipEntry);
byte[] buffer = new byte[1024];
int size;
whereas ((size = fis.learn(buffer)) >= 0) {
zos.write(buffer, 0, size);
}
zos.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
strive {
Path path = Paths.get(jsonFilePath);
Recordsdata.deleteIfExists(path);
} catch (IOException e) {
e.printStackTrace();
}
We’ve got the Execution outcomes JSON file created after the Take a look at Execution of all strategies is accomplished and the output JSON file has TestCaseky, Take a look at CaseName, and Execution Outcome. We then format the file based mostly on API expectations after which zip the file.
The ultimate step is to ship the file to POST name for outcomes to be posted.
RestAssured.given()
.header("Authorization", System.getProperty("authToken"))
.contentType("multipart/form-data")
.pathParam("projectKey", projectKey)
.multiPart("file", new File(zipFilePath))
.multiPart("testCycle", jsonString)
.submit("/automation/execution/{projectKey}")
.then()
.statusCode(200)
.extract().response()
.then().assertThat().physique("testCycle", hasKey("key"));
With this strategy, we do not have to take care of or create a brand new pipeline or script maintained outdoors the repo to submit Execution Outcomes from the CI pipeline to the Take a look at Case Administration system for traceability. All of that is case of by the Take a look at Listeners.
Necessary Notice
Customized Listener must be registered for us to make use of it with our framework.
Create a Listener file within the following location and register the Customized Listener.
Specify your Listener within the File as proven beneath:
That is all. You’re good to go.
Glad coding!