Behaviour Driven vs Test Driven Development (BDD vs TDD)
What is TDD?
TDD (Test-Driven Development) is a software development approach that relies on writing tests before writing the actual code. TDD is a process where the developer writes an automated test case that defines the expected behaviour of the code and then writes the actual code that passes the test case. TDD is primarily focused on ensuring the code meets the specific requirements of the test cases.
Test cases are written in the format known as Arrange-Act-Assert (AAA)
. It describes the environment setup, invokes test methods, & verifies the expected output of a feature or function.
Example:
Feature: User Authentication
Arrange: a user with the credentials.
Act: with invoking authentication method.
Assert: on returned result, (success/failure)
What is BDD?
BDD (Behavior-Driven Development) is a software development approach that extends TDD by placing more emphasis on the behaviour of the system rather than the specific features or requirements.
BDD emphasizes collaboration between developers, testers, Business Analyst & Customer stakeholders to ensure that the system's behaviour is aligned with the business goals. BDD requires that all participants use a common language to describe the behaviour of the system in terms of user stories, acceptance criteria and scenarios.
The scenarios are typically written in a specific format known as Given-When-Then (GWT)
. It describes the context, action, and expected outcome of a particular feature or function.
Overall, BDD helps to align the development process with business goals and customer needs and ensures that software is developed in a way that meets those requirements.
Example:
Feature: User Authentication
Expected behaviour: As a user, I want to log in to the system so that I can access my account.
Scenario: Valid credentials
Given: a user with valid credentials.
When: the user logs in with their credentials.
Then: the user is directed to their account page.
Scenario: Invalid credentials
Given: a user with invalid credentials.
When: the user logs in with their credentials.
Then: an error message is displayed.
TDD implementation
class LoginTests: XCTestCase {
func testLoginSuccess() {
// Arrange
let username = "peter.parker"
let password = "V@lidPassw0rd"
let authService = AuthService()
// Act
let result = authService.login(username: username, password: password)
// Assert
XCTAssertTrue(result.success)
XCTAssertEqual(result.user.username, username)
}
func testLoginFailure() {
// Arrange
let username = "peter.parker"
let password = "inV@lidPassw0rd"
let authService = AuthService()
// Act
let result = authService.login(username: username, password: password)
// Assert
XCTAssertFalse(result.success)
XCTAssertEqual(result.error, .invalidCredentials)
}
}
BDD Implementation
class LoginScreen {
var username: String
var password: String
func enterUsername(_ username: String) {
self.username = username
}
func enterPassword(_ password: String) {
self.password = password
}
func tapLoginButton() {
let authService = AuthService()
authService.login(username: username, password: password)
}
}
class HomeScreen {
func isDisplayed() -> Bool {
return true
}
}
class LoginTests: XCTestCase {
func testSuccessfulLogin() {
// Given
let loginScreen = LoginScreen()
let username = "testuser"
let password = "testpassword"
// When
loginScreen.enterUsername(username)
loginScreen.enterPassword(password)
loginScreen.tapLoginButton()
// Then
let homeScreen = HomeScreen()
XCTAssertTrue(homeScreen.isDisplayed())
}
}
Hope you will start using both BDD & TDD
in your daily development routine.
You can reach out to me via Linked In or https://nasirmomin.web.app
Subscribe to my newsletter
Read articles from Nasir directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Nasir
Nasir
App developer in iOS & Flutter