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



我正在尝试使用jbehave扩展创建一个带有serenity(前修昔底德)的BDD测试,这是我的故事(源自serenity 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 fruitrn| fruit  | price |rn| apples | 5.00  |rn| pears  | 6.00  |")
public void givenThatISellTheFollowingFruitFruitPriceApples500Pears600() {
    // PENDING
}
@Given("I sell the following vegetablesrn| vegetable | price |rn| potatoe   | 4.00  |rn| 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注释更具可读性(不添加表参数)?

您可以像这样检索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时,究竟是什么不起作用?

最新更新