Allure Annotations
Allure Annotations
Allure annotations are used in the Allure framework, which is a flexible test reporting tool often used in conjunction with testing frameworks like TestNG, JUnit, and others. The Allure framework helps generate clean, visual reports for your tests. Here are some commonly used Allure annotations:
@Test: Marks a test method to be included in the report.
Examples
@Test
public void myTest() {
// test code
}
@Step: Used to describe steps in a test. These steps will be displayed in the report.
Example:
@Step("Open the homepage")
public void openHomePage() {
// method code
}
@Attachment: Attaches files or data to the report. This can be used to attach screenshots, logs, etc.
Example:
@Attachment
public byte[] saveScreenshot(byte[] screenshot) {
return screenshot;
}
@Feature: Used to mark the feature being tested. It helps categorize the test case in the report.
Example:
@Feature("Login")
@Test
public void loginTest() {
// test code
}
@Story: Describes a particular story (e.g., a user story). It helps break down tests into smaller, more specific parts.
Example:
@Story("As a user, I want to log in to my account")
@Test
public void loginStory() {
// test code
}
@Severity: Specifies the severity of a test. It is used to categorize tests by their importance (e.g., critical, normal, minor).
Example:
@Severity(SeverityLevel.CRITICAL)
@Test
public void criticalTest() {
// test code
}
@Link: Used to link to external resources (e.g., bug tracking, Jira) directly from the test report.
Example:
@Link("http://bugzilla.com/issue/TEST-123")
@Test
public void testWithLink() {
// test code
}
@Description: Provides a description for the test, which will appear in the Allure report.
Example:
@Description("This test verifies the user login functionality.")
@Test
public void userLoginTest() {
// test code
}
@BeforeAll and @AfterAll: Used for setup and teardown operations for the entire test class or suite.
Example:
@BeforeAll
public static void setUp() {
// code for setup before all tests
}
@AfterAll
public static void tearDown() {
// code for teardown after all tests
}
Example with Allure Annotations:
import io.qameta.allure.*;
import org.junit.jupiter.api.Test;
public class LoginTest {
@Test
@Description("This test verifies the user login functionality.")
@Feature("Login")
@Story("As a user, I want to log in to my account")
@Severity(SeverityLevel.CRITICAL)
public void testLogin() {
// Test code for login
openHomePage();
loginUser("username", "password");
verifyLoginSuccess();
}
@Step("Open the homepage")
public void openHomePage() {
// Code to open the homepage
}
@Step("Login with username {0} and password {1}")
public void loginUser(String username, String password) {
// Code to log in
}
@Step("Verify successful login")
public void verifyLoginSuccess() {
// Code to verify login success
}
}
This will create a well-structured report showing all the steps with detailed descriptions, severity, and links if necessary. You can also capture attachments like screenshots or logs for additional context.