2

我正在尝试使用 jbehave 扩展创建一个带有宁静(前修昔底德)的 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
}

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

我试过了ExamplesTable按照关于 jbehave 表格参数的文档尝试了参数,但没有奏效。

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

4

1 回答 1

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

于 2015-07-13T12:53:12.307 回答