Java – Run instrument tests from specific packages via the spoon-gradle plugin

Run instrument tests from specific packages via the spoon-gradle plugin… here is a solution to the problem.

Run instrument tests from specific packages via the spoon-gradle plugin

I’m using spoon-gradle-plugin from Roman Mazur. I was able to run all the tests at once, but I couldn’t specify the “group” tests I wanted to start. Currently my spoon settings are as follows:

spoon {
    debug = true

baseOutputDir = file("$buildDir/spoon-log")
    if (project.hasProperty('spoonClassName')) {
        className = project.spoonClassName

if (project.hasProperty('spoonMethodName')) {
            methodName = project.spoonMethodName
        }
    }

adbTimeout = 60 * 60;
}

My tests are in the package:

enter image description here

My goal is to create independent gradle tasks that rely on spoon to launch tests from each package separately. Roman gave us the parameters instrumentationArgs It should be able to edit certain properties in the spoon.

As I saw on main git of spoon, it says you can specify the package where spoon-runner should look for your tests, example below:

--e package=com.mypackage.unit_tests

So my idea is to put this property in instrumentationArgs. Therefore, I created spoon tasks like this:

task spoonAuthFlowTests(type: GradleBuild, dependsOn: ['spoon']) {
    spoon {
        instrumentationArgs = ["package=com.myapp.instrumentation.flowtests.AuthFlowTests"]
        noAnimations = true;
    }
}

task spoonFlowTests(type: GradleBuild, dependsOn: ['spoon']) {
    spoon {
        instrumentationArgs = ["package=com.myapp.instrumentation.flowtests"]
        noAnimations = true;
    }
}

What I can say is that the noAnimations parameter nicely extends the default spoon configuration and prevents gifs from being created. So instrumentationArgs will definitely take my string array, but won’t apply the changes because in my terminal:

2016-01-08 15:13:10 [SDR.run] About to actually run tests for [04ffe19ad317d2e7]
03:13:10 I/RemoteAndroidTest: Running am instrument -w -r   -e package com.myapp.instrumentation.flowtests -e class com.myapp.instrumentation.flowtests.AuthFlowTests.LoginUserFlowTests com.myapp.debug1.test/com.myapp.instrumentation.helper.runner.MyAppTestRunner on lge-nexus_ 4-04ffe19ad317d2e7

No matter what I do with the “package” attribute, I always get the result:

-e package com.myapp.instrumentation.flowtests

I want to change it, but don’t know how. Also, I can say that I tried to look in the string “com.myapp.instrumentation.flowtests” in my project, and the only place to use it is: tests in the package + gradle task described above. So it’s not hardcoded anywhere. If I start testing in the following way, the same location is chosen:

./gradlew spoon

After I used :

./gradlew spoonAuthFlowTests

It also runs the entire test set.

Solution

Your problem is that you misinterpreted how spoon block works in the Gradle configuration.
When you write something like that

spoon {
  debug = true
}

You basically modified the singleton objects associated with your Gradle project. The project contains a configuration that is shared among all tasks created by the spoon plugin.
The Spoon plugin creates separate tasks for the different flavors defined in your project (so that you can run tests for each flavor separately). There are also tasks such as spoonSmall, spoonMedium only running tests with @Small or @Medium comments.
All of these tasks use the same configuration object that you changed with spoon {}.

Therefore, when you call spoon {} in a task definition, you can simply overwrite the existing value. and apply the final value.

If you want to create a custom spoon runner task, you should write it like this

import com.stanfy.spoon.gradle.SpoonRunTask
task spoonAuthFlowTests(type: SpoonRunTask) {
  instrumentationArgs = ['package=com.myapp.instrumentation.flowtests.AuthFlowTests']
   But you will have to set many other options on the tasks,
   like instrumentationApk and applicationApk files.
}

You can > . Most of these are set from a single configuration object by the plugin when it creates the task.

If this sounds too complicated, you can choose a different way. Configure parameters using project properties that you can define on the command line.

spoon {
  instrumentationArgs = ["package=${project.getProperty('spoonPackage')}"]
  noAnimations = true;
}

Now you can run

./gradlew spoon -PspoonPackage=com.myapp.instrumentation.flowtests

Therefore, instead of specifying different tasks on the command line, you specify different project properties.

The downside is that you won’t be able to run tests for 2 packages with a single gradle call. You must call it twice with different values.

Related Problems and Solutions