Allure Framework: using @Step and @Attachment annotations with TestNG and Maven

怎甘沉沦 提交于 2019-12-24 04:31:35

问题


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


来源:https://stackoverflow.com/questions/25163853/allure-framework-using-step-and-attachment-annotations-with-testng-and-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!