Sebastian Höffner
Sebastian Höffner

Reputation: 1944

TestNG Listeners: working with XML, failing with Annotation

I am trying to add a Listener to my TestNG test class.

The Listener MyListener is:

package somepackage.util;
import org.testng.ITestContext;
import org.testng.TestListenerAdapter;

public class MyListener extends TestListenerAdapter {
    @Override
    public void onFinish(ITestContext testContent) {
        System.out.println("onFinish");
    }
}

For the test class SimpleTest I have:

package somepackage;

import somepackage.util.MyListener;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners({ MyListener.class })
public class SimpleTest {
    @Test
    public void test() {
        System.out.println("Running test...");
        Assert.assertTrue(true);
    }
}

And the XML is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="SomeTests">
    <listeners>
        <!--listener class-name="somepackage.util.MyListener" /-->
    </listeners>
    <test name="SimpleTest">
        <classes>
            <class name="somepackage.SimpleTest" />
        </classes>
    </test>
</suite>

The output is (lots of path things cut):

<snip>
[Parser] Running:
   <snip>/test.xml
Running test...

===============================================
SomeTests
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Process finished with exit code 0

And when I change the comment in the XML to be an actual element:

<listener class-name="somepackage.util.MyListener" />

It works:

<snip>
[Parser] Running:
   <snip>/test.xml
Running test...
onFinish

===============================================
SomeTests
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Process finished with exit code 0

Any ideas why the Annotation gets ignored while the XML works fine? It would be ok for me to work with the XML, but I would like to have the ability to use annotations as well.

In case it matters I am running the tests with IntelliJ IDEA 14 and Java 7.

Upvotes: 3

Views: 3684

Answers (2)

Ran Adler
Ran Adler

Reputation: 3709

Example of take screen shot when test fails with web driver

public class DriverEventListener extends TestListenerAdapter {


    @Override
    public void onTestFailure(ITestResult testResult) {
        if(testResult.getInstance() instanceof  AutomationSupport){
        AutomationSupport instance = (AutomationSupport) testResult.getInstance();
        Driver driver = instance.getDriver();
        File scrFile = ((TakesScreenshot) driver.getDriver()).getScreenshotAs(OutputType.FILE);
        String name = testResult.getMethod().getMethodName()+"__"+System.currentTimeMillis();
        try {
            FileUtils.copyFile(scrFile, new File("\\\\127.0.0.1\\MetadorScreenShot\\\\"+name+".jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        }

    }

    @Override
    public void onTestSuccess(ITestResult testResult) {
        super.onTestSuccess(testResult);
    }



}

Upvotes: 0

Sebastian H&#246;ffner
Sebastian H&#246;ffner

Reputation: 1944

The problem is a dependency issue. In my pom.xml I had a dependency on some classes which itself depended on TestNG 5.11. This worked pretty good until I figured out that I was not able to use the @Listeners annotation.

With the command

mvn dependency:tree

I was able to figure out that I had the wrong dependencies. To get rid of them I had to change the pom.xml around the artifact dependent on TestNG 5.11:

<dependency>
    <groupId>some.group.id</groupId>
    <artifactId>the.artifact.id</artifactId>
    <vers‌​ion>1.0</version>
    <exclusions>
        <exclusion>
            <groupId>org.testng</groupId>
            <artifactId>‌​testng</artifactId>
        </exclusion>
    </exclusions>
</dependency>

After that the correct TestNG dependency was working fine and I was able to use my Listener.

On a side note: The behavior of when e.g. retryAnalyzers and Listeners are called changed dramatically over the versions from 5.11 to 6.9.6, and I guess it did so quite a few times. There are for example many questions on STO dealing with "how to fix reports for retried tests which eventually pass" - none of those approaches I found worked fine for me, so I figured out one myself. This is not exactly related to the problem, but I just want you to make sure that you double check if your listeners and analyzers are called as expected and work as intended after fixing the dependencies.

Upvotes: 2

Related Questions