简体   繁体   中英

Running tests from JAR dependency

I'm working on creating a JAR with my test code and planning to run the test from a different project.

Below is the approach I'm trying to implement.

Project B:

configurations { testApi }
task testJar ....
artifacts { testApi testJar }

Project A:

dependencies {
    testRuntime "xx.xxx.xxx.projectName", configuration: "integtest";
}

The test seems to be not running with this approach. Any idea on what could be the issue? Any better approaches?

Gradle only looks for test classes in the directories configured via testClassesDirs property of the respective Test -type task which with the java plugin is project.sourceSets.test.output.classesDirs for the test task. Running tests from JAR files in dependencies is not supported.

You need to extract the JAR file and add the extracted directory to this property. I think something like the following construct should work, I didn't test it though:

configurations {
    externalTests
}
dependencies {
    externalTests "xx.xxx.xxx.projectName", configuration: "integtest"
    testRuntime "xx.xxx.xxx.projectName", configuration: "integtest"
}
test {
    // if only one dependency in externalTests you can use the simpler, it will fail if there are multiple dependencies
    testClassesDirs += zipTree(configurations.externalTests.singleFile)
    // if multiple dependencies in externalTests you need to use
    testClassesDirs += configurations.externalTests.files.collect { zipTree it }.sum()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM