Allure Framework: using @Step and @Attachment annotations with TestNG and Maven
21,050
These annotations are used in the same way with any Java-based test framework.
To create a step:
- Create method with any visibility modifier (public, private, protected) with step logic and annotate it with @Step annotation. You can optionally specify step name in annotation attributes.
- Call this method inside test method.
An example:
@Test
public void someTest() throws Exception {
//Some code...
stepLogic();
//Some more assertions...
}
@Step("This is step 1")
private void step1Logic() {
// Step1 implementation
}
@Step("This is step 2")
private void step2Logic() {
// Step2 implementation
}
To create an attachment:
- Create method with any visibility which return byte[] - attachment contents and annotate it with @Attachment annotation.
- Call this method inside any test
Example:
@Test
public void someTest() throws Exception {
//Some code...
createAttachment();
//Some more assertions...
}
@Attachment(name = "My cool attachment")
private byte[] createAttachment() {
String content = "attachmentContent";
return content.getBytes();
}
In order to make @Step and @Attachment annotations work you need to correctly enable AspectJ in your configuration. This is usually done via -javaagent JVM argument pointing to aspectj-weaver.jar file.
Further reading:
Author by
JavaCreeper
Updated on July 09, 2022Comments
-
JavaCreeper almost 2 years
I am working on a project that uses Allure framework with Java, TestNG and Maven. But I'm unable to generate correct XML files while using Allure @Step and @Attachment annotations in my Java program. Any sample code demonstrating usage of the above annotations is appreciated. I am using Allure 1.4.0.RC8.
-
Lauri about 2 yearsdo you have any suggestions how i would add aspectj-weaver.jar as javaagent if i'm invoking testng test suite inside spring-boot application. (not as tests for those application but at runtime to rerun some end2end tests when user so pleases.