java Allure 框架:在 TestNG 和 Maven 中使用 @Step 和 @Attachment 注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25163853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Allure Framework: using @Step and @Attachment annotations with TestNG and Maven
提问by JavaCreeper
I am working on a project that uses Allure frameworkwith 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.
我正在开发一个将Allure 框架与 Java、TestNG 和 Maven 结合使用的项目。但是在我的 Java 程序中使用 Allure @Step 和 @Attachment 注释时,我无法生成正确的 XML 文件。任何演示上述注释用法的示例代码都值得赞赏。我正在使用倾城 1.4.0.RC8。
回答by vania-pooh
These annotations are used in the same way with any Java-based test framework.
这些注释的使用方式与任何基于 Java 的测试框架相同。
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.
- 使用步骤逻辑创建具有任何可见性修饰符(公共、私有、受保护)的方法,并使用 @Step 注释对其进行注释。您可以选择在注释属性中指定步骤名称。
- 在测试方法中调用此方法。
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 @Attachmentannotation.
- Call this method inside any test
- 创建具有任何可见性的方法,该方法返回byte[]- 附件内容并使用@Attachment批注对其进行批注。
- 在任何测试中调用此方法
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 @Stepand @Attachmentannotations work you need to correctly enable AspectJ in your configuration. This is usually done via -javaagentJVM argument pointing to aspectj-weaver.jarfile.
为了使@Step和@Attachment注释起作用,您需要在配置中正确启用 AspectJ。这通常通过指向aspectj-weaver.jar文件的-javaagentJVM 参数来完成。
Further reading:
进一步阅读: