Using Selenium, TestNG and ReportNG with Gradle

Lidia picture Lidia · Apr 19, 2012 · Viewed 7.3k times · Source

Has anybody here successfully configured reportng with gradle 1.0-milestone-9?

I downloaded a Selenium 2, TestNG sample developed with Gradle (from https://github.com/iainrose/page-objects). It works nicely (after adding System Properties to build.gradle for switching browsers) and now I would like to add ReportNG to the mix. I tried the following:

dependencies {
compile "org.seleniumhq.selenium:selenium-java:2.15.0"
compile "org.testng:testng:6.3.1"
compile "org.uncommons:reportng:1.1.2"
}

It produces the following error:

Could not resolve all dependencies for configuration ':compile'.
> Artifact 'org.testng:testng:6.3.1:jdk15@jar' not found.

I found a similar issue at http://issues.gradle.org/browse/GRADLE-2042 and added a comment there, however, there's been no follow up yet. The only suggestion there is to remove reportng from dependencies... I'm hoping someone here knows of a way to get reportng working with gradle.

After adding code suggested by Szpak below:

After adding code to build.gradle as suggested by Szpak I could finally run the tests - still without the reportng listeners but with the dependency in place. However, once I plugged in listeners, as in:

    useTestNG() {
    options {
        listeners << 'org.uncommons.reportng.HTMLReporter'
        listeners << 'org.uncommons.reportng.JUnitXMLReporter'
    }

I got java.lang.StackOverflowError:

A problem occurred evaluating root project 'console-bg1'.
> java.lang.StackOverflowError (no error message)

* Exception is:
org.gradle.api.GradleScriptException: A problem occurred evaluating root project 'console-bg1'.
at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:54)

(...)

Caused by: java.lang.StackOverflowError
at sun.reflect.GeneratedMethodAccessor12.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaClassImpl.invokeMissingMethod(MetaClassImpl.java:804)
at groovy.lang.MetaClassImpl.invokePropertyOrMissing(MetaClassImpl.java:1096)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1049)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuperN(ScriptBytecodeAdapter.java:128)
at org.gradle.api.tasks.testing.testng.TestNGOptions.methodMissing(TestNGOptions.groovy:204)

From my research online it looks like something is wrong with the dependencies. I am still unable to use reportng with gradle...

Answer

Marcin Zajączkowski picture Marcin Zajączkowski · Apr 20, 2012

As a workaround you can exclude testng-5.0-jdk15 transitive dependency from reportng package.

dependencies {
    compile "org.seleniumhq.selenium:selenium-java:2.15.0"
    compile "org.testng:testng:6.3.1"
    compile("org.uncommons:reportng:1.1.2") {
        exclude group: "org.testng", module: "testng"
    }
    compile "com.google.inject:guice:3.0"
}

Btw, testCompile usually is a better choice for test dependencies.

Update: Added guice dependency to avoid "ClassNotFoundException: com.google.inject.Module".