How to make Cucumber features run in an Android project as local unit tests?

家住魔仙堡 提交于 2019-12-24 10:27:28

问题


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:

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

What am I missing here?


回答1:


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.




回答2:


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']
            }
        }
    }
)


来源:https://stackoverflow.com/questions/52348197/how-to-make-cucumber-features-run-in-an-android-project-as-local-unit-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!