4. TestRunner and Test steps

To run features we need a Java class to start the test run. Here is a simple example:

package cucumberTest;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
features = "Feature",
dryRun = true
)

public class TestRunner {

}

The TestRunner class needs to be annotated with @RunWith(Cucumber.class) and a body of it can be totally empty. By annotating the class with @CucumberOptions we can specify extra options to our test run. In this case, we used the annotation to specify the location of the feature file and to set the test run to ‘dry’. Running the test in ‘dry’ mode means that cucumber will not execute the test steps, rather it will just check for test steps implementations. For the previously given feature, we got this output on the console.

You can implement missing steps with the snippets below:

@Given("^I have chosen to sign up$")
public void i_have_chosen_to_sign_up() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@When("^I sign up with valid details$")
public void i_sign_up_with_valid_details() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^I should receive a confirmation email$")
public void i_should_receive_a_confirmation_email() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^I should see a personalized greeting message$")
public void i_should_see_a_personalized_greeting_message() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Given("^I enter an email address that has already registered$")
public void i_enter_an_email_address_that_has_already_registered() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^I should be told that the email is already registered$")
public void i_should_be_told_that_the_email_is_already_registered() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^I should be offered the option to recover my password$")
public void i_should_be_offered_the_option_to_recover_my_password() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

If we create a new java class with the above printed template the test run result will look like:

Bdd Example

BDDExample

The test steps should be annotated with @Given, @When and @Then annotations. Steps with And and But prefix will get the previously listed step’s prefix as annotation. These annotations differ just logically, which means that any of those can be used at any place. However, good practice is to use logically proper ones to make the class structured and more human readable.

All test steps should be implemented in void methods, but they can have parameters. Test steps with parameters will be explained in a future chapter.

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>