如何创建 Cucumber 数据表?

2023-12-30

我想使用 Java(而不是 Gherkin)手动设置 Cucumber 数据表。

在 Gherkin 中,我的表格如下所示:

| h1 | h2 |
| v1 | v2 |

到目前为止,我的 Java 看起来像这样:

List<String> raw = Arrays.asList( "v1", "v2");
DataTable dataTable = DataTable.create(raw, Locale.getDefault(), "h1", "h2");

我得到的是一个带有标题但没有内容的数据表。它也比预期的要长:

  | h1| h2 |
  |   |    |
  |   |    |

我确信解决方案一定相当简单,但我现在有点不知所措。我需要做什么才能拿到我的桌子?


希望这可以帮助。如果你吃饱了小黄瓜步骤看起来像这样......

When I see the following cooked I should say:
  | food  | say     |
  | Bacon | Yum!    |
  | Peas  | Really? |

你想要这个在 Java 中。 (请注意,黄瓜.api.数据表 http://cukes.info/api/cucumber/jvm/javadoc/cucumber/api/DataTable.html传入的是测试前使用您的预期值设置的)。

@When("^I see the following cooked I should say:$")
public void theFoodResponse(DataTable expectedCucumberTable) {
    // Normally you'd put this in a database or JSON
    List<Cuke> actualCukes = new ArrayList();
    actualCukes.add(new Cuke("Bacon", "Yum!"));
    actualCukes.add(new Cuke("Peas", "Really?")); 

    Another link to a Full Example.diff(actualCukes)
}

我要说的是,在 Aslak Hellesøy 的示例中,他实际上并没有使用 DataTable。

他会按照你的例子做这样的事情:

@When("^I see the following cooked I should say:$")
public void theFoodResponse(List<Entry> entries) {
    for (Entry entry : entries) {
        // Test actual app you've written
        hungryHuman.setInputValue(entry.food);
        hungryHuman.setOutputValue(entry.say);
    }
}

public class Entry {
    String food;
    String say;
}

有关更多阅读的完整示例,请查看:

  • 小黄瓜计算器:link https://github.com/cucumber/cucumber-jvm/blob/master/examples/java-calculator/src/test/resources/cucumber/examples/java/calculator/basic_arithmetic.feature
  • Java计算器步骤定义:link https://github.com/cucumber/cucumber-jvm/blob/master/examples/java-calculator/src/test/java/cucumber/examples/java/calculator/RpnCalculatorStepdefs.java

EDIT:

抱歉,@Christian 的杀伤力太大,您可能不需要如何在应用程序中使用它的整个上下文,只需要一种干净的使用方式数据表.create我发布的大部分内容是使用 Entry 类给猫剥皮的另一种方法(这可能对稍后阅读本文的人有所帮助。)

所以你在评论中所做的事情并不遥远。我不是集合方面的专家,所以我不能给你任何关于制作 2D 字符串列表的提示,但我可以澄清最后两个参数(如果你使用全部 4 个参数)。

  • 如果您的列使用日期或日历字段,您可以指定Format https://github.com/cucumber/cucumber-jvm/blob/master/core/src/main/java/cucumber/api/Format.java?source=cc in the 倒数第二参数。
  • 如果您不使用last one对于列名称,那么它将自动读取列名称的顶行字符串。
  • 您也可以删除 Locale.getDefault(),它会为您执行此操作;那么你正在看:

.

List<List<String>> infoInTheRaw = Arrays.asList( Arrays.asList("h1", "h2"),
    Arrays.asList("v1", "v2") ); 
DataTable dataTable = DataTable.create(infoInTheRaw);

您还可以使用同样混乱的构造函数。 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何创建 Cucumber 数据表? 的相关文章

随机推荐