如何使Cucumber功能作为本地单元测试在Android项目中运行? [英] How to make Cucumber features run in an Android project as local unit tests?

查看:219
本文介绍了如何使Cucumber功能作为本地单元测试在Android项目中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Java编写的Android项目,正在Android Studio中进行。



我想使用Cucumber进行一些内部组件的集成测试。 (注意:我知道这不是BDD方式,但是对我有用)。我希望测试使用


  • 为步骤添加了一个类( Steps1.java ),如上所示。


  • 我在这里缺少什么?

    解决方案

    我已经找到了如何配置Android的方法当我从命令行运行 gradlew测试时,该应用程序将黄瓜测试作为本地单元测试运行。这基于我的此处的问题并且我在这里写了一篇有关它的 HOWTO博客文章: Android:无需设备或仿真器即可运行Cucumber测试



    完成该操作的关键在于应用的 build.gradle 文件。通过锁定 testOptions 部分中的 unitTests.all 钩子,我可以使用<$ c $运行Cucumber c> javaexec 像这样:

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


    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?

    解决方案

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

    这篇关于如何使Cucumber功能作为本地单元测试在Android项目中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆