无法使用Gradle运行TestNG

时间:2016-09-03 08:49:42

标签: gradle testng

我有一个与TestNG一起运行的简单代码,但是我无法使用Gradle运行相同的代码,因为它没有找到主要方法,因为我正在使用注释,这并不奇怪。

但在这种情况下,如果必须使用Gradle,如何运行代码。

请注意,我对Gradle很新,并且对此并不了解。

代码:

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");
    }
}

上述代码与TestNG Library完美匹配。但不是Gradle。

这是我的Gradle Build设置:

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'        
}

Gradle返回没有主要方法。

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:7)

没有必要使用application插件来运行测试。

build.gradle应为:

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'
}

命令:gradle test。另外,将tryTestNG放在src/test/java下,并用大写字母命名。

这是demo

还要注意来自测试的println语句在控制台中不可见。要查看它们,请导航到测试报告。它位于:<project_dir>/build/reports/tests/index.html