Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle integration test building but not running

I'm setting up a multi module gradle project and trying to separate integration tests for all subprojects. I've managed to create integTest task and I can see it builds the integration test classes and creates integration test reports in different directories to the unit tests. It isn't however running my integration test.

Project structure is something like

|_ gradle
| |_ integration-test.gradle
|_ subproject
| |_ src
| |  |_ main
| |  | |_ java
| |  | |_ resources
| |  |_ test
| |  | |_ java
| |  | |_ resources
| |  |_ integTest
| |    |_ java
| |    | |_sit
| |    |   |_ MyTest.java
| |    |_ resources
| |_ build.gradle
|_ build.gradle
|_ gradle.properties
|_ settings.gradle

In the integration-test.gradle I have

sourceSets {
    integTest {
        java.srcDir file('src/integTest/java')
        resources.srcDir file('src/integTest/resources')
        compileClasspath += sourceSets.main.output + configurations.testRuntimeClasspath
        runtimeClasspath += output + compileClasspath
    }
}

dependencies {
    integTestCompile sourceSets.main.output
    integTestCompile sourceSets.test.output 

    integTestCompile configurations.compile
    integTestCompile configurations.testCompile 

    integTestRuntime configurations.runtime
    integTestRuntime configurations.testRuntime 
}

task integTest(type: Test) {
    group = LifecycleBasePlugin.VERIFICATION_GROUP
    description = 'Runs the integration tests.' 

    maxHeapSize = '1024m' 

    testClassesDir = sourceSets.integTest.output.classesDir
    classpath = sourceSets.integTest.runtimeClasspath 

    binResultsDir = file("$buildDir/integration-test-results/binary/integTest")

    reports { 
        html.destination = "$buildDir/reports/integration-test"
        junitXml.destination = "$buildDir/integration-test-results"
    }

    mustRunAfter tasks.test
}

check.dependsOn integTest

And then in the subproject build.gradle I add the following at the top

apply from: "$rootDir/gradle/integration-test.gradle"

After running gradle clean build I can see the unit tests building and running and see the test reports afterwards. I can also see that the integration tests are building in a separate integTest directory underneath the classes in the build dir. I can also see a new integration-test-results directory created in the build directory, however the tests defined in MyTest.java are not running

I also see this in the console when running with the -i flag

> Task :javaproject:integTest
Task ':javaproject:integTest' is not up-to-date because:
  Output property 'binResultsDir' file E:\dev\java\workspace\javaproject\subproject\build\integration-test-results\binary\integTest has been removed.
  Output property 'binResultsDir' file E:\dev\java\workspace\javaproject\subproject\build\integration-test-results\binary\integTest\output.bin has been removed.
  Output property 'binResultsDir' file E:\dev\java\workspace\javaproject\subproject\build\integration-test-results\binary\integTest\output.bin.idx has been removed.
Finished generating test XML results (0.0 secs) into: E:\dev\java\workspace\javaproject\subproject\build\integration-test-results
Generating HTML test report...
Finished generating test html results (0.006 secs) into: E:\dev\java\workspace\javaproject\subproject\build\reports\integration-test
:javaproject:integTest (Thread[Task worker for ':',5,main]) completed. Took 0.092 secs.
:javaproject:check (Thread[Task worker for ':',5,main]) started.
like image 731
PDStat Avatar asked Jul 17 '18 12:07

PDStat


People also ask

Does Gradle build also run tests?

By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. This detection uses different criteria depending on the test framework used. For JUnit, Gradle scans for both JUnit 3 and 4 test classes.

Does Gradle run tests in parallel by default?

Parallel execution Yet Gradle will only run one task at a time by default, regardless of the project structure (this will be improved soon). By using the --parallel switch, you can force Gradle to execute tasks in parallel as long as those tasks are in different projects.

How do I run Gradle without a test?

To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build. As a result, the test sources aren't compiled, and therefore, aren't executed.


1 Answers

Right for anyone using JUnit5 and setting up integration tests in this way make sure to add useJUnitPlatform() to your integration test task. So my task became

task integTest(type: Test) {
    useJUnitPlatform()
    group = LifecycleBasePlugin.VERIFICATION_GROUP
    description = 'Runs the integration tests.' 

    maxHeapSize = '1024m' 

    testClassesDir = sourceSets.integTest.output.classesDir
    classpath = sourceSets.integTest.runtimeClasspath 

    binResultsDir = file("$buildDir/integration-test-results/binary/integTest")

    reports { 
        html.destination = "$buildDir/reports/integration-test"
        junitXml.destination = "$buildDir/integration-test-results"
    }

    mustRunAfter tasks.test
}

And then it all started magically working. I spotted it because I re-ran the build with gradle clean build -d and spotted the following which helped me refine my googlefu

13:58:08.909 [DEBUG] [TestEventLogger] Gradle Test Run :subproject:integTest STARTED
13:58:08.943 [DEBUG] [org.gradle.api.internal.tasks.testing.detection.AbstractTestFrameworkDetector] test-class-scan : failed to scan parent class java/lang/Object, could not find the class file
like image 149
PDStat Avatar answered Sep 25 '22 03:09

PDStat