【问题标题】:Cucumber feature outlines黄瓜功能概述
【发布时间】:2015-09-21 14:52:00
【问题描述】:

是否可以以与场景相同的方式参数化功能文件?那么功能中的每个场景都可以引用一些变量,这些变量稍后由整个功能文件的单个表定义?

到目前为止,我找到的所有答案(例如Feature and scenario outline name in cucumber before hook)都使用 Ruby 元编程,这对我正在使用的 jvm 设置没有太大希望。

【问题讨论】:

标签: cucumber cucumber-jvm gherkin


【解决方案1】:

不,不是,而且有充分的理由。功能文件旨在简单易读,而不是用于编程。即使使用场景大纲和表格通常也不是一件好事,因此,如果不阅读其他定义变量的东西就无法理解这一点,并且具有无法理解的功能会适得其反。

但是,您可以将所有变量和内容放入步骤定义中,并以更高的抽象级别编写您的功能。您会发现实现这一点要容易得多,因为您可以使用一种编程语言(擅长这方面的东西)。

【讨论】:

    【解决方案2】:

    参数化功能文件的一种方法是在编译时从模板生成它。然后在运行时你的黄瓜运行器执行生成的功能文件。

    如果您使用 gradle,这很容易做到。这是一个例子:

    build.gradle 中,添加 groovy 代码,如下所示:

    import groovy.text.GStringTemplateEngine
    
    task generateFeatureFiles {
        doFirst {
            File featuresDir = new File(sourceSets.main.output.resourcesDir, "features")
            File templateFile = new File(featuresDir, "myFeature.template")
            def(String bestDay, String currentDay) = ["Friday", "Sunday"]
            File featureFile = new File(featuresDir, "${bestDay}-${currentDay}.feature")
            Map bindings = [bestDay: bestDay, currentDay: currentDay]
            String featureText = new GStringTemplateEngine().createTemplate(templateFile).make(bindings)
            featureFile.text = featureText
        }
    }
    
    processResources.finalizedBy(generateFeatureFiles)
    

    myFeature.template 位于 src/main/resources/features 目录中,可能如下所示:

    Feature: Is it $bestDay yet?
      Everybody wants to know when it's $bestDay
    
      Scenario: $currentDay isn't $bestDay
        Given today is $currentDay 
        When I ask whether it's $bestDay yet
        Then I should be told "Nope"
    

    运行构建任务将在 build/src/main/resources 中创建一个 Frid​​ay-Sunday.feature 文件,并填写 bestDaycurrentDay 参数.

    generateFeatureFiles 自定义任务在 processResources 任务之后立即运行。生成的特征文件然后可以由黄瓜运行器执行。

    您可以从功能模板文件生成任意数量的功能文件。例如,代码可以从资源目录中的配置文件中读取参数。

    【讨论】:

      猜你喜欢
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 2011-11-23
      • 1970-01-01
      • 2021-08-20
      • 2012-02-22
      相关资源
      最近更新 更多