【问题标题】:How to add attributes in xml report from junit test?如何在 junit 测试的 xml 报告中添加属性?
【发布时间】:2020-07-07 15:53:26
【问题描述】:

我想在 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>

有什么想法吗?

【问题讨论】:

    标签: android xml kotlin testing junit


    【解决方案1】:

    似乎没有可用的解决方案来解决这个问题。但是,如果您关注此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后,你会在target/surefire-reports里面得到一个your.package.TestName-output.txt文件,内容如下:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 2021-09-27
      相关资源
      最近更新 更多