android - 关于 Android Studio 中 calabash-android 支持的问题 : Ruby, Editing features and steps, Launching tests

标签 android ruby cucumber android-studio calabash

我在 64 位 Windows 7 上使用 Android Studio。我是 Android Studio(或任何 Intelij IDE)的菜鸟。

我下载并安装了 Ruby 1.9.3、Ruby DevKit 和 calabash-android,我可以使用命令行 (calabash-android run) 在我的 Android 应用程序上成功运行 Cucumber 测试

我还设法为 Android Studio 安装了 Cucumber 插件,这样我的功能文件就可以从自动完成等功能中受益。

我有以下问题:

  • 我可以安装一个 Ruby 插件(RubyMine 吗?)以便我可以为我的测试编写步骤定义? 如果是这样,我听说人们可以调试 Cucumber 测试:这也可以在 Android Studio 中为 Android 应用程序完成吗?

  • 我可以从 Android Studio 为 Android 应用启动 calabash 测试吗?如果是这样,我该怎么做?

  • 我能否在 Android 应用的 Gradle 构建中使用 calabash 集成(自动化)测试?如果是这样,我该怎么做?

谢谢!

更新:

我附加了一个自定义 gradle Plugin<Project> (请参阅下面我编写的 groove 代码,为运行 calabash-android 测试提供基本支持。

这些手动步骤仍然是必要的:
- 安装 Ruby 1.9.x 及其 Devkit,安装 calabash-android gem 等。
- 使用 android gradle 插件(手动或自动)构建适当的(风格的)APK

在应用程序的 build.gradle 中, 添加 apply plugin: 'calabash'现在可以工作了,它允许构建运行一个特性文件作为 calabash 测试。
它检查可用的产品 flavor (构建 flavor )并添加适当的葫芦相关任务(例如 calabashDebugcalabashFlavor1Release 等)。

下面是实现我的“葫芦”插件的 groovy 文件(目前仅适用于 Windows):

    package com.mediaarc.gradle.plugins

    import org.gradle.api.*
    import org.gradle.api.plugins.*
    import org.gradle.api.tasks.*

    class CalabashPlugin implements Plugin<Project> {
        void apply(Project project) {
            project.extensions.create("calabash", CalabashPluginExtension)

            if (!project.android) {
                throw new IllegalStateException("Android plugin is not configured.")
            }

            project.android.applicationVariants.each { variant ->
                final def buildName  = variant.name
                final def buildVar   = variant.baseName
                final def packageApp = variant.packageApplication;

                project.task("doPrepare${buildName}") << {
                    project.calabash.init(project, buildVar)
                    def apkFile = packageApp.outputFile
                    project.calabash.writeCommandFile(apkFile)
                }

                project.task("doClean${buildName}") << {
                    project.calabash.init(project, buildVar)

                    project.calabash.clean()
                }

                project.task("calabash${buildName}", type: Exec, dependsOn: [ project["assemble${buildName}"], project["doPrepare${buildName}"] ]) {
                    println project["assemble${buildName}"]
                    project.calabash.init(project, buildVar)
                    configureTask(project[name], buildName)

                    project.calabash.execute(project[name])
                }

                project.task("cleanCalabash${buildName}", dependsOn: project["doClean${buildName}"]) {
                    project.calabash.init(project, buildVar)
                    configureClean(project[name], buildName)
                }
            }
        }

        private def configureTask(def task, def buildVariant) {
            task.group = JavaBasePlugin.VERIFICATION_GROUP
            task.description = "Runs calabash tests for Build '${buildVariant}'"
        }

        private def configureClean(def task, def buildVariant) {
            task.group = BasePlugin.BUILD_GROUP
            task.description = "Deletes the calabash tests results for Build '${buildVariant}'"
        }
    }

    class CalabashPluginExtension {
        def root = 'src/calabash'
        def resultFile = "calabash-results.html"

        //protected def hash = new Object()
        protected File outputFile
        protected File workingDir
        protected File tmpFile

        protected init(Project project, def buildVariant) {
            if (!buildVariant) {
                buildVariant = "debug"
            }

            File rootFile = project.file(root)
            outputFile   = new File(project.file("build/reports/calabash/${buildVariant}"), resultFile)
            workingDir   = rootFile
        }

        protected writeCommandFile(def apkFile) {
            if (!workingDir.exists()) {
                throw new IllegalStateException("The root directory for the calabash-tests could not be found: '${workingDir}'")
            }

            if (!(new File(workingDir, "features").exists())) {
                throw new IllegalStateException("The required 'features' directory could not be found in '${workingDir}'")
            }

            outputFile.parentFile.mkdirs()

            def calabashCmd = "cd ${workingDir.canonicalPath}\r\necho calabash-android run \"${apkFile.canonicalPath}\" --format html --out \"${outputFile.canonicalPath}\"\r\n"
            getCommandFile().write calabashCmd
        }

        protected execute(Exec exec) {
            exec.commandLine 'cmd', '/c', getCommandFile().canonicalPath
        }

        protected clean() {
            outputFile.delete()
        }

        private File getCommandFile() {
            if (!tmpFile) {
                tmpFile = File.createTempFile("run-calabash", ".bat")
                tmpFile.deleteOnExit()
            }
            return tmpFile
        }
    }

最佳答案

很好的问题。 Xamarin 有一个 webinar on using Calabash tests在他们的测试云产品中。在演讲快结束时,有相当多的动手实践,包括设置测试生态系统和运行 Android 的 Calabash 测试。里面有很多内容不适用于您的环境,但是 Karl Krukow(calabash-android 的主要贡献者之一)提供了一些非常好的技巧和见解。

关于android - 关于 Android Studio 中 calabash-android 支持的问题 : Ruby, Editing features and steps, Launching tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16985782/

相关文章:

ruby - 使用 rspec 进行测试时,对于非空文件,File.read 为空

java - Android 应用程序和 (Android) 库存档

java - AsyncTask 在方法(.cancel)之后仍然运行

ruby-on-rails - belongs_to 关联栏中的组函数

ruby - 在 TeamCity 上构建和测试命令行 Gem

java - 在测试中使用Java + Spring + Cucumber 框架相对于核心Java + Cucumber 框架有哪些优势?

java - 执行 Cucumber 测试时 Spring Boot 休息服务关闭

java - 在一个Activity中组织多个Fragment接口(interface)

android - Kotlin : getResources() on a null object reference

ruby-on-rails - 注册时发生未定义的方法错误