Listener in multi-test context
See original GitHub issueHi,
I have a listener which both implements ISuiteListener and IClassListener.
public class MyListener implements ISuiteListener, IClassListener {
public MyListener() {
System.out.println("Instantiate new MyListener");
}
public void onStart(ISuite suite) {
System.out.println("onStart of " + this);
}
public void onBeforeClass(ITestClass testClass) {
System.out.println("onBeforeClass of " + this);
}
public void onTestStart(ITestResult result) {
}
public void onTestSuccess(ITestResult result) {
}
public void onTestFailure(ITestResult result) {
}
public void onTestSkipped(ITestResult result) {
}
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
}
public void onStart(ITestContext context) {
}
public void onFinish(ITestContext context) {
}
public void onFinish(ISuite suite) {
}
public void onAfterClass(ITestClass testClass) {
}
}
and two test classes that use this listener via a common mother class
@Listeners(MyListener.class)
public class CommonTest {
}
public class ATest extends CommonTest {
@Test
public void testA1() {
System.out.println("A1");
}
}
public class BTest extends CommonTest {
@Test
public void testB1() {
System.out.println("B1");
}
}
I run these two classes in separate <test>
<suite name="Suite with two tests">
<test name="ATest">
<classes>
<class name="ATest" />
</classes>
</test>
<test name="BTest">
<classes>
<class name="BTest" />
</classes>
</test>
</suite>
I got the following output
onStart of MyListener@b101344 onBeforeClass of MyListener@1f3f668d onBeforeClass of MyListener@b101344 A1 onBeforeClass of MyListener@1f3f668d onBeforeClass of MyListener@b101344 B1
There’s something illogical to me here: why is onStart called only for one of the intances while both instances have their onBeforeClass called twice.
I am not sure about expected behavior, but the following solutions would sound at least logical
- only one listener is instantiated
- one listener per class, onStart called for each, onBeforeClass only called once per listener
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
TestNG Listeners in Selenium : Types & Examples
Listeners are TestNG annotations that literally “listen” to the events in a script and modify TestNG behaviour accordingly.
Read more >Drivers — Testplan documentation - Read the Docs
Context at any point during the MultiTest startup is defined as the state of the set of drivers that have already started. As...
Read more >Adding multi test suite support to the Eclipse PDE unit ...
TestRunListener since it's testRunStarted() method includes a test suite name parameter but was unable to find any example on how to adapt org....
Read more >TestNG Listeners In Selenium Webdriver For Automated Testing
What are TestNG listeners & types of listeners in TestNG. ... an instance 'context', which contains all the information about the test run....
Read more >Testing an Apache Kafka Integration within a Spring Boot ...
Almost two years have passed since I wrote my first integration test for a Kafka Spring Boot application. It took me a lot...
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 Free
Top 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
I now have
Thank you
I fixed the first problem (too many calls of methods) but it is not yet pull-requested/merged.
To fix the second problem, it will take more time because I think all listener management should be reworked. For example,
@Listeners
is only looked by theTestRunner
but not atSuiteRunner
and theTestRunner
will add appropriate listeners into the suite. In your case, as you have 2<test>
, the listener is instantiated twice. We don’t have twice call ononStart
because it is not expected and some tricks were made.I think runners should only be concerned by their own listeners and pass the others to the lower levels (ie: suite -> test), and not the opposite.