如何创建Cucumber DataTable?

Chr*_*ian 10 cucumber gherkin cucumber-jvm

我想使用Java(而不是Gherkin)手动设置Cucumber DataTable.

在小黄瓜,我的桌子看起来像这样:

| h1 | h2 |
| v1 | v2 |
Run Code Online (Sandbox Code Playgroud)

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

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

我得到的是带有标题但没有内容的DataTable.它也比预期的要长:

  | h1| h2 |
  |   |    |
  |   |    |
Run Code Online (Sandbox Code Playgroud)

我确信解决方案必须相当简单,但我现在有点不知所措.拿桌子需要做什么?

Eri*_*son 11

希望这可以帮助.如果你是满的Gherkins步骤看起来像这样......

When I see the following cooked I should say:
  | food  | say     |
  | Bacon | Yum!    |
  | Peas  | Really? |
Run Code Online (Sandbox Code Playgroud)

你想在Java中使用它.(请注意,传入的cucumber.api.DataTable在测试之前设置了您的预期值).

@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)
}
Run Code Online (Sandbox Code Playgroud)

我会说,在AslakHellesø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;
}
Run Code Online (Sandbox Code Playgroud)

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

编辑:

对于矫枉过正的@Christian,您可能不需要在应用程序中使用它的整个上下文,只是一种使用DataTable.create的简洁方法,而且我发布的大部分内容都是另一种方式来使用条目来修饰那只猫class(对于那些稍后阅读的人可能会有所帮助.)

所以你在评论中这样做的方式并不遥远.我不是专业人士,所以我不能给你任何关于制作你的2D字符串列表的提示,但我可以澄清最后两个参数(如果你使用全部4个).

  • 如果您的列使用日期或日历字段,则可以在倒数第二个参数中指示 格式.
  • 如果您不使用 最后一个列名称,那么它将自动读取列名称的顶行字符串.
  • 您也可以删除Locale.getDefault(),它会为您执行此操作; 所以你看着:

.

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

你也可以使用同样混乱的构造函数.:)