我们能否在 karate.env 的基础上实现 'Filtering on Examples'

Can we achieve 'Filtering on Examples' based on karate.env

我们能否达到 Jbehave

上的 'Filtering on Examples'

示例如下:

* def request = { item: '#(item)' }
Examples:
|karate.env:     |item                |
|@dev            |778983-110833-110834|
|@qa             |848079-419456-419457|

我们需要实现的是:

  1. 空手道 DSL 执行示例中的测试 table 基于 当前值 karate.env
  2. 空手道必须创建一个请求 = { item: '778983-110833-110834' } 如果我 运行 在开发环境中测试 & { item: '848079-419456-419457' } 如果我 运行 在质量检查中测试。

我无法使用 karate.env 属性 实现,但使用标签实现了,请参考下面的示例:

Feature:

  Background:
    * url 'https://reqres.in/api'
    * configure headers = { 'Content-Type': 'application/json'}

  Scenario Outline:
    * def reqJson = { "name": "name", "job": "<item>"}
    And path 'users'
    And request reqJson
    When method post
    Then status 201
    And match response.job == '<item>'

    @dev
    Examples:
      | item |
      |   111|

   @qa
    Examples:
      | item |
      |   222|

在环境=qa 的命令行上触发:mvn test -Dcucumber.options="--tags @qa" 在 environment=dev 的命令行上触发:mvn test -Dcucumber.options="--tags @dev"

因为我想使用 karate.env 属性.

如果有任何其他方法可以实现它,请告诉我

我想你正在寻找这个:https://github.com/intuit/karate#tags-and-examples

A little-known capability of the Cucumber / Gherkin syntax is to be able to tag even specific rows in a bunch of examples ! You have to repeat the Examples section for each tag. The example below combines this with the advanced features described above.

Scenario Outline: examples partitioned by tag
* def vals = karate.tagValues
* match vals.region[0] == '<expected>'

  @region=US
  Examples:
    | expected |
    | US       |

  @region=GB
  Examples:
    | expected |
    | GB       |

编辑:对于那些登陆这里尝试这样做的人,我建议另一种方法,你可以这样调用一个功能:

* call read('foo-' + karate.env + '.feature')

请记住,空手道可以读取 JSON(或 CSV)文件,您可以使用它来驱动 Examples: https://github.com/intuit/karate#dynamic-scenario-outline

最后,我不推荐这么多 - 但如果你想实现不 运行 测试特定 karate.env 值的逻辑,你可以通过 karate.abort():

* if (karate.env == 'prod') karate.abort()

只需在 Scenario: 之后添加该行,测试将在需要时跳过。