Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize Junit 4 XML report

Background:

TestNG supports adding your own Reporter classes in order to modify the reports being generated or generating new reports as needed.

However, JUnit doesn't have such a functionality, so a brute-force way would be to write your own Runner and then generate your own custom report.

But, I ask this question in order to find if there is something better?

Basically, I want to add custom attribute to every executed method.

<testcase name="test_test_something" classname="some.class.name" time="0.069" my-own-attribute="somevalue"/>

So my question is:

  • How is this XML report generated by JUnit and Gradle?

  • Is there a way to modify this process of report generation to add custom data to the report while doing minimal changes?

like image 301
Kumar Shubham Avatar asked Jan 23 '19 08:01

Kumar Shubham


People also ask

What is JUnit xml format?

JUnit xml is a framework that was used in many applications in the test frameworks. By default, the test will generate xml files which are simple reports used for the execution of the test. These files are used to generate the report which was custom, we can also generate the reports as per the requirement of testing.

How do I run a JUnit test from xml?

Create JUnit Test Case Class Create a java class, which is a JUnit test class, TestJunit. java in /work/testng/src. To execute the JUnit test cases, define the property junit="true" as in the xml above. The JUnit test case class TestJunit is defined in class name.

Can we generate report using JUnit?

By default, JUnit tests generate simple report XML files for its test execution. These XML files can then be used to generate any custom reports as per the testing requirement. We can also generate HTML reports using the XML files.


1 Answers

How is this XML report generated by JUnit and Gradle?

It’s eventually generated by the Gradle internal class org.gradle.api.internal.tasks.testing.junit.result.JUnitXmlResultWriter.

Basically, I want to add custom attribute to every executed method.

<testcase name="test_test_something" classname="some.class.name" time="0.069" my-own-attribute="somevalue"/>

[…] Is there a way to modify this process of report generation to add custom data to the report while doing minimal changes?

Unfortunately, there is no way to add further attributes to the <testcase/> element. This code shows how the element and its attributes are currently created; there is no way to hook into that creation process.


If you can live with a hacky solution, then you could try writing your custom data to StdOut/StdErr during the test and set the outputPerTestCase property as follows:

// (configuring the default `test` task of the `java` plugin here; works with
// any task of Gradle’s `Test` type, though)
test {
    reports {
        junitXml {
            outputPerTestCase = true
        }
    }
}

The written output will then end up at least somewhere within the <testcase/> element and you might be able use it from there somehow.

like image 74
Chriki Avatar answered Oct 12 '22 05:10

Chriki