Julien,
I gave this a try to check if something had changed in TestNG recently. It doesn't seem to work when it comes to trying to see the results in a built-in reporter.
If I had a test class as below :
public class DataProviderSample implements ITest {
private static ThreadLocal<String> testNames = new ThreadLocal<String>();
@Test (dataProvider = "dp")
public void testMethod(int number, String text) {
String txt = "Thread Id [" + Thread.currentThread().getId() + "] with value (" + number + "," + text + ")";
testNames.set(txt);
}
@DataProvider (name = "dp", parallel = true)
public Object[][] getData() {
return new Object[][] {
{1, "Poetry"},
{2, "Novels"},
{3, "TextBooks"}
};
}
@Override
public String getTestName() {
return testNames.get();
}
}
and a suite file as below
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Class1Suite" parallel="methods" verbose="2">
<test name="SampleTest">
<classes>
<class name="organized.chaos.forums.DataProviderSample"/>
</classes>
</test>
</suite>
I initially trigger a NullPointerException because my getTestName() implementation attempts at invoking a ThreadLocal value, but since the Reporters are perhaps running in a different thread, they don't have any value set as a testname within testNames ThreadLocal variable. [ only way to get past the problem is to define a initialValue() implementation for the ThreadLocal variable and provide with a default value.
private static ThreadLocal<String> testNames = new ThreadLocal<String>(){
@Override
protected String initialValue() {
return "Unknown";
}
};
I believe that for one to be able to achieve this requirement from the OP, one would need to build a custom reporter which would perhaps extract an attribute named "testName" from a method's ITestResult object and then construct the test report. One could maybe build an implementation of org.testng.IInvokedMethodListener listener and within its beforeInvocation() one could set this attribute for a test method.
Atleast that was the only way I saw in which one can do this.
Please let me know if I missed something anywhere.