Gradle - Test

Management

How to add a test Dependency

Jar

add a testImplementation or testCompile dependency

dependencies {
    testImplementation 'junit:junit:4.12' 
}
dependencies {
    testCompile("junit:junit:4.12")
}

Main Jar in a multi-module project

dependencies {
   testCompile(project(":project-name"))
}

Test Jar in a multi-module project

in the module where the test are to create a test jar in kotlin dsl

/**
 * Create a test jar
 * From:
 *   https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:project_jar_dependencies
 *   https://stackoverflow.com/questions/53335892/using-testcompile-output-from-other-subproject-gradle-kotlin-dsl
 */
configurations {
  create("test")
}

tasks.register<Jar>("testJar") {
  dependsOn("testClasses")
  archiveBaseName.set("${project.name}-test")
  from(sourceSets["test"].output.classesDirs)
}

artifacts {
  add("test", tasks["testJar"])
}
  • in the module that depends on the test class
dependencies {
    testCompile(project(":project-name","test"))
}

Help

gradlew help --task test

Skip

gradle build -x test

Report

Integration

Doc:

Steps:

  • Project Layout
├── build.gradle
├── gradle
│   └── integration-test.gradle
├── settings.gradle
└── src
    ├── integTest
    │   └── java
    │       └── DefaultFileReaderIntegrationTest.java
    ├── main
    │   └── java
    │       ├── DefaultFileReader.java
    │       ├── FileReader.java
    │       └── StringUtils.java
    └── test
        └── java
            └── StringUtilsTest.java

sourceSets {
    integTest {
        java.srcDir file('src/integTest/java')
        resources.srcDir file('src/integTest/resources')
        compileClasspath += sourceSets.main.output + configurations.testRuntimeClasspath
        runtimeClasspath += output + compileClasspath
    }
}
  • To execute the test
task integTest(type: Test) {
    description = 'Runs the integration tests.'
    group = 'verification'
    testClassesDirs = sourceSets.integTest.output.classesDirs
    classpath = sourceSets.integTest.runtimeClasspath
    mustRunAfter test
}

check.dependsOn integTest

Documentation





Discover More
Gradle - Multi-Project

A page multi-project setup in gradle. There is only one settings.gradle file at the root There is one top-level build script (build.gradle) (also known as the root project) that configure common...



Share this page:
Follow us:
Task Runner