BDD:具有 serenity 和 jbehave 的嵌入式表

BDD: Embedded tables with serenity and jbehave

我正在尝试使用 jbehave 扩展创建一个带有 serenity(以前的 thucydides)的 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 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
}

如何在测试中检索 table 个参数?

我根据 jbehave 表格参数的文档尝试了 ExamplesTable 参数,但没有用。

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

您可以像这样检索 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 

如何访问 table 中的多个行和列已写在 http://jbehave.org/reference/stable/tabular-parameters.html

下的 jBehave 文档中

您也可以考虑只创建一个 table 而不是三个:

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 时究竟是什么不起作用?