Packaging TestProject Tests and Addons With Gradle

Before we can publish our tests and addons on the app.testproject.io website, we have to package our tests and addons to jar file. This blog post describes how we can create a Gradle project that can create the required jar file. After we have finished this blog post, we:

  • Understand how we can get the TestProject SDK.
  • Can declare the dependencies of our Gradle project.
  • Know how we can package our tests and addons to a jar file.

Let's start by downloading the TestProject SDK.

This blog post is the second part of my TestProject tutorial that is sponsored by TestProject.io. However, the views and opinions expressed in this tutorial are mine.

This blog post assumes that:

By the way, you might want to read the other parts of my TestProject tutorial.

Downloading the TestProject SDK

Before we can use the TestProject SDK, we have to download it from the the Developers page of app.testproject.io website. We can do this by following these steps:

You can download the SDK only if you have a user account. If you don't have a TestProject user account, you have to request it from TestProject website.
  1. Open the the Developers page. If you are not logged in, the website will ask you to log in.
  2. Select the downloaded SDK (we will use the Java SDK) and click the 'Download SDK' button.
  3. Download the io.testproject.sdk.java.jar file.
  4. Create a libs directory to the root directory of our Gradle project.
  5. Copy the io.testproject.sdk.java.jar file to the libs directory.

After we have download the TestProject SDK, the directory structure of our Gradle project should look as follows:

libs
|__ io.testproject.sdk.java.jar

Next, we will find out how we can configure the Gradle properties of our Gradle build.

Configure the Gradle Properties of Our Gradle Build

When we want to configure the Gradle properties of our Gradle build, we have to create a file called: gradle.properties and put this file to the root directory of our Gradle build. After we have created this file, the directory structure of our Gradle project looks as follows:

libs
|__ io.testproject.sdk.java.jar
gradle.properties

After we have created the gradle.properties file, we have to configure the Gradle properties of our Gradle build by following these steps:

  1. Ensure that our source code is compiled by using the Java compiler 1.8.
  2. Ensure that the compiled class files are compatible with the Java 8 virtual machine.
  3. Configure the version of the packaged jar file. We will use the value of this property in the name of the created jar file.

After we have configured the Gradle properties of our Gradle build, the gradle.properties file looks as follows:

sourceCompatibility = 1.8
targetCompatibility = 1.8
version = 0.0.1
At the moment, the TestProject SDK supports only Java 8.

Additional Reading:

Let's move on and find out how we can create the build script of our Gradle build.

Creating the Build Script of Our Gradle Build

When we want to create the build script of our Gradle build, we have to create a file called: build.gradle) and put this file to the root directory of our Gradle build. After we have created this file, the directory structure of our Gradle project looks as follows:

libs
|__ io.testproject.sdk.java.jar
build.gradle
gradle.properties

We can now configure our Gradle build by following these steps:

First, we have to create a Java project by applying the Java plugin. After we have applied this plugin, we have to set the encoding of the Java compiler to 'UTF-8'.

After we have created our Java project, the build.gradle file looks as follows:

apply plugin: 'java'

compileJava.options.encoding = 'UTF-8'

Second, we have to configure the dependency configurations of our Gradle build by following these steps:

  1. Create a dependency configuration called: tpsdk. We will use this configuration when we declare the TestProject SDK dependency.
  2. Ensure that the compile dependency configuration extends the tpsdk configuration. This ensures that we can compile classes which use the TestProject SDK.

After we have created the required dependency configurations, our build.gradle file looks as follows:

apply plugin: 'java'

compileJava.options.encoding = 'UTF-8'

configurations {
    tpsdk
    compile.extendsFrom tpsdk
}

Third, we have to declare the dependencies of our Gradle build. When we declare the dependencies of our Gradle build, we have to follow these two rules:

  1. We must add the TestProject SDK dependency to the tpsdk dependency configuration. Also, we must ensure that Gradle resolves this dependency from the libs directory.
  2. We must add all other dependencies to the compile dependency configuration.

After we have declared the required dependencies, the build.gradle file looks as follows:

apply plugin: 'java'

compileJava.options.encoding = 'UTF-8'

configurations {
    tpsdk
    compile.extendsFrom tpsdk
}

dependencies {
    tpsdk files('libs/io.testproject.sdk.java.jar')
}

Fourth, we have configure the jar task by following these steps:

  1. Configure the name of the created jar file by using the formula: [The name of the root project]-[The version of the project].jar.
  2. Ensure that the created jar file contains all dependencies which belong to the runtime dependency configuration.
  3. Ensure that the TestProject SDK dependency isn't included in the created jar file. We have to exclude this dependency from the created jar file because we want to avoid class loading conflicts that can occur when our test or addon is run by the TestProject agent. The TestProject agent loads always the latest SDK jar that's backward compatible with our SDK version. Also, excluding the TestProject SDK dependency will minimize the size of the created jar file.
  4. Include the used SDK version in the created jar file. We can do this by copying the testproject-sdk.properties file from the TestProject SDK dependency to the created jar file. We must remember to do this because otherwise the TestProject platform will display an error message when we try to upload the jar file to app.testproject.io website. The TestProject platform uses the SDK version for determining if a TestProject agent can run a test or addon. If the TestProject agent loads an old SDK, it cannot run tests or addons written with a newer SDK.

After we have configured the jar task, our build.gradle file looks as follows:

apply plugin: 'java'

compileJava.options.encoding = 'UTF-8'

configurations {
    tpsdk
    compile.extendsFrom tpsdk
}

dependencies {
    tpsdk files('libs/io.testproject.sdk.java.jar')
}

jar {
    archiveName "${rootProject.name}-${version}.jar"
    dependsOn configurations.runtime
    from {
        (configurations.runtime - configurations.tpsdk).collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }

    from {
        (configurations.tpsdk).collect {
            zipTree(it).matching {
                include 'testproject-sdk.properties'
            }
        }
    }
}

We have now created a Gradle project that can compile and package both TestProject tests and addons. Let's summarize what we learned from this blog post.

Summary

This blog post has taught us five things:

  • We have to download the TestProject SDK from the the Developers page of app.testproject.io website.
  • The TestProject SDK supports only Java 8.
  • We have to add the TestProject SDK dependency to the tpspk dependency configuration.
  • We have to add all other dependencies to the compile dependency configuration.
  • We must ensure that the TestProject SDK dependency isn't included in the created jar file.

P.S. You can get the example application of this blog post from Github.

1 comment… add one

Leave a Reply