BDD: встроенные столы с безмятежностью и jbehave

Я пытаюсь создать тест BDD с безмятежностью (бывший thucydides), используя расширение jbehave, это моя история (происходящая из примеров 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
}

Как мне получить аргументы таблицы в моем тесте?

Я попробовал параметры ExamplesTable в соответствии с документацией по табличным параметрам jbehave, но это не сработало.

Есть ли способ сделать аннотацию given более читаемой (не добавляя параметры таблицы)?


person phury    schedule 10.07.2015    source источник


Ответы (1)


Вы можете получить параметр ExampleTable следующим образом (и получить более читаемые аннотации):

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

Если он не находит объявленный метод и сообщает вам, что этот шаг ожидает выполнения, вы можете проверить, есть ли в вашей истории пробел после слова фрукт:

Given that I sell the following fruit 

Как получить доступ к нескольким строкам и столбцам в ваших таблицах описано в документации jBehave в разделе http://jbehave.org/reference/stable/tabular-parameters.html

Вы также можете подумать о создании только одной таблицы вместо трех:

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?

person spcial    schedule 13.07.2015
comment
Я пробовал несколько способов ввести $exampleTable в свой метод, но я думаю, что использовал <exampleTable> и не удалил конец сгенерированного имени метода (... FruitPriceApples500Pears600) - person phury; 21.07.2015
comment
Я хотел, чтобы первый пример работал, потому что я использую json в своих табличных данных, а ввод нескольких объектов json в каждой строке на самом деле не читается - person phury; 21.07.2015