黄瓜中的“可重用/通用示例”表

时间:2018-09-04 14:30:39

标签: cucumber bdd gherkin cucumber-java qaf

是否可以在多个方案中使用相同的“示例”表?

所以不要像下面这样:

Scenario Outline: First Scenario
    Given I am viewing "<url>"
    Then I assert that the current URL "<url>"
    Examples:
      | url                |
      | https://google.com |
      | https://twitter.com|

  Scenario Outline: Second Scenario
    Given I am viewing "<url>" with route "</contactus>"
    Then I assert that "<url>" contains "contactus"
    Examples:
      | url                |
      | https://google.com |
      | https://twitter.com|

我可以做类似

的操作
Scenario Outline: Reusable Example
    Examples:
      | url                |
      | https://google.com |
      | https://twitter.com|

  Scenario: First Scenario
    Given I am viewing "<url>"
    Then I assert that the current URL "<url>"

  Scenario: Second Scenario
    Given I am viewing "<url>" with route "</contactus>"
    Then I assert that "<url>" contains "contactus"

我发现了一个similar question on StackOverflow,但是我无法将所有场景合并到一个场景中。自从2014年发布此问题以来,也许框架中有了一些我不知道的进步:D

先谢谢您。

2 个答案:

答案 0 :(得分:2)

您可以使用qaf-gherkin在其中将示例移动到外部文件中,并将其用于一个或多个方案。使用qaf,您的功能文件可能如下所示:

Scenario Outline: First Scenario
   Given I am viewing "<url>"
   Then I assert that the current URL "<url>"
   Examples:{'datafile':'resources/testdata.txt'}

Scenario Outline: Second Scenario
Given I am viewing "<url>" with route "</contactus>"
Then I assert that "<url>" contains "contactus"
Examples:{'datafile':'resources/testdata.txt'}

您的数据文件将如下所示:

url
https://google.com
https://twitter.com

这里是reference

答案 1 :(得分:0)

您可以使用Background来指定对于所有方案都相同的步骤。 (请查看有关约束的链接)

功能文件可能看起来像

Feature: use of reusable Given

  Background: Reusable Example
    Given I am viewing url
      | https://google.com |
    And a search phrase is entered in the search field

  Scenario: First Scenario
    And step for first scenario

  Scenario: Second Scenario
    And step for second scenario

实现Given的粘合代码

@Given("^I am viewing url$")
public void iAmViewing(List<String> url) throws Throwable {
    System.out.println("url = " + url);
}

编辑:问题更新后,Scenario Outline可以同时适用于两个示例。

Feature: use of example

  Scenario Outline: First Scenario
    Given I am viewing "<host>" with path "<path>"
    Then I assert that the current URL is "<host><path>"

    Examples:
      | host                | path       |
      | https://google.com  | /          |
      | https://twitter.com | /contactus |