Avirup Das
Avirup Das

Reputation: 311

Unable to run a TestNG with Gradle

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.

Upvotes: 7

Views: 11099

Answers (1)

Opal
Opal

Reputation: 84844

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.

Upvotes: 7

Related Questions