如何用testng配置gradle,包已经导入但出错? [英] How to configure gradle with testng, package already imported but getting error?

查看:849
本文介绍了如何用testng配置gradle,包已经导入但出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Gradle。我能够通过testng运行我的测试用例,但无法使用eclipse中的Gradle运行。



以下是 build.gradle

  apply plugin:'java'

apply plugin:'eclipse'

group ='selenium.webdriver.qa'

repositories {
//使用'jcenter'解决您的依赖关系。
//你可以在这里声明任何Maven / Ivy /文件库。
jcenter()
}

依赖关系{
//生产代码在编译时使用SLF4J日志记录API
compile'org.slf4j:slf4j -api:1.7.21'

testCompile'junit:junit:4.12'

repositories {
mavenCentral()
}

依赖关系{
testCompile'org.testng:testng:6.9.4'
编译组:'org.seleniumhq.selenium',名称:'selenium-java',版本:'2. + '
编译组:'org.seleniumhq.selenium',名称:'selenium-server',版本:'2. +'
编译组:'junit',名称:'junit',版本: '4. +'
编译组:'org.slf4j',名称:'slf4j-api',版本:'1.7。+'
编译组:'ch.qos.logback',名称: 'logback-core',版本:'1.0。+'
编译组:'ch.qos.logback',名称:'logback-classic',版本:'1.0。+'
}

test {
useTestNG(){
includecom.mca.automation.sc ript.SanityTestScript

}

test.testLogging {
showStandardStreams = true
}

当我运行测试类时。它显示以下错误:

  D:\Project\SanityTestScript.java:4:error:package org.testng not存在
import org.testng.Assert;
^
D:\Project\SanityTestScript.java:5:错误:程序包org.testng.annotations不存在
import org.testng.annotations.AfterTest;
^
D:\Project\SanityTestScript.java:6:错误:程序包org.testng.annotations不存在
import org.testng.annotations.BeforeTest;
^
D:\Project\SanityTestScript.java:7:错误:程序包org.testng.annotations不存在
import org.testng.annotations.Test;
^
D:\Project\SanityTestScript.java:18:错误:找不到符号
@BeforeTest
^
符号:类BeforeTest
位置:class MCA_SanityTestScript
D:\Project\SanityTestScript.java:24:错误:找不到符号
@Test
^
符号:class Test
location :类MCA_SanityTestScript
D:\Project\SanityTestScript.java:51:错误:找不到符号
@AfterTest
^
符号:类AfterTest
位置: class MCA_SanityTestScript
D:\Project\SanityTestScript.java:48:error:can not find symbol
Assert.assertTrue(expEvent.equalsOnActiveUntilDate(actualEvent));
^
符号:变量Assert
位置:class SanityTestScript
8错误
失败

失败:构建失败,出现异常。

*出错:
任务'compileJava'的执行失败。
>编译失败;详细信息请参阅编译器错误输出。

*尝试:
使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项运行以获取更多日志输出。

我无法理解,到底我做错了什么。即使testng包已经导入。使用Eclipse,将testng库添加到项目中。 您的构建在 compileJava 任务。这意味着你在你的主源代码集中的 src / main / java 文件夹中有你的测试类,它使用TestNG。它们应该位于测试源代码集中的 src / test / java 文件夹中。

您已将TestNg库添加到 testCompile 配置,这是正确的。但是这也意味着只有在测试编译和运行时才会将此库添加到类路径中。不用于编译和运行你的主应用程序。你似乎在你的应用程序类中有测试类。


I am newbie towards Gradle. I am able to run my test cases through testng but unable to run with Gradle from eclipse.

Following is the build.gradle

apply plugin: 'java'

apply plugin: 'eclipse'

group = 'selenium.webdriver.qa'

repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // The production code uses the SLF4J logging API at compile time
    compile 'org.slf4j:slf4j-api:1.7.21'

testCompile 'junit:junit:4.12'

repositories {
    mavenCentral()
}

dependencies {
    testCompile 'org.testng:testng:6.9.4'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.+'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-server', version: '2.+'
    compile group: 'junit', name: 'junit', version: '4.+'
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.+'
    compile group: 'ch.qos.logback', name: 'logback-core', version: '1.0.+'
    compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.0.+'
}

test {
    useTestNG(){
        include "com.mca.automation.script.SanityTestScript"
    }
}

test.testLogging {
    showStandardStreams = true
}

When i run the test class. It shows following error:

D:\Project\SanityTestScript.java:4: error: package org.testng does not exist
import org.testng.Assert;
                 ^
D:\Project\SanityTestScript.java:5: error: package org.testng.annotations does not exist
import org.testng.annotations.AfterTest;
                             ^
D:\Project\SanityTestScript.java:6: error: package org.testng.annotations does not exist
import org.testng.annotations.BeforeTest;
                             ^
D:\Project\SanityTestScript.java:7: error: package org.testng.annotations does not exist
import org.testng.annotations.Test;
                             ^
D:\Project\SanityTestScript.java:18: error: cannot find symbol
    @BeforeTest
     ^
  symbol:   class BeforeTest
  location: class MCA_SanityTestScript
D:\Project\SanityTestScript.java:24: error: cannot find symbol
    @Test
     ^
  symbol:   class Test
  location: class MCA_SanityTestScript
D:\Project\SanityTestScript.java:51: error: cannot find symbol
    @AfterTest
     ^
  symbol:   class AfterTest
  location: class MCA_SanityTestScript
D:\Project\SanityTestScript.java:48: error: cannot find symbol
        Assert.assertTrue(expEvent.equalsOnActiveUntilDate(actualEvent));
        ^
  symbol:   variable Assert
  location: class SanityTestScript
8 errors
 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

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

I am not able to understand, exactly what i am doing wrong. Even the testng package is already imported. Using Eclipse, so testng library added to project.

解决方案

Your build fails during compileJava task. This means that you have your test classes, the one which uses TestNG, in you src/main/java folder, in the main sourceset. They should be in src/test/java folder, in test sourceset.

You have added TestNg library to the testCompile configuration, which is correct. But it also means that this library is added to the classpath only for test compilation and running. Not for compilation and running of your main application. And you seem to have test classes among your application classes.

这篇关于如何用testng配置gradle,包已经导入但出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆