如何创建一个Cucumber DataTable? [英] How do I create a Cucumber DataTable?

查看:1697
本文介绍了如何创建一个Cucumber DataTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to manually set up a Cucumber DataTable using Java (instead of Gherkin).

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

In Gherkin, my table would look like this:

| h1 | h2 |
| v1 | v2 |

我的Java到目前为止:

My Java so far looks like this:

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

我得到的是一个DataTable标题,但没有内容。它也比预期的长:

What I get back is a DataTable with headers but no contents. It's also longer than expected:

  | h1| h2 |
  |   |    |
  |   |    |

我相信解决方案必须相当简单,但我有点失落现在。我需要做什么来获得我的表?

I'm sure the solution must be fairly simple, but I'm a bit at a loss right now. What do I need to do to get my table?

推荐答案

希望这有助于。如果你充分Gherkins步骤看起来像这样...

Hope this helps. If you're full Gherkins step looks like this...

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

(请注意,传入的 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)
}

我会说在AslakHellesøy的例子中,他实际上并不使用DataTable。

I will say that in the examples by Aslak Hellesøy he doesn't actually use the 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;
}

更多阅读的完整示例:


  • Gherkins计算器:链接

  • Java计算器步骤定义:链接

  • Gherkins calculator: link
  • Java calculator Step Definitions: link

编辑

对于@kristian的overkill,对于你可能不需要如何使用它在应用程序的整个上下文,只是一个干净的方式使用 DataTable.create 和我发布的大部分是另一个方式到皮肤那猫的Entry类(这可能有助于那些阅读此后。)

Sorry for the overkill @Christian, you perhaps didn't need the whole context of how to use it in an app, just a clean way to use DataTable.create and most of what I posted was another way to skin that cat with the Entry class (Which might be helpful for those reading this later.)

所以你在你的评论中的方式是不远处。我不是在集合,所以我不能给你任何提示,使你的2D列表的字符串,但我可以澄清最后两个参数(如果你使用所有4)。

So the way you did it in your comment wasn't far off. I'm no pro at collections, so I can't give you any tips on making your 2D List of Strings, but I can clarify on the last two parameters (if you're using all 4).


  • 如果您的列使用日期或日历字段,则可以指示
    Format in the second最后参数。

  • 如果您不为列名使用
    最后一个,那么它会自动读取列名称的顶行字符串。

  • 此外,您可以删除Locale.getDefault(),它会为您做;那么您正在查看:

  • If your columns are using Date or Calendar fields you can indicate a Format in the second to last param.
  • If you don't use the last one for Column Names, then it will automatically read your top line string for the column names.
  • Also you can drop the Locale.getDefault() and it will do that for you; so then you're looking at:

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

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

You could also use the constructor which would be equally messy. :)

这篇关于如何创建一个Cucumber DataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆