Analysis of the technical principles of the technical principles of the ALLURE JAVA Annotations framework in the Java class library
Allure Java Annotations framework is an open source framework for generating beautiful test reports.It can integrate with the test framework (such as Testng and Junit) to provide additional annotations for the test method to help generate detailed test reports.
The technical principles of the Allure Java Annotations framework mainly involve two aspects: the listener of the test framework and the collection of test results.
1. Test the listener of the frame:
When the Allure Java Annotations framework and the test framework are integrated, a special listener will be registered.This listener can capture various events in the test, such as the start of the test, the end of the test, the failure of the test, and so on.The listener sends these events to the ALLURE JAVA Annotations framework.
2. Collection of test results:
Allure Java Annotations framework collects the execution information of the test method based on events sent by the monitor.It collects information about the name, description, execution time, parameters of the test method, and stores them in the data structure in memory.
In addition, Allure Java Annotations framework also supports the use of additional annotations to customize test reports.For example,@Title annotations can add custom titles to the test method.@Tag annotation can add labels to the test method,@STEP annotations can add steps to the test method, and so on.These annotations can further enrich the content and readability of the test report.
The following is an example of using Allure Java Annotations framework:
import org.testng.annotations.Test;
import io.qameta.allure.Allure;
import io.qameta.allure.Attachment;
import io.qameta.allure.Description;
import io.qameta.allure.Epic;
import io.qameta.allure.Feature;
import io.qameta.allure.Flaky;
import io.qameta.allure.Issue;
import io.qameta.allure.Link;
import io.qameta.allure.Owner;
import io.qameta.allure.Severity;
import io.qameta.allure.SeverityLevel;
import io.qameta.allure.Step;
import io.qameta.allure.Stories;
import io.qameta.allure.Story;
import io.qameta.allure.TmsLink;
import io.qameta.allure.TmsLinks;
import io.qameta.allure.Title;
import io.qameta.allure.model.Status;
import io.qameta.allure.model.StepResult;
public class TestClass {
@Test
@Title("This is a sample test")
@Description("This test demonstrates the usage of Allure annotations")
@Epic("Epic 1")
@Feature("Feature 1")
@Story("Story 1")
@Tag("Tag 1")
@Severity(SeverityLevel.CRITICAL)
@Link("https://example.com")
@Issue("1234")
@TmsLink("4321")
public void sampleTest() {
allureLog("Step 1");
// Perform test actions
allureLog("Step 2");
// Perform more test actions
allureLog("Step 3");
// Perform final test actions
}
@Step("{0}")
private void allureLog(String message) {
Allure.step(message);
}
@Attachment(value = "Sample Attachment", type = "text/plain")
private byte[] attachTextFile() {
return "Sample attachment content".getBytes();
}
@Test
@Flaky
@Title("This is a flaky test")
@Description("This test is expected to fail occasionally due to external dependencies")
public void flakyTest() {
// Perform test actions
}
@Test
@Title("This is a failed test")
public void failedTest() {
throw new AssertionError("Expected failure");
}
}
In the above example, the method of `sampleTest ()` is annotated as the Allure test case, and uses multiple Allure annotations to customize the test report.Among them, `@STEP` Annotation is used to generate test steps in different steps of the test method.
After the test execution is completed, the Allure Java Annotations framework generates beautiful and detailed test reports based on the collected results, including the summary information, execution results, steps, accessories, etc. of the test case.These reports can be generated in multiple formats (such as HTML and XML) and can be easily shared and discussed with team members.
This is the technical principles of the Allure Java Annotations framework.Through the integrated test framework, listening to testing events, and using the annotation to pack the test results, this framework can generate beautiful and useful test reports to help the team better test management and collaboration.