Allure Framework: using @Step and @Attachment anno

2019-07-17 08:07发布

问题:

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.

回答1:

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:

  • Using with TestNG
  • Steps
  • Attachments