Professional Documents
Culture Documents
10.10.23 - Agenda - TDD-BDD
10.10.23 - Agenda - TDD-BDD
10.10.23 - Agenda - TDD-BDD
c. Refactor and optimize: Once the test passes, you can refactor and optimize
the code while ensuring that the test continues to pass. This helps maintain and
improve code quality.
d. Repeat: Continue this cycle, adding more tests and code in small increments,
ensuring that all tests pass at each step. This iterative process leads to a
comprehensive suite of tests and well-structured code.
Behavior Driven Development (BDD) :
BDD is an extension of TDD that shifts the focus from testing to describing the
behavior of the system from a user's perspective. It emphasizes collaboration
between developers, testers, and domain experts (often referred to as
"stakeholders"). BDD typically involves the following components:
BDD helps bridge the communication gap between technical and non-technical
team members by providing a shared language for discussing system behavior.
It promotes a more holistic view of the software and its interactions with users.
Summary, TDD focuses on
writing tests to verify the correctness
of individual code components, while
BDD focuses on specifying the
expected behavior of the entire
system from a user's perspective.
Both methodologies promote testing
and can lead to more robust and
maintainable software when
implemented effectively. The choice
between TDD and BDD often
depends on the project's specific
requirements and the team's
preferred approach to development
and collaboration.
Example: TDD Scenario: Adding Two Numbers
```java
public void testAddition() {
Calculator calculator = new Calculator();
int result = calculator.add(2, 3);
assertEquals(5, result);
}
```
2. **Write the Minimum Code:**
- Now, you implement the `Calculator` class with the `add` method
to make the test pass.
```java
public class Calculator {
public int add(int a, int b) {
return a + b;
}
}
Example: BDD Scenario: Adding Two Numbers - This scenario describes the behavior of adding two numbers using the calculator.
2. **Automation:**
- You then automate the scenario using a BDD framework, linking it to code that implements
the behavior.
**Refactor and Optimize:** ```java
- Once the test passes, you can refactor and optimize the code @Given("I have a calculator")
if needed. public void i_have_a_calculator() {
// Initialization code for the calculator
}
**BDD Approach:**
@When("I add {int} and {int}")
1. **Write a Scenario:** public void i_add(int a, int b) {
// Code to perform addition
- In BDD, you start by writing a scenario using a BDD framework int result = calculator.add(a, b);
like Cucumber in plain language. // Store the result for later verification
context.setResult(result);
```gherkin }
Scenario: Adding two numbers @Then("the result should be {int}")
Given I have a calculator public void the_result_should_be(int expected) {
When I add 2 and 3 int actual = context.getResult();
assertEquals(expected, actual);
Then the result should be 5 }
```
**Collaboration:**
- Throughout the process, team members, including non-technical stakeholders, can collaborate to define and refine the
scenario, ensuring it aligns with the desired behavior.
Why BDD Framework?
What is Cucumber?
Cucumber is a testing framework which
supports Behavior Driven Development (BDD). It lets us
define application behavior in plain meaningful English
text using a simple grammar defined by a language
called Gherkin. Cucumber itself is written in Ruby, but it
can be used to “test” code written in Ruby or other
languages including but not limited
to Java, C# and Python.