How to configure test classes to use build artifact as test resource


(Aver Mimas) #1

Hi,

I have a gradle build whose final output is a zip file. I need to use this artifact in my test. How can I configure my gradle build so that my test classes can see this build artifact as a test resource?

I’m using ‘org.unbroken-dome.test-sets’ plugin to create a new “sourceset” called “integrationTest”. This way, I can have following structure, in addition to regular file/folder convention

src/integrationTest/groovy/org/name/package/name
src/integrationTest/resource/

The plugin also provides a task name using the new sourceset. So, I can type “gradlew integrationTest” to run my test cases from src/integrationTest/groovy/... directory. All of this is working fine and my test cases can also use resources needed from the src/integrationTest/resources directory.

Now, I need to enhance the test cases to use one of the build artifact as a test resource. Obviously, this build artifact is generated after a successful build is run by executing gradlew clean build. But, I’m not sure how or if it’s possible for gradle to configure so that the “integrationTest” source set can have resources from both src/integrationTest/resource as well as a build artifact.


(Aver Mimas) #2

I tried to solve it like this but unsuccessful.

I added following to my build.gradle:

sourceSets {
    integrationTest {
        resources.srcDirs += "$buildDir/distributions" 
    }
}

I noticed this automatically copies files from build/distributions into build/resources/integrationTest directory. I needed to have the unzipped copy of the artifact accessible to my test class. So, I removed the above config and added following to my build.gradle

processIntegrationTestResources {
    from zipTree (file("$buildDir/distributions/my-archive-1.0-SNAPSHOT.zip"))
    into 'my-archive'
}

I know the file name in above snippet is hardcoded but at least this unzips the files and copies them into build/resources/integrationTest directory (expected it to create a sub-directory named my-archive but it didn’t; that’s a different issue).

Anyways, I thought this would make the resource (unzipped copy of the archive) accessible to my test class, but it didn’t. Here’s a snippet of my test class located in src/integrationTest/groovy/org/name/package/name directory

package org.name.package.name

import groovyx.net.http.RESTClient

import spock.lang.Shared
import spock.lang.Specification

class MyArchiveIntegrationSpec extends Specification {

    String webHost = "localhost"
    int port = "80"

    def setup() {
        //install plugin from my archive
        def command = "src/integrationTest/resources/setup.sh"
        def exitValue = executeOnShell(command)

        assert exitValue == 0
    }

    def "check web server is up and running"() {
        given: "a http client"
            // def client = HttpClientBuilder.create().build()
            def client = new RESTClient("http://$webHost:$port/")

        when: "accessing web server"
            def response = client.get(path: "")

        then: "web server is running and returns http status code 200"
            response.status == 200
            println "Web server's returning status code: $response.statusLine.statusCode"
    }

In the setup() function in above example, I needed to execute setup.sh, which is built by this project and archived.

Is there any other way I can make the build artifact available to my test?