How to configure gradle with testng, package already imported but getting error?

心已入冬 提交于 2020-01-03 05:15:09

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/39031758/how-to-configure-gradle-with-testng-package-already-imported-but-getting-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!