简体   繁体   English

如何将一些 json 文件中的数据传递到 Gherkin 功能文件

[英]How to pass a data from some json file to Gherkin feature file

I want to parameterize my gherkin feature file steps by taking data from some other JSON file.我想通过从其他一些 JSON 文件中获取数据来参数化我的小黄瓜功能文件步骤。 Any suggestions for this.对此有任何建议。 I searched almost everywhere but couldn't find answers.我几乎到处搜索,但找不到答案。

I am aware of scenario where examples being used with multiple values for a variable using scenario outline in gherkin feature file but not looking for that.我知道使用小黄瓜功能文件中的场景大纲将示例与变量的多个值一起使用但不寻找的场景。

Currrently I am using like this and below values in quotes are being passed to step definitions目前我正在像这样使用下面的引号中的值被传递给步骤定义

    Scenario: Buy last coffee
        Given There is "Starbucks" coffee
        And I added "Sugarless" syrup

Expected: I want to get data of variables from JSON file or any other file also and pass these data values to step definition functions.预期:我想从 JSON 文件或任何其他文件中获取变量数据,并将这些数据值传递给步骤定义函数。 is it possible?可能吗?

gherkin feature file:小黄瓜功能文件:

    Scenario: Buy last coffee
        Given There is "${data.coffeeshop}" coffee
        And I added "${data.sugarType}" syrup

data.json:数据.json:

    {
        "coffeeshop": "starbucks",
        "sugarType": "Sugarless",

    }

I have implemented similar approach recently. 我最近已经实现了类似的方法。 I am matching json data based on scenario name. 我根据方案名称匹配json数据。 the json will look like this.In this way, you can dynamically match test data with your scenarios. json看起来像这样。您可以根据情况动态匹配测试数据。

{
"scenario1":
      {
       "coffeeshop": "starbucks",
        "sugarType": "Sugarless"
      },
"scenario2":
      {
       "coffeeshop": "starbucks",
        "sugarType": "Sugarless"
      }
}

your feature file will look like this, 您的功能文件将如下所示,

Scenario: senario1
        Given There is coffee
        And I added syrup

Scenario: senario2
        Given There is  coffee
        And I added syrup

Its a common Cucumber anti-pattern to try and inject data into feature files. 它是一种常见的Cucumber反模式,用于尝试将数据注入到功能文件中。 Its difficult to do partly because it goes against the whole ethos of writing good feature files. 之所以很难做到这一点,部分原因是它与编写好的功能文件的全部精神背道而驰。

The way Cucumber wants you to work is to push the details down and to abstract the process so that the feature is not doing any programming (looping, iterating over steps etc.). Cucumber希望您进行工作的方式是向下推细节并抽象化流程,以使功能不执行任何编程(循环,遍历步骤等)。 You can improve your practice here by thinking about what is in the json file and why you want to iterate over it. 您可以在这里通过考虑json文件中的内容以及为什么要对其进行迭代来改善实践。

Your json file seems to want to iterate over a number of coffee shops to see if they can make a coffee. 您的json文件似乎要遍历许多咖啡店,看看它们是否可以煮咖啡。 So your feature could give the group of coffee shops a name and then talk about whether the group of shops can do something. 因此,您的功能可以为一组咖啡店命名,然后再讨论一组咖啡店是否可以做些什么。 Perhaps something like 也许像

Scenario: Seattle coffee shops can make an iced mocha
  Given our coffee shops are located in Seattle
  Then our coffee shops can make an iced mocha

and implement the scenarios 并实施方案

Given 'our coffee shops are located in Seattle' do
  @coffee_shops = get_seattle_coffee_shops
end

Then 'our coffee shops can make an iced mocha' do
  @coffee_shops.each do | shop |
    assert can_make_recipe(
      shop: shop,
      recipe: Recipes::IcedMocha
    )
  end
end

The above is a very crude start, and I would pull out more code from the step definitions into helper methods. 上面是一个非常粗糙的开始,我将从步骤定义中提取更多代码到辅助方法中。 The key part I'm trying to illustrate here is that the scenario and steps are know working with a group of coffee shops rather than just one coffee shop 我要在此处说明的关键部分是,已知场景和步骤适用于一组咖啡店,而不仅仅是一个咖啡店

In your scenario the helper method get_seattle_coffee_shops would load and process your json to get your data. 在您的方案中,辅助方法get_seattle_coffee_shops将加载并处理json以获取数据。 Because this processing has been pushed down from the feature file (non-code) into code this operation is now much easier to implement. 由于此处理已从功能文件(非代码)下推到代码中,因此此操作现在更容易实现。 This "Pushing the How Down" is a very important technique when Cuking, and is how you should approach any problem which involves programming feature files. 在“压缩”时,这种“按下方式”是一项非常重要的技术,它是您应该如何解决涉及对功能文件进行programming任何问题的方法。

@abhinandan Mehandiratta were you successfully in implementing this. @abhinandan Mehandiratta 您是否成功实施了这一点。 If yes I need support on this please let me know如果是,我需要这方面的支持,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 空手道 Api 测试 - 如何将数据从一个特征文件传递到另一个特征文件 - Karate Api Testing - How to pass data from one feature file to another 如何从我的 testcafe 文件中的单独文件传递数据 - How to pass data from a seprate file in my testcafe file 测试从本地 JSON 文件中获取一些数据的异步操作创建器 - Testing async action creator that fetches some data from a local JSON file 如何在 Specflow 的同一功能文件中提供多个数据作为输入 - How to give multiple data as input in the same feature file in Specflow 如何从JSON文件中提取数据以编写测试用例 - How to extract the data from JSON file to write test case 如何使用 Appium 和 Cucumber 从特征文件导航到 Step 定义? - How to navigate to the Step definition from the feature file using Appium and cucumber? 我已经创建了一个.json文件,并希望使用放心的方法从该json文件中获取数据。 我该如何实现? - I have created a .json file and want to fetch the data from that json file using rest assured method. How can I achieve this? 如何测试 Laravel 中 JSON 文件中存在的嵌套 JSON 数据? - How to test a nested JSON data present in JSON file in Laravel? 文件中的硒传递值 - selenium pass value from a file 根据json文件中的列表test-data参数化测试 - Parametrize the test based on the list test-data from a json file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM