Gradle Unit Test Question

So I am reaching out here because there are some very talented gradle users lurking around (yes I know who some of you are :grin:)

I want to do some unit tests for the server application of my engine. And building the tests is not an issue, but running them is.

Due to the serverā€™s complex setup, I cannot just run the tests ā€˜in placeā€™ like the normal ā€˜testā€™ task attempts to do. I need to build a folder with the necessary resources in it, and have the run directory for the tests point there. AKA, File myFile = new File("./myFile") woule be a top level file in that directory for context. An example of these resources are groovy scripts that exist in a ./scripts/ external to the jar.

For my builds, I have a custom set of build tasks that setup all the resources in the distribution folder for the build. But I have no idea how to do the same for the tests, and then run them in that folder.

Also, my build script when creating the distribution adds custom properties to the manifest using:

jar {
    manifest {
        attributes(
                'Main-Class':             'io.tlf.outside.server.Main',
                'Implementation-Title':   project.name,
                'Implementation-Version': version,
                'Version-ID': versionId,
                'Class-Path': configurations.compile.files.findAll{ !it.getName().startsWith('openjfx-') }.collect{ 'lib/' + it.getName() }.join(' ')
        )
    }
}

Is there any way to get the unit tests to also include these manifest properties, like a way to ā€˜fakeā€™ reading the properties?

Any help would be greatly appreciated.

Thanks,
Trevor

Interesting challenges, somewhat beyond my current skill level. I suspect theyā€™re doable, though.

Have you tried the Gradle Forum?

No I have not. I do not even have an account there. I should probably make one.

I figured it out!

So, you can specify where the tests are actually run from by doing:

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
    workingDir = new File("$buildDir/testExec") // <<< Runtime directory for tests
}

Then I created a set of tasks that copy the required files into the folder prior to the tests running.
Note: you will have to make the directory, gradle will not make it for you.
Here is the example for copying my scripts into it. Now I need to copy the rest of my resources into it.

task testPrep() {
    doLast {
        mkdir "$buildDir/testExec"
    }
}
task testPrepScripts(type: Copy, dependsOn: testPrep) {
    from ('src/main/groovy')
    into "$buildDir/testExec/scripts/server"
}
tasks.test.dependsOn("testPrepScripts")
1 Like

Note: strictly speaking, what you are talking about arenā€™t ā€œunit testsā€ā€¦ but more ā€œintegration testsā€. Maybe ā€œfunctional testsā€ if you squint.

ā€¦and I only mention that to help in future searches related to this. Many people (myself included) use the built in unit-testing stuff to do functional testing and moreā€¦ but then when we have problems we need to look outside the scope of ā€œunit testā€. Sometimes it helps.

2 Likes