目前,在运行maven构建时,我的所有测试都会运行,并将一个尽是火的报告创建为一个XML日志(称为TEST-my.project.TestApp)。
我已经创建了我自己的自定义注释@Trace(RQ = "requirement“),以便将我的测试链接到它正在验证的某个需求。
我想要的是,当在构建过程中使用Maven运行测试时,可以在在尽职尽责的报告中生成的XML日志中运行,而不是:
<testcase time="0.113" classname="my.project.TestApp" name="FirstTest"/>我应该得到:
<testcase time="0.113" classname="my.project.TestApp" name="FirstTest">
<traceability>requirement it tests</traceability>
</testcase>我的问题是:
如何将上述注释的注释和实现与Maven在构建时使用的JUnit类运行程序挂钩?或者如何将其连接到创建报告的“尽职”插件上?
发布于 2017-02-09 11:58:01
好吧,我成功地做了一些对我很有用的事情:
首先,我做了我的自定义注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) //can use in method only.
public @interface Trace {
public String[] RQ() default "";
}然后我做一个听众:
public class TestLogger extends RunListener {
private static Map<String[], String[]> requirementsMap = new LinkedHashMap<String[], String[]>();
public void testFinished(Description description) {
if (description.getAnnotation(Trace.class) != null){
String[] testDescription = { description.getClassName(), description.getMethodName() };
requirementsMap.put(testDescription, description.getAnnotation(Trace.class).RQ());
}
}
@Override
public void testRunFinished(Result result) throws Exception {
XMLRequirementsReporter writer = new XMLRequirementsReporter();
writer.writeXMLReport(requirementsMap);
super.testRunFinished(result);
}
}然后,我创建了自己的Junit测试运行程序,在其中添加侦听器:
public class TestRunner extends BlockJUnit4ClassRunner
{
public TestRunner(Class<?> klass) throws InitializationError
{
super(klass);
}
@Override
public void run(RunNotifier notifier)
{
notifier.addListener(new TestLogger());
super.run(notifier);
}
}最后,我向pom XML添加以下内容:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory></directory>
<targetPath>${project.build.directory}/surefire-reports</targetPath>
<includes>
<include>TEST-Traceability.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<build>这将在maven构建中生成我自己的xml报告,在该报告中,需求和测试之间有关联,并且可以进一步使用该报表生成HTML报告或其他任何内容。
https://stackoverflow.com/questions/42003715
复制相似问题