nilesh
nilesh

Reputation: 14287

Customizing TestNG result xml

I am using TestNG to run my test suites and the tests are kicked off via Jenkins. I am using TestNG plugin in Jenkins. One of our requirement is to add a custom attribute called 'critical' to testng-results.xml

Current testng-result.xml

<testng-results skipped="0" failed="3" total="5" passed="2">
......
</testng-results>

Expected testng-result.xml

 <testng-results skipped="0" failed="3" total="5" passed="2" critical="2">
    ......
 </testng-results>

I tried below in @Test just to see how setAttribute works, but it didn't add any attribute to the test in testng-results.xml (not sure what I am missing, I am running TestNG programatically and not through command line). I am not sure if ITestResult.setAttribute would be useful in my case because I guess it will add attribute at the test level and I need to add attribute at the suite level. I also implemented ITestListener but wasn't successful. Am I missing anything, can someone point me in the right direction?

Edit1

TestNGRunner Class Running TestNG by code

XmlSuite suite = new XmlSuite();
suite.setName("Custom Report");     
XmlTest test = new XmlTest(suite);
test.setName("customreport");
classes = new ArrayList<XmlClass>();
String [] runSuites = {"com.test.Testing"};
for(String asuite:runSuites)
  classes.add(new XmlClass(asuite));
test.setXmlClasses(classes);
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();      
tng.setXmlSuites(suites);           
tng.run();

com.test.Testing class

@Test
public void test() {
  System.out.println("running my test");
  ITestResult result = Reporter.getCurrentTestResult();
  result.setAttribute("critical", 10);
}

Upvotes: 1

Views: 4259

Answers (2)

Soni K
Soni K

Reputation: 166

Maybe set @Test (priority=0), Zero is having highest priority (or critical)

Upvotes: 0

Cedric Beust
Cedric Beust

Reputation: 15608

You should be seeing these attributes in testng-results.xml, although they will be in a section of their own and not as an attribute of the <testng-results> tag.

Upvotes: 1

Related Questions