testng-team / testng

TestNG testing framework
https://testng.org
Apache License 2.0
1.99k stars 1.02k forks source link

ISuiteListener.onStart method called mulitple times if i have multiple test elements in xml configuration file #171

Closed chanjarster closed 9 years ago

chanjarster commented 12 years ago

Hi

I'm using testng 6.3.1. I got a weird result if i declare a @Listeners, which define my implementation of ISuiteListener, on a base class and define multiple test elements, each of which include only one test class inherited from that base class, in the xml configration file.

Here is my testng.xml file:

<suite name="suite" verbose="1">
    <test name="test1">
        <classes>
            <class name="org.beangle.webtest.temp.TestA" />
        </classes>
    </test>
    <test name="test2">
        <classes>
            <class name="org.beangle.webtest.temp.TestB" />
        </classes>
    </test>
</suite>

My implementation of ISuiteListener

public class SuiteListener implements ISuiteListener {

    @Override
    public void onFinish(ISuite suite) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStart(ISuite suite) {
        System.out.println("*******Suite Listener Start");
        System.out.println(":::::::suite detected: " + suite.getName());
        for(XmlTest xmlTest : suite.getXmlSuite().getTests()) {
            System.out.println(":::::::test  detected: " + xmlTest.getName());
            for(XmlClass xmlClass : xmlTest.getClasses()) {
                System.out.println(":::::::class detected: " + xmlClass.getName());
            }
        }
        System.out.println("*******Suite Listener End\n");
    }
}

TestBase.java

@Listeners(SuiteListener.class)
public class TestBase {
}

TestA.java

public class TestA extends TestBase {
    @Test
    public void test() {
    }
}

TestB.java

public class TestB extends TestBase {
    @Test
    public void test() {
    }
}

After I run the test, the console output is

*******Suite Listener Start
:::::::suite detected: suite
:::::::test  detected: test1
:::::::class detected: org.beangle.webtest.temp.TestA
:::::::test  detected: test2
:::::::class detected: org.beangle.webtest.temp.TestB
*******Suite Listener End

*******Suite Listener Start
:::::::suite detected: suite
:::::::test  detected: test1
:::::::class detected: org.beangle.webtest.temp.TestA
:::::::test  detected: test2
:::::::class detected: org.beangle.webtest.temp.TestB
*******Suite Listener End

I am confused about the result. SuiteListener.onStart method is called twice, is that the expected result ?

Can I make SuiteListener.onStart method called only once?

Thanks in advance.

agogs commented 10 years ago

This is because of multiple <test> tags in your xml, I gues this is a bug in testng, I too observed it and reported here, but not reply yet.

cosyman commented 9 years ago

Yes I met the same problem, I tested it in testng 6.8.8 and testng 6.8.17。 No only that.

If I have multiple suite files,I declare my custom IReporter or IExecutionListener in maven-surefire-plugin like

<artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <suiteXmlFiles>
            <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
            <suiteXmlFile>src/test/resources/testng2.xml</suiteXmlFile>
        </suiteXmlFiles>
        <properties>
            <property>
            <name>listener</name>
                          <value>com.ctrip.cap.reporter.MyReporter,com.ctrip.cap.reporter.MyReporter</value>
            </property>
        </properties>
       </configuration>

or I use @ Anotation Listeners in super class, or I use testng's xml configuration in testng.xml and testng2.xml like

    <listeners>
        <listener class-name="com.ctrip.cap.internal.suite.MyExecutionListener " />
        <listener class-name="com.ctrip.cap.reporter.MyReporter" />
    </listeners>

They all called multiple times.

My Question is that why not make Listener's Class Container as a Set,and no matter how to register it,The Listener of the same Class and same type should be called once time.

Abhijitdatta commented 4 years ago

I am facing the same issue . In my scenario, there is only one testcase in single class. I am using Testing version 7.1.0 and I am facing the issue. I have tried testing version below 6.10 but facing the same. Kindly help.

krmahadevan commented 4 years ago

@Abhijitdatta can you show us a complete sample that can be used to reproduce this problem ?