jeudi 4 avril 2019

Cucumber - How to struct your tests steps?

I'm currently learning cucumber and in very simple test, i had some doubts: "How is the best way to organize my StepClasses.

This is my .feature:

Feature: How many potatoes have in the sack

Scenario: I put one potato in the Bag
    Given the bag has 10 potatoes
    When I put 1 potato
    Then I should be told 11 potatoes

  Scenario: I remove one potato from the Bag
    Given the bag has 10 potatoes
    When I remove 1 potato
    Then I should be told 9 potatoes

And my StepClass:

public class Stepdefs {

private Integer potatoesInTheBag;

@Given("^the bag has 10 potatoes$")
public void the_bag_has_10_potatoes(){
    this.potatoesInTheBag=10;
}

@When("^I put 1 potato$")
public void i_put_one_potato(){
    this.potatoesInTheBag = potatoesInTheBag + 1;
}

@Then("^I should be told (\\d+) potatoes$")
public void i_should_be_told_potatoes(int potatoes) throws Exception {
    assertEquals(potatoesInTheBag.intValue(),potatoes);
}

@When("^I remove 1 potato$")
public void i_remove_one_potato(){
    this.potatoesInTheBag = potatoesInTheBag - 1;
}

}

This example works fine, but should i_remove_one_potato() stay in here, or in another step class ? Another question, if i want to use Scenario Outline, how i would do that in this case ? Because the answers would be different although the potato added/remove would be the same. There are good practices that guide this process of structuring your cucumber tests ?

Thx

Aucun commentaire:

Enregistrer un commentaire