如何从junit测试的XML报告中添加属性?

时间:2020-07-07 15:53:26

标签: android xml kotlin testing junit

我想在junit测试生成的xml报告中添加一些属性,以增加对测试的可追溯性

这就是我想要的:

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

    }
}

然后将这对(“键”,“值”)添加到我的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>

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

似乎没有解决方案可解决此问题。但是,如果遵循此answer :,则至少可以在-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后,您将在your.package.TestName-output.txt中获得一个target/surefire-reports文件,其内容如下:

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

您可能希望以一种易于以后解析/访问它们的方式来格式化控制台日志。

这对于JUnit 4和JUnit 5都应该起作用,并且与Surefire插件而不是测试框架有关。如果使用Gradle作为构建系统,则可以找到正确的配置here