Search code examples
javaandroidgradlecucumber-jvm

How to run tests filtered with tags with cucumber-jvm and gradle?


I work with cucumber for some time now, and now I want to migrate test suite usage to gradle from maven.

I managed to prepare the project covering the basic usage, running tests, having the results etc. The last piece I am missing is ability to only run the tests filtered on specific tag. Running acceptance tests is done with product flavor:

productFlavors {
    uats {
        testInstrumentationRunner "com.paddy.cuespton.cuespton.test.Instrumentation"
    }

    full {
        applicationId "com.paddy.app.cuespton"
        versionName "1.0"
    }
}

Which enables me to run tests with task:

./gradlew connectedAndroidTestUatsDebug

Is is possible to add param with tag to this task to run specific tests only?

I tried to use https://github.com/samueltbrown/gradle-cucumber-plugin/ plugin which should in theory solve this issue, but I cannot get it running with Android due to language incompatibility.

Here is repo on which I am working, https://github.com/paddyzab/espresso-cucumber-sandbox.

Thanks for help!


Solution

  • Didn't try this cucumber-plugin but assuming that we have similar setup you can do the following (sample repo):

    1) define corresponding buildConfigField for the uats flavor:

    Uats {
            testInstrumentationRunner "com.quandoo.gradletestpoc.test.Instrumentation"
    
            // passing instrumentation parameters
            buildConfigField "String", "TAGS", "\"${getTagsProperty()}\""
        }
    

    2) define getTagsProperty() method:

     def getTagsProperty() {
         return project.hasProperty("tags") ? project.getProperties().get("tags") : ""
     }
    

    3) Handle passed tag in onCreate() method of your custom instrumentation class:

    private static final String TAGS_KEY = "tags";
    ......
    @Override
    public void onCreate(final Bundle bundle) {
        super.onCreate(bundle);
    
        // Reading runner params
        String tags = BuildConfig.TAGS;
        if (!tags.isEmpty()) {
            bundle.putString(TAGS_KEY, tags);
        }
    
        instrumentationCore.create(bundle);
        start();
    }
    

    4) Run

    ./gradlew connectedAndroidTestUatsDebug -Ptags="@bar"
    

    Enjoy!