Test-Driven Development (TDD) and Behaviour-Driven Development
(BDD)
TDD (test driven development) and BDD (behaviour driven development) are unique
software development techniques that differ in what they are testing and how they are
testing it. Despite their similar names, they serve distinct purposes.
What Is TDD?
TDD stands for test driven development. TDD is the process of writing a test for a specific
portion of functionality, allowing the test to run to determine failures, and then adjusting
the code as necessary to remedy the failures. Using these tests, developers can ensure
that they’ve written functional, dependable code. Additionally, if other developers need
to use components of the code, they can run tests to confirm their code’s functionality.
TDD Process
There are several steps taken to execute test driven development:
1. The developer writes automated test
cases to test the lines of code.
2. These tests are then executed to
determine the location of any failures in
the program.
3. Changes are subsequently applied
(refactoring) to ensure that the failures
are corrected and do not occur again in
the future.
4. Finally, tests are executed again until
they are passed without error.
1.1 Test-Driven Development cycle
Benefits of TDD
There are many benefits to using TDD instead of another style of development, such as:
Rework time reduced: Test driven development does not allow new code to be
written unless the existing code is successfully tested without failures. Until the
failures are completely addressed and removed, the process of writing code is
halted. Therefore, time spent reworking broken code is kept at a minimum.
Quick feedback: As the tests focus on specific sections of code at a time,
developers can receive more immediate feedback that allows them to implement
changes more quickly.
Increased development productivity: With TDD, the focus is on the production of
functional code rather than test case design. As such, productivity increases, and
development moves along smoothly.
More flexible, maintainable code: Because every part of the code is tested before
moving on to the next part of the software development process, the code
maintains functionality and is adaptable in the future.
Test Driven Development Example
To better understand how test driven development works in development, it’s helpful to
go through an example. In this instance, we can define a class password to meet the
condition that it must be 5–10 characters.
Your first step would be to write the code that fulfils the necessary requirements. Then
you run the tests to ensure that the code is valid. For example, you create a class to test
the password length, run the test, and then check to see if the output is true or false
according to the conditions that you set—whether or not the length of the password is 5–
10 characters. If the test returns false, then you can adjust the code as necessary.
What Is BDD?
BDD stands for behaviour driven development, and it is a way for teams of software
developers and others to work together to narrow the distance between the business-
focused team members and technical-focused people through:
Encouragement of teamwork across roles to increase understanding of what the
problem at hand is
The use of rapid, small iterations of work done to advance feedback and enhance
value flow
Producing system documentation that is automatically checked against the
system’s behaviour
BDD typically involves the software developers, test engineers, and project managers as
well as other stakeholders. This group collaborates to develop concrete examples of
acceptance criteria in a user story. Using a domain-specific language, these examples are
then described and put into a feature file. This feature file is subsequently converted into a
specification that can be executed, thereby allowing developers to write actual tests that
can also be executed.
BDD Process
The steps to behaviour driven development are fairly simple and repeatable as necessary:
1. Behaviour is described typically
by utilizing a user story. This
allows the team to discuss
concrete examples of the new
functionality so that everyone can
agree on the expectations of the
behaviour.
2. Action is then written by turning
the examples into documentation
in such a way that it can be
automated.
3. The test is executed to assist the developers1.2and
BDDguide them Process
Development through the
cycle
development of the code.
4. The code is then created to make the action pass and make the code functional.
Benefits of BDD
There are several benefits to making use of BDD for software development, including:
Incorporation of user experience: BDD focuses on the users’ experience and, as
such, allows the team to develop a broader perspective and make note of gaps in
their understanding.
Cost-effectiveness: Because BDD sets priorities for users, developers, and
investors alike, it allows for resources to be used optimally in the development of
programs.
Simple cross-browser testing: BDD focuses on behaviour, which means that it
gives you an ideal framework for cross-browser testing.
Behaviour Driven Development Example
Behaviour driven development follows the “Given-when-then” framework. For example, if
your site has a language translation feature and provides translations of the entire
webpage in Italian, Spanish, and French, you could encounter scenarios such as the
following:
Scenario: Receive Messages in my Set Language (Italian)
o Given I am the user “fallow”
o When the system sends the message “Invalid Login”
o Then I should see the error message “Login non valido”
Scenario: Change the language I use
o Given I am the user “dmessina”
o When I set my language to “Spanish”
o Then my language should be equal to “Spanish”