question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Listener in multi-test context

See original GitHub issue

Hi,

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:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gjuillotcommented, Sep 6, 2016

I now have

Instantiate new MyListener onStart of MyListener@331c9ba1 onBeforeClass of MyListener@331c9ba1 A1 onBeforeClass of MyListener@331c9ba1 B1

Thank you

0reactions
juherrcommented, Sep 1, 2016

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 the TestRunner but not at SuiteRunner and the TestRunner 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 on onStart 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found