Custom field in JUnit and TestNG Report

459 views
Skip to first unread message

Ashish S

unread,
Feb 4, 2022, 4:57:14 AM2/4/22
to testng-users
Hi All,

Need one help to figure out custom field addition in JUnit & TestNG Report. Please share how we can achieve it.

For example, If i have a Test which is annotated with some information e.g. Author
@Test
@Author(id="unique")
public void simpleTest(){

}


// I am looking for possibility of generation of JUnit & TestNG reports both as below... i.e with additional Information as highlighted in red.

<testsuite ....>
  <testcase classname="XXX" name="XXX" author="unique" time="XXX"/>
  <system-out/>
</testsuite>

Thanks,
Ashish




⇜Krishnan Mahadevan⇝

unread,
Feb 7, 2022, 12:00:18 AM2/7/22
to testng-users
Ashish,

What you are asking for is only available partially in TestNG. 

TestNG has an attribute on the "@Test" annotation using which you can pass in any amount of custom attributes (You don't need any custom annotation from your own side)

Here's a sample

import org.testng.annotations.CustomAttribute;
import org.testng.annotations.Test;

public class SampleTestClass {

  @Test(attributes = {
      @CustomAttribute(name = "name", values = "dragon-warrior"),
      @CustomAttribute(name = "skill-set", values = {"kung-fu", "eating-dumplings"})
  })
  public void sampleTestMethod() {
    System.err.println("Hello world test case ran");
  }
}

AFAIK, TestNG doesn't read these attributes in its own default reports (That's strange. We should have honoured them. Maybe we can do that in the upcoming version of TestNG as part of this new issue that I have just now created https://github.com/cbeust/testng/issues/2725)

So for you to be able to see them in the JUnit reports, you would need to build your own customisation around it by extending one of the following

org.testng.reporters.EmailableReporter2 - This is what builds the emailable-report
org.testng.reporters.JUnitReportReporter - This is what generates the junit reports

I don't know if it would make sense for you to add these attributes into the JUnit reports, because I think the JUnit reports follow a schema and if the schema doesn't have a provision for this, there's a very good chance that all tools (Jenkins for e.g., ) which use a schema to parse JUnit reports would ignore rendering these custom attributes.

I got to know about the schema part in which I was building a JUnit report https://github.com/RationaleEmotions/junitreport for TestNG.

Hope that helps!

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/a14f346f-9859-4536-8f65-5f17ca4b4a04n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages