如何在junit测试的xml报告中添加属性



我想在junit测试生成的xml报告中添加一些属性,为我的测试添加可跟踪性

以下是我想要的:

class MyTest{
@Test
@AddAttribute("key","value")
fun myTest()
{
}
}

然后将这对("key"、"value"(添加到我的report.xml中,如:

<?xml version='1.0' encoding='UTF-8' ?>
<testsuite name="MyTests" tests="1" failures="0" errors="0" skipped="0" time="20.846" timestamp="2020-07-07T15:07:57" hostname="localhost">
<properties>
<property name="project" value="app" />
</properties>
<testcase name="MyTest" classname="MyTest" time="12.912" key="value" />
</testsuite>

有什么想法吗?

似乎没有解决方案。但是,如果您遵循以下答案:,那么您至少可以在-output.txt文件中捕获您的文章到控制台。

让我们以下面的测试为例:

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestReportingTest {
@Test
public void testOutput() {
System.out.println("Key One: Value");
System.out.println("Key Two: 1,2,3,4,5");
assertEquals(4, 2 + 2);
}
}

并将Surefire插件配置如下:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</plugin>

执行mvn test后,您将在target/surefire-reports中获得一个your.package.TestName-output.txt文件,其中包含以下内容:

Key One: Value
Key Two: 1,2,3,4,5

您可能希望格式化控制台日志,以便以后可以轻松地解析/访问它们。

这应该适用于JUnit4和JUnit5,并且与Surefire插件而不是测试框架有关。如果您使用Gradle作为构建系统,您可以在这里找到正确的配置。

最新更新