1

I would like to add some attributes in the xml report generated by junit tests to add traceability to my tests

Here's what I would like :

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

    }
}

And then to have this pair ("key","value") added to my report.xml like :

<?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>

Any ideas ?

Nicolas
  • 114
  • 1
  • 7

1 Answers1

0

It seems there is no solution available to solve this. However, if you follow this answer:, you can at least capture your writings to the console in an -output.txt file.

Let's take the following test as an example:

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);
  }
}

and configuring your Surefire Plugin as the following:

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

after executing mvn test, you'll get a your.package.TestName-output.txt file inside target/surefire-reports with the following content:

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

You might want to format your console logs in a way you can easily parse/access them afterward.

This should work both for JUnit 4 and JUnit 5 and is rather related to the Surefire Plugin and not the testing framework. If you are using Gradle as the build system, you can find the correct configuration here.

rieckpil
  • 10,470
  • 3
  • 32
  • 56