Nothing Special   »   [go: up one dir, main page]

BDD and Cucumber: With Interview Questions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

BDD and Cucumber

With Interview Questions

Japneet Sachdeva
What is BDD?
BDD (Behavior Driven Development) is a software development methodology that emphasizes defining the
expected behavior of the software using human-readable language, facilitating collaboration between
technical & non-technical stakeholders.

Primary focus of BDD is collaboration between stakeholders like - Product Owner, Business Analyst, QA and
Developers.

Japneet Sachdeva
What is Feature file?
Features file contain high level description of the Test Scenario in simple language.
It is known as Gherkin. Gherkin is a plain English text language.
Example - Feature: Visit career guide page in career.guru99.com
Scenario: Visit career.guru99.com
Given: I am on career.guru99.com
When: I click on career guide menu
Then: I should see career guide page

Japneet Sachdeva
Keywords used in Feature file?
Cucumber Feature File consist of following components –
● Feature: A feature would describe the current test script which has to be executed.
● Scenario: Scenario describes the steps and expected outcome for a particular test case.
● Scenario Outline: Same scenario can be executed for multiple sets of data using scenario outline. The
data is provided by a tabular structure separated by (I I).
● Given: It specifies the context of the text to be executed. By using datatables “Given”, step can also be
parameterized.
● When: “When” specifies the test action that has to performed
● Then: The expected outcome of the test can be represented by “Then”

Japneet Sachdeva
What is Step Definition file?
Step definition maps the Test Case Steps in the feature files(introduced by Given/When/Then) to code. For a step
definition to be executed, it must match the given component in a feature. Cucumber finds the Step Definition
file with the help of the Glue code in Cucumber Options.

Example - @Given("User is on Home Page")


public void user_is_on_Home_Page() throws Throwable {
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://google.com");
}

Japneet Sachdeva
What is Step Definition file?
Step definition maps the Test Case Steps in the feature files(introduced by Given/When/Then) to code. For a step
definition to be executed, it must match the given component in a feature. Cucumber finds the Step Definition
file with the help of the Glue code in Cucumber Options.

Example - @Given("User is on Home Page")


public void user_is_on_Home_Page() throws Throwable {
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://google.com");
}

Japneet Sachdeva
What is Test Runner file?
Test runner is used to run your code. This file is executed which helps to execute your step definition code
defined by feature file.

So basic understanding is - Create Feature file -> Create step definition for feature file -> Run your code using
Test Runner

Basic Example - @RunWith(Cucumber.class)


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

Japneet Sachdeva
How different files are created?
Feature files are usually created by business owners - by Business analysts or Product Owner

Step Definitions and Test Runners are created by - QA Automation Engineers/SDET

If in your organization - Feature files are created by QA then its not correct way of collaboration and instead a
different approach like TDD should be used.

Japneet Sachdeva
Interview Questions
Q1) Can you explain the difference between a feature & a scenario in BDD?

A feature is a high-level description of a functionality or behavior of the software being developed. It describes
the business value & purpose of the functionality. A scenario, on the other hand, is a specific example that
demonstrates the behavior of a feature in a particular situation or context.

Q2) Can you describe the three core elements of BDD: Given, When, & Then?

Given represents the initial state of the system or application, including any preconditions or setup required for
the scenario.
When represents the action or event that triggers the behavior being tested.
Then represents the expected outcome or result of the behavior being tested.

Japneet Sachdeva
Interview Questions
Q3) How do you handle data-driven testing in BDD scenarios?
To handle data-driven testing in BDD scenarios, you can use Scenario Outlines and Examples tables to create reusable,
parameterized tests that cover multiple input combinations without duplicating code.
In a Scenario Outline, you define placeholders using angle brackets (<>) within the Given-When-Then steps, which
represent the variable data points.
Then, provide an Examples table beneath the Scenario Outline, listing the different sets of values for the placeholders.

Q4) What is a hook in Cucumber?


A hook in Cucumber is a mechanism that allows developers to execute specific code snippets at different points during
the test execution lifecycle.
Hooks can be used for tasks such as setting up test data, managing browser sessions, or cleaning up resources after tests.
Common hooks include Before, After, BeforeStep, and AfterStep, which are executed before or after a scenario or
individual steps, respectively.

Japneet Sachdeva
Interview Questions
Q5) What is the purpose of the Behaviour Driven Development (BDD) methodology in the real world?
Answer: BDD is a methodology to understand the functionality of an application in the simple plain text
representation.

The main aim of the Behavior Driven Development framework is to make various project roles such as Business
Analysts, Quality Assurance, Developers, Support Teams understand the application without diving deep into the
technical aspects.

Q6) What is the use of Background keyword in Cucumber?

Answer: Background keyword is used to group multiple given statements into a single group. This is generally
used when the same set of given statements are repeated in each scenario of the feature file.

Japneet Sachdeva
If you need more such posts then keep
following @Japneet Sachdeva on LinkedIn
Happy Learning, Cheers!!

Japneet Sachdeva

You might also like