我们可以实现示例过滤吗?基于空手道

发布于 2025-01-22 15:27:39 字数 1320 浏览 1 评论 0原文

我们可以实现 jbehave

下面的示例:

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

我们需要实现的目标是:

  1. 空手道DSL以基于示例表中执行测试 空手道的当前值
  2. 。 '778983-110833-110834'} 如果我在开发环境中进行测试& {项目: '848079-419456-419457'}如果我在QA中进行测试。

我无法通过使用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 @qa” 在commandline for Environing = dev上触发:mvn test -dcucumber.options =“ - tags @dev”

请告诉我是否有其他方法可以实现它,因为我想使用karate.env财产。

Can we achieve 'Filtering on Examples' that we have on Jbehave

Sample below:

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

What we need to achieve is:

  1. Karate DSL to execute the tests in Examples table based on the
    current value of karate.env
  2. Karate must create a request = { item:
    '778983-110833-110834' }
    if i run tests in dev environment & { item:
    '848079-419456-419457' }
    if i run tests in qa.

I was unable to achieve this by using karate.env property but achieved it using the tags, please refer to example below:

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|

Triggering on commandline for environment=qa : mvn test -Dcucumber.options="--tags @qa"
Triggering on commandline for environment=dev : mvn test -Dcucumber.options="--tags @dev"

Please let me know if there is any other way of achieving it since i wanted to use karate.env property.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2025-01-29 15:27:39

我认为您正在寻找: https://github.com/intub.com/intuit/karate/karate#标签和例子

黄瓜 /小黄瓜语法的鲜为人知的能力将能够在一堆示例中标记特定行!您必须重复每个标签的示例部分。下面的示例将其与上述高级功能结合在一起。

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)文件中读取,您可以使用它来驱动示例: https://github.com/intuit/karate/karate#dynamic-scenario-scenario-senario-inline-cenrio-unline < /a>

最后,我不建议这样做太多 - 但是,如果您想实现逻辑以不运行特定karate.env值的测试,则可以通过 karate.abort()

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

只需在<<之后添加该行代码>方案:,并且在需要时会跳过测试。

I think you are looking for this: 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       |

EDIT: for those landing here trying to do this, I suggest another approach, you can call a feature like this:

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

Remember, Karate can read from JSON (or CSV) files and you can use that to drive Examples: https://github.com/intuit/karate#dynamic-scenario-outline

And finally, I don't recommend this much - but if you want to achieve the logic to NOT run a test for a particular karate.env value, you can do this via karate.abort():

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

Just add that line just after the Scenario: and the test will be skipped when needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文