3. Create Test Runner and Get Code Stubs

Applies to TestLeft 15.40, last modified on March 17, 2022

When the feature story is ready, you can run your test to get code snippets for step definitions.

1. Create Test Runner

To run your BDD test, you must use one of the available runners. In this tutorial, we will use JUnit, but you can use any other.

  1. Create a new JUnit class:

    Created JUnit class

    Click the image to enlarge it.

  2. Add the following annotations to the class:

    Java

    package com.example.cucumber.test;

    import org.junit.runner.RunWith;
    import cucumber.api.CucumberOptions;
    import cucumber.api.junit.Cucumber;

    @RunWith(Cucumber.class)
    @CucumberOptions(
        format = {"pretty", "html:target/Destination"},
        features={"src/test/resources/UndoFeature.feature"}
        )
    public class UndoFeatureTest {
    }

    • The RunWith annotation commands to run the specified class instead of the created JUnit class.

    • The CucumberOptions annotation specifies different options for the Cucumber test. In our case, we specify the following options:

      • format – specifies the output format for the test result.

      • features – specifies the path to the feature file to run.

        Tip: You can specify the path to a folder. In this case, Cucumber will run all the feature files stored in that folder.

2. Run Test

In IntelliJ IDEA, you can run JUnit tests in different ways: from the Run menu, from the Project panel, from the context menu of the code editor. In this tutorial, we will show you how to run tests as part of a Maven build.

  1. Open the Maven Projects panel.

    Note: If the Maven Project panel is hidden, select View > Tool Window > Maven Projects from the main menu.
  2. Select the test phase from the Lifecycle list.

  3. Click Run on the panel’s toolbar:

    Run the Maven build

    Click the image to enlarge it.

The Run panel shows the test log:

The test log

Click the image to enlarge it.

3. Get Method Stubs

Cucumber skips undefined test steps and generates code snippets for the missing test steps. You can find the generated snippets in the test log.

Example

  1. Create a new class in the same package as the test runner.

  2. Open the test log and copy the generated code snippets.

  3. Paste the code snippets to the created class. Now, you should have a class with a number of method stubs:

    Java

    public class UndoFeatureSteps {
        @Given("^I open Notepad$")
        public void i_open_Notepad() throws Exception {
            // Write code here that turns the phrase above into concrete actions
            throw new PendingException();
        }

        @Given("^there is no text in Notepad$")
        public void there_is_no_text_in_Notepad() throws Exception {
            // Write code here that turns the phrase above into concrete actions
            throw new PendingException();
        }

        @When("^I type \"([^\"]*)\"$")
        public void i_type(String arg1) throws Exception {
            // Write code here that turns the phrase above into concrete actions
            throw new PendingException();
        }

        @When("^I press \\[Ctrl \\+ z\\]$")
        public void i_press_Ctrl_z() throws Exception {
            // Write code here that turns the phrase above into concrete actions
            throw new PendingException();
        }

        @Then("^I should see no text in Notepad$")
        public void i_should_see_no_text_in_Notepad() throws Exception {
            // Write code here that turns the phrase above into concrete actions
            throw new PendingException();
        }
    }

    Each method corresponds to a test step and consists of the following elements:

    • Annotation - The @Given, @When or @Then annotation that specifies the block of the scenario the test step belongs to.

    • Annotation parameter - A regular expression that represents the text of the test step after a keyword.

    • Java method - The method to execute.

  4. Currently, there are errors in the class. To fix them, import the following classes:

    Java

    import cucumber.api.PendingException;
    import cucumber.api.java.en.Given;
    import cucumber.api.java.en.Then;
    import cucumber.api.java.en.When;

Now, you should have a test class with a bunch of method stubs that do not perform the needed actions. Let’s create test steps.

Prev     Next

See Also

Behavior-Driven Development
About Behavior-Driven Development

Highlight search results