mercredi 1 juillet 2020

Share Java test helpers classes across different kinds of test modules in Gradle project

I have two kinds of tests: unit and integration tests. I would like to have another one in which I would like to place more complex test cases. Each kind of test is placed in different module.

Bellow I am placing a piece of project structure to make it a little bit more visible

...
---buildSrc
------build.gradle(1)
------gradle.properties
---src
------main (files with production code)
------integration-test
------test (files with unit tests)
------new-test
------build.gradle(2)
...

My goal: I have already created plenty of test helpers which are placed in integration-test module and I would like to reuse them in new-test module.

This is how looks like my sourceSets in build.gradle(2) file:

sourceSets {
    integrationTest {
        java.srcDir file('src/integration-test/java')
        resources.srcDir file('src/integration-test/resources')
        compileClasspath += main.output + test.output
        runtimeClasspath += main.output + test.output
    }
    newTest {
        java {
            srcDirs 'src/new-test/java'
            srcDirs 'src/integration-test/java'
        }
        resources.srcDir file('/src/new-test/resources')
        compileClasspath += main.output + test.output + integrationTest.output
        runtimeClasspath += main.output + test.output + integrationTest.output
    }
}

And thanks for that (srcDirs 'src/integration-test/java') I am able to use sources from integration-test in new-test module but I have serious doubts if it is a proper way to do that because I am getting a communicate from Intellij:

Path [C:/Repositories/my-project/project/src/integration-test/java] of module [project.newTest] was removed from modules [project.integrationTest]

This is my newTest task from build.gradle(2) file:

task newTest(type: Test) {
    useJUnitPlatform() {
        excludeEngines "junit-vintage-engine"
    }
    testClassesDirs = sourceSets.newTest.output.classesDirs
    classpath = sourceSets.newtTest.runtimeClasspath
    outputs.upToDateWhen { false }
}

And this is my configurations section from build.gradle(2) file:

configurations {
    integrationTestCompile.extendsFrom testCompile
    integrationTestRuntime.extendsFrom testRuntime

    newTestCompile.extendsFrom testCompile
    newTestRuntime.extendsFrom testRuntime
}

I have already looked into following tutorials:

  1. Multi-project test dependencies with gradle (I have only one project and here it is shown how to share test classes between projects)
  2. How to share test classes across Gradle projects?
  3. http://michaelevans.org/blog/2019/09/21/stop-repeating-yourself-sharing-test-code-across-android-modules/ (I can't currently migrate to Gradle 5.6)
  4. Include tests from other module

So what can I do to use test helpers from integration-test module in new-test module? I would like to also separate those helpers classes into separate module and then my new-test and integration-test tasks could take it as a test dependency.

Thanks!

Aucun commentaire:

Enregistrer un commentaire