testng-team / testng

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

IAnnotationTransformer called multiple time Discrepancy between 6.x and 7.x #2312

Closed baubakg closed 4 years ago

baubakg commented 4 years ago

TestNG Version

7.0 & 7.1

Expected behavior

IAnnotationTransformer called once per method. (version 6.X)

Actual behavior

IAnnotationTransformer called 15 times per method. (version > 7.X)

Is the issue reproductible on runner?

Test case sample

Create a retry listener. Create one test using the listener.

Here is my listener:

public class RetryListener implements IAnnotationTransformer {
    protected static Logger log = LogManager.getLogger();

    private static int executedNrOfTimes = 0;

    public static void reset() {
        executedNrOfTimes = 0;
    }

    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor,
            Method testMethod) {

        log.debug("Setting Retry for " + testMethod.getDeclaringClass().getTypeName() + "."
                + testMethod.getName());

        //    ----  6.X    
        IRetryAnalyzer retry = annotation.getRetryAnalyzer();

        annotation.setRetryAnalyzer(Retry.class);

        executedNrOfTimes++;

        /*  //    ----  7.X
        Class<? extends IRetryAnalyzer> retry = annotation.getRetryAnalyzerClass();

        if (retry.equals(DisabledRetryAnalyzer.class)) {
            annotation.setRetryAnalyzer(Retry.class);
        }*/

    }

    public static int getEexecutedNrOfTimes() {
        return executedNrOfTimes;
    }

}

Here is my test

    @Test
    public void testHellowWorldRetryBug() {

        // Rampup
        TestNG myTestNG = new TestNG();
        TestListenerAdapter tla = new TestListenerAdapter();
        myTestNG.addListener((ITestNGListener) tla);

        XmlSuite mySuite = new XmlSuite();
        mySuite.setName("Automated Suite Retry");
        List<XmlSuite> mySuites = new ArrayList<XmlSuite>();
        mySuites.add(mySuite);
        // Set the list of Suites to the testNG object you created earlier.
        myTestNG.setXmlSuites(mySuites);

        // Add listeners
        mySuite.addListener(
                "com.my.tests.RetryListener");

        // Create an instance of XmlTest and assign a name for it.
        XmlTest myTest = TestTools.attachTestToSuite(mySuite,
                "Test Retry Hello World");

        // Add package to test
        List<XmlPackage> l_packages = new ArrayList<>();
        l_packages.add(
                new XmlPackage("com.my.tests.data"));
        myTest.setXmlPackages(l_packages);

        TestReExecutorAndFilter.resetSeed();

        myTestNG.run();

        assertThat("We should have 1 skipped test",
                tla.getSkippedTests().size(), is(equalTo(1)));
        assertThat("We should have 1 successful method",
                tla.getPassedTests().size(), is(equalTo(1)));
        assertThat("We should have no failed methods",
                tla.getFailedTests().size(), is(equalTo(0)));

        assertThat("RetryAdpaper should only be called once", RetryListener.getEexecutedNrOfTimes(),is(equalTo(1)));

    }

I was expecting the Listener to be executed once per test. Also I have no idea about why it is called 15 times.

amitbhoraniya commented 4 years ago

@baubakg It looks like fix of https://github.com/cbeust/testng/issues/1790 has introduced your issue.

baubakg commented 4 years ago

Thanks. Can't wait to test it :)