Question

Lets say I have a test case in a Java based Gradle project that requires an image to be present to complete, and that the image is largish (~100MB). For example, maybe a test needs to read pixel data from an image so that it can confirm that some CV algorithm is completing consistently. It would be pretty straight forward to just add this data to a directory in the project, such as src/test/data, and check the data into version control with everything else, but that makes the repository very large and requires a copy of the image also be checked into every other project that wishes to use it.

Is there a way to package this image so that it may be added to a network repository such as Nexus or Artifactory and then pulled in to a project using the Gradle dependency mechanism? If so, how would I then reference the image from within my test case?

Edit: This is the solution I ended up going with.

  1. Publish an artifact containing the test data. In this case I decided to actually bundle multiple test images into a zip file and publish it like that.
  2. Add the following to my gradle.build file:

    configurations {
        testData
    }
    
    dependencies {
        testData group: 'com.company', name: 'testData', version: '1.0', ext: 'zip'
    }
    
    task unzip(type: Copy) {
        def outputDir = file("${buildDir}/unpacked/dist")
    
        from {zipTree(configurations.testData.singleFile)}
        into outputDir
    }
    
    test {
    dependsOn unzip
        systemProperties 'testDataDir': file("${buildDir}/unpacked/dist")
    }
    

    This will add a new configuration to hold the test data in, as well as a new task to unzip the data. Note that the brackets around zipTree(configurations.testData.singleFile) are important because they create a closure that ensures that the path is only resolved at build time. Without them, the test data is downloaded whenever the build script is run, not just when the test task is. The last block in there configures the test task to depend on the unzip one, and then to pass the location of the files into the tests as a system property. You can read the property within the test files with System.getProperty("testDataDir"). Because the unzip task is set as a dependency of the test task, the file will only be downloaded and unzipped when the test task is executed. This keeps normal builds fast.

Was it helpful?

Solution 2

As Peter said, add it as a configuration dependency.

image "some.group:someArtifact:1.0@img"

In your gradle script reference it as

configuration.image.singleFile.getPath()

OTHER TIPS

Yes, there are ways to make this work. For example, you could publish the image to Nexus and resolve it using Gradle's regular dependency management (e.g. using the artifact-only notation "some.group:someArtifact:1.0@img"). Then you could configure the test task to pass the image's local file path to the test via a system property. Or you could put the image on the test runtime class path and access it via, say, ClassLoader#getResource.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top