Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to run a TestNG with Gradle

Tags:

gradle

testng

I have a simple code which runs with TestNG, but I am unable to run the same with Gradle, as it says no main method is found, which is, well, not surprising since I am using annotations.

But in such a scenario, how to run the code if I must use Gradle.

Kindly note, I am very new to Gradle, and do not harbour much knowledge about the same.

Code:

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class tryTestNG
{
    @BeforeClass
    public void setup()
    {
        System.out.println("I am in Setup");
    }

    @Test
    public void test()
    {
        System.out.println("I am in Test");
    }

    @AfterClass
    public void tearDown()
    {
        System.out.println("I am in tearDown");
    }
}

The above code runs perfectly with TestNG Library. However not with Gradle.

Here is my Gradle Build setup:

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'tryTestNG'
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'

repositories {
    mavenCentral()
}

test {
    useTestNG()
}


dependencies {
    compile group: 'org.testng', name: 'testng', version: '6.9.10'        
}

The Gradle returns that there is no Main Method.

Working Directory: /home/avirup/MyWorkspace/JavaWorkspace/TestNGGradle
Gradle User Home: /home/avirup/.gradle
Gradle Distribution: Gradle wrapper from target build
Gradle Version: 2.9
Java Home: /usr/lib/jvm/java-8-oracle
JVM Arguments: None
Program Arguments: None
Gradle Tasks: run

:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:runError: Main method not found in class tryTestNG, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/usr/lib/jvm/java-8-oracle/bin/java'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 0.514 secs

Thanks for your help.

like image 969
Avirup Das Avatar asked Sep 03 '16 08:09

Avirup Das


People also ask

Does Gradle run all unit tests when running integrationtest?

then, when I run gradle integrationTest, gradle execute successfully all my integration tests. But when I run gradle test, gragle do not run any unit test I have. I have even printed the sourceSets.getByName ("test").allSource.asPath and I see all unit tests there .java, but for some reason (that I do not know) gradle do not run any of them.

Does Gradle Clean test -I show the class files?

I was quite surprised to see it does not. The tests are picked up and compiled correctly, so I see the class files. Also I do get a report of the run, but it says 0 tests (expected 2). Running gradle clean test -i gives me the following:

Does Gradle parallelization work with TestNG V13?

With gradle, the parallelization does not work with testng v 6.13 or 6.11. or 6.10. Sorry, something went wrong. @ppatellz, I just have the same issue in v13 so its logical add more info here instead of opening new similar (or maybe same) issue.

How do I add Gradle dependencies to a Maven project?

From the maven repository, you have a gradle tab, Just click Gradle tab, You’ll find your dependency which needs you to add to your build.gradle file in a parent folder. By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes.


1 Answers

There's no need to use application plugin to run tests.

The build.gradle should be:

apply plugin: 'java'

sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'

repositories {
    mavenCentral()
}

test {
    useTestNG()
}


dependencies {
    compile group: 'org.testng', name: 'testng', version: '6.9.10'
}

And the command: gradle test. Also, put tryTestNG under src/test/java and name it with capital letter.

Here is a demo.

Also mind that println statements from tests won't be visible in console. To view them navigate to test report. It's under: <project_dir>/build/reports/tests/index.html.

like image 163
Opal Avatar answered Feb 12 '23 13:02

Opal