How to make Cucumber features run in an Android pr

2019-07-26 00:04发布

I have an Android project written in Java that I'm working on in Android Studio.

I'd like to use Cucumber for integration testing of some internal components (note: I know this is not the BDD way, nonetheless useful to me). I want the tests to run as local unit tests (without Instrumentation) using gradlew test because the components under test do not interact with the Android SDK.

My problem is that the Cucumber features are not recognized by Gradle and do not run when I run gradlew test.

Here's how I set it up so far:

  1. Added these dependencies to my app's build.gradle:

    testImplementation 'io.cucumber:cucumber-java:3.0.2'
    testImplementation 'io.cucumber:cucumber-junit:3.0.2'
    testImplementation 'io.cucumber:cucumber-jvm:3.0.2'
    
  2. Also there, I added the path to where I've put my Feature file:

    android {
        ...
        sourceSets {
            test {
                assets.srcDirs = ['src/test/java/integrationTest/assets']
            }
        }
    }
    

    This is based on this folder structure:

    enter image description here

  3. Added a class for the steps (Steps1.java) as can be seen above.

What am I missing here?

2条回答
爷、活的狠高调
2楼-- · 2019-07-26 00:48

Your feature files are probably not getting picked up because you did not include a runner. You can either create a JUnit Runner or use the Gradle cucumber plugin. I am not sure if either would work in Android though.

Also you don't need io.cucumber:cucumber-jvm:3.0.2 as a dependency. It is only a pom.

查看更多
Juvenile、少年°
3楼-- · 2019-07-26 00:54

I've found out how to configure my Android app to run my Cucumber tests as "local unit tests" when I run gradlew test from the command line. This is based on my SO question here and I've written a "HOWTO" blog post about it here: Android: Run Cucumber tests without a device or an emulator.

The key to getting it done is in the app's build.gradle file. By latching on to the unitTests.all hook in the testOptions section I am able to run Cucumber using javaexec like so:

android {
    ...
    testOptions {
        unitTests.all {
            def classpath2 = getClasspath()
            javaexec {
                main = "cucumber.api.cli.Main"
                classpath = classpath2
                args = ['--plugin', 'pretty', '--glue', 'gradle.cucumber', 'src/test/java/cucumber/assets']
            }
        }
    }
)
查看更多
登录 后发表回答