3 min read

Frameworks - BDD (cucumber)

Behavior Driven Development framework i.e. Behavior Driven Development is a software development approach that allows the tester/business analyst to create test cases in simple text language (English).

The simple language used in the scenarios helps even non-technical team members to understand what is going on in the software project. This helps and improves communication among technical and non-technical teams, managers, and stakeholders.

Advantages of cucumber.

1. It is helpful to involve business stakeholders who can't easily read code.

2.  Cucumber focuses on end-user experience

3.  Style of writing tests allow for easier reuse of code in the tests

4.  Quick and easy set up and execution

5.  Efficient tool for testing


What is background in cucumber?

Background allows you to add some context to the scenarios in a single feature. A Background is much like a scenario containing a number of steps. The difference is when it is run. The background is run before each of your scenarios but after any of your Before Hooks.

Example:
Feature: Multiple site support
  Background:
    Given a global administrator named "Greg"
    And a blog named "Greg's anti-tax rants"
    And a customer named "Dr. Bill"
    And a blog named "Expensive Therapy" owned by "Dr. Bill"
 
  Scenario: Dr. Bill posts to his own blog
    Given I am logged in as Dr. Bill
    When I try to post to "Expensive Therapy"
    Then I should see "Your article was published."

What are hooks in cucumber?

Hooks are blocks of code that run before or after each scenario. You can define them anywhere in your project or step definition layers, using the methods @Before and @After. Cucumber Hooks allows us to better manage the code workflow and helps us to reduce code redundancy.

It supports only two hooks (Before & After) which works at the start and the end of the test scenario. As the name suggests, @before hook gets executed well before any other test scenario, and @after hook gets executed after executing the scenario.

n fact, as you noticed already, their structures are a little bit different. The common practice is to use them as follows:

  • Use Background when you provide customer-readable pre-conditions to your scenarios
  • Use Before when you have to do some technical setup before your scenarios

But the main thing to understand here is the order of the operations:

Before Hook 1 -> Before Hook 2 -> ... -> Background -> Scenario

They just represent different levels of pre-conditions.


What is the use of when, then, and, but?

Scenario: Multiple Givens
  Given one thing
    And another thing
    And yet another thing
  When I open my eyes
  Then I see something
    But I don't see something else

To Cucumber steps beginning with And or But are exactly the same kind of steps as all the others.


How to implement steps of feature file/ what is dry run?

The dry run is when, after writing feature file, the code is run, even before the development of step defiitions. This does not execute the actual test cases, but does a strict dry run instead.A dry run tests the Gherkin syntax for validity. Doing this strictly also checks for all Cucumber glue code (step definitions) being available.


What is glue?

We need a Runner File to run the test, it contains the location of Feature Files and the package containing the corresponding Step Files.Thus it glues the Feature Files with the Step Files and glues both with the cucumber runtime.

Note:We need to specify the package for the Step Files (NOT the specific Java file name).It suggests that there is no One-To-One mapping between Feature File and Step File by their naming convention.

Multiple feature file can be implemented by single Step File having methods with matching pattern for each of the steps in those Feature Files.

Single Feature File can be implemented by multiple Step Files as the Step Methods can be distributed among those Step Files; only restriction is that all those Step Files are to be in the specified package.

Matching Step Methods are searched across the specified package, Hence even if we specify two differently named methods with same regex pattern in two different Step Files in same package, then also at runtime error will occur (DuplicateStepException).

Step Methods are reusable. Means to say, for same Steps written in different Step Scenarios or even different Feature Files (generally Given steps can be like that) we need to write only one Step Method.


What is cucumber options?

The JUnit/TestNG Runner can pick up configuration options defined via the @CucumberOptions annotation. For example, if you want to tell Cucumber to use the two formatter plugins pretty and html, you would specify it like this:

@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty", "html:target/cucumber"})
public class RunCukesTest {}

Run Cucumber Test

@RunWith(Cucumber.class)
@CucumberOptions(
  
  plugin = {"pretty", "html:target/html-report", "json:target/json-report.json", "rerun:target/rerun.txt" },
  monochrome=true,
  features="./src/java/features/feature1.feature",
  glue= {"com.steps"},
  tags="@test2")

public class TestRunner {
}

Run Cucumber Failed Test

@RunWith(Cucumber.class)
@CucumberOptions(
  plugin = {"pretty", "html:target/html-report", "json:target/json-report.json", "rerun:target/rerun.txt" },
  monochrome=true,
  glue= {"com.steps"},
  features = "@target/rerun.txt" //Cucumber picks the failed scenarios from this file

  )
public class FailedTestsRunner {
}