ITestResult Thread Not Used in TestListenerAdapter Extended Classes onTestSuccess/Failure/Skipped
See original GitHub issueSummary
When configuring a setupMethod with ITestResult, annotated with @BeforeMethod, and then populating attributes to be used in correlating the failure in a test method (@Test), the ITestResult thread is not available to the listener class extending the TestListenerAdapter.
Note, this works in version 6.14.0, but fails starting 7.0.0.
TestNG Version
7.3.0
Expected behavior
ITestResult Thread to be populated with attributes assigned in a test method.
Actual behavior
Only the ITestResults populated prior to the @Test method are available onTestSuccess (or onTestFailure or onTestSkipped)
Is the issue reproducible on runner?
- Shell
- Maven
- Gradle
- Ant
- Eclipse
- IntelliJ
- NetBeans
Test case sample
package org.mygroup;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.testng.Assert.assertFalse;
public class TestSample {
private ThreadLocal<ITestResult> tr = new ThreadLocal<>();
TestSample() {
}
@BeforeMethod(alwaysRun = true)
public void setupMethod(ITestResult tr) {
this.tr.set(tr);
tr.setAttribute("field1", "value1");
tr.setAttribute("field2", "value2");
System.out.println("Attribute names size in Setup: " + tr.getAttributeNames().size());
}
@AfterClass(alwaysRun = true)
public void tearDown() {
tr.remove();
}
@Test(groups = {"acceptance"})
public void testInvocation() {
//These values aren't included in the Listener after v7.0.0
tr.get().setAttribute("field3", "value3");
tr.get().setAttribute("field4", "value4");
System.out.println("Attribute names size in test: " + tr.getAttributeNames().size());
assertFalse(true);
}
}
The Listener that reveals only the first two fields are loaded:
package org.mygroup;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
public class Listener extends TestListenerAdapter {
@Override
public void onTestFailure(ITestResult tr) {
getTestResultLogObject(tr);
}
@Override
public void onTestSkipped(ITestResult tr) {
printTestResultAttributes(tr);
}
@Override
public void onTestSuccess(ITestResult tr) {
getTestResultLogObject(tr);
}
void printTestResultAttributes(ITestResult tr) {
System.out.println("Attribute names size in Listener: " + tr.getAttributeNames().size());
System.out.println(getAttributes(tr));
}
private Map<String, String> getAttributes(ITestResult tr) {
Set<String> names = tr.getAttributeNames();
Map<String, String> attributes = new HashMap<>();
for (String name : names) {
Object value = tr.getAttribute(name);
if (null == value) {
attributes.put(name, "null");
}
else {
attributes.put(name, value.toString());
}
}
return attributes;
}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:10
Top Results From Across the Web
TestListenerAdapter - javadoc.io
public class TestListenerAdapter extends java.lang.Object implements IResultListener2. A simple ITestListener adapter that stores all the tests that were ...
Read more >Relating Test Instance Data to TestListenerAdapter's ITestResult
public class TestAnalysisListener extends TestListenerAdapter { ... ITestResult does not know the thread ID used for the test, so how can I find...
Read more >ITestResult is throwing an exception | Selenium Forum
I am getting the following exception when I use the ITestResult ... TestBase; public class TestA extends TestBase { @Test public void ...
Read more >TestNG Listener onFinish(ITestContext context) can't get ...
When I instantiate Chrome driver, I wrap it in a ThreadLocal , so I can use getter and setter when I want to...
Read more >TestNG Listeners in Selenium: ITestListener & ITestResult ...
Types of Listeners in TestNG; Test Scenario: Steps to create a TestNG Listener; Use of Listener for multiple classes.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@egglestonbd - Please also note that, TestNG by default would ensure that even a
beforeInvocation()
andafterInvocation()
listener call happens on the same thread. Here’s a cleaned up version of your same test code (This is using TestNG7.3.0
)And here’s the output
Closing this issue as requested.
Hi,
Could you try without the
ThreadLocal
and passing theITestResult
as test param?testInvocation(ITestResult tr)
?