Test-Driven Development (TDD) and Behavior-Driven Development (BDD); the difference between them.
Test-Driven Development; This Is a software development approach in which you develop test cases to specify and validate what the code should do. In simple terms, you create test cases for each functionality first and then if the test fails then the new code is written to pass the test. This makes code simple and bug-free. Test-Driven Development starts with designing and developing tests for every small functionality of an application. TDD framework instructs developers to write new code only if an automated test has failed. This avoids duplication of code.
Test-Driven development is a process of developing and running automated tests before the actual development of the application. TDD is sometimes called Test First Development.
We write the Test first and the Test acts as the specification. The specification is not on a piece of paper, it's actual code and we can run it and verify that we have achieved our goal.
The main is to write the test first and THEN write your production code. You do it often iteratively.
"You write the test so that you can then write production code. You don't write the test to test your code."
The Sequence of Steps;
Read the requirements and Add Test cases,
Run Test that will fail immediately
Write the necessary code to pass the test as quickly as possible
Run Test again
Refactor Code
Repeat
How to perform TDD Test?
Add Test
Execute Test
Write your Code
Repeat
Behaviour Driven Development; is an Agile software development methodology in which an application is documented and designed around the behaviour a user expects to experience when interacting with it. The BDD is written in plain English.
Example of a BDD Test;
Feature: Login functionality
As a customer
To use the application
I want to log in with email and password
Scenario: Logging in with invalid credentials
Given I am at the Account/Login Page
When I fill the account email textbox with the value "incorrect@mail.com"
And fill the password textbox with the value "incorrectpassword"
And click the login button
Then a text "Can't login! Wrong email or password." Should appear in the validation errors region.
Scenario: Logging in with valid credentials
Given I am at the Account/Login Page
When I fill the account email textbox with the value "myname@mymail.com"
And fill the password textbox with the value "mypassword"
And click the login button
Then I should be on the home page
Difference between TDD and BDD
- In TDD Test cases are written by developers in a programming language
, while in BDD the Test cases are written by Stakeholders(also users) in plain English
- The TDD is written to test each unit (module) of the code
, while in BDD we test end-to-end expectations from the Software.
- The TDD is suited for Unit Testing while the BDD is suitable for Acceptance Testing.