BDD:具有宁静和jbehave的嵌入式表格

时间:2015-07-10 12:37:07

标签: java bdd jbehave thucydides serenity-bdd

我正在尝试使用jbehave扩展创建一个带有宁静(前thucydides)的BDD测试,这是我的故事(源自宁静的jbehave示例)

Scenario: a scenario with embedded tables
Given that I sell the following fruit
| fruit  | price |
| apples | 5.00  |
| pears  | 6.00  |
And I sell the following vegetables
| vegetable | price |
| potatoe   | 4.00  |
| carrot    | 5.50  |
When I sell fruit
Then the total cost should be total
Examples:
| goods           | total |
| apples, carrot  | 11.50 |
| apples, pears   | 11.00 |
| potatoe, carrot | 9.50 |

生成的java代码如下:

@Given("that I sell the following fruit\r\n| fruit  | price |\r\n| apples | 5.00  |\r\n| pears  | 6.00  |")
public void givenThatISellTheFollowingFruitFruitPriceApples500Pears600() {
    // PENDING
}

@Given("I sell the following vegetables\r\n| vegetable | price |\r\n| potatoe   | 4.00  |\r\n| carrot    | 5.50  |")
public void givenISellTheFollowingVegetablesVegetablePricePotatoe400Carrot550() {
    // PENDING
}

@When("I sell fruit")
public void whenISellFruit() {
}

@Then("the total cost should be total")
public void thenTheTotalCostShouldBeTotal() {
    // PENDING
}

如何在测试中检索表参数?

我根据有关jbehave表格参数的文档尝试了ExamplesTable参数,但这些参数不起作用。

有没有办法让given注释更具可读性(通过不添加表格参数)?

1 个答案:

答案 0 :(得分:3)

您可以像这样检索ExampleTable参数(并且具有更多可读的给定注释):

@Given("that I sell the following fruit $exampleTable")
public void thatISellTheFollowingFruit(ExamplesTable exampleTable) {
    System.out.println("MyTable: "+exampleTable.asString());
}

如果它没有找到声明的方法并告诉您此步骤处于待处理状态,则可以在 fruit 之后检查您的故事中是否有空格:

Given that I sell the following fruit 

如何访问表中的多个行和列,请参阅http://jbehave.org/reference/stable/tabular-parameters.html下的jBehave文档

您还可以考虑只创建一个表而不是三个:

Scenario: a scenario with embedded tables
Given I sell <product1> 
And the price is <product1price>
And I sell <product2> 
And the price is <product2price>
When I sell something
Then the total cost should be <total>
Examples:
| product1 | product1price | product2 | product2price | total |
| apples   | 5.00          | carrot   | 6.50          | 11.50 |
| apples   | 5.00          | pears    | 6.00          | 11.00 |
| potatoe  | 4.00          | carrot   | 9.50          | 13.50

java代码必须看起来像这样才能访问参数:

@Given("I sell <product1>")
public void iSellProduct(@Named("product1") String product1) {
    //do something with product1
}

这有帮助吗?如果没有,当您尝试阅读exampleTable时究竟什么不起作用?