BDD in Swift: How to Write Tests That Business Understands

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why Regular Tests Don't Save You, and What BDD Is

Every iOS developer has faced this situation: you've written a hundred unit tests, coverage is 90%, but at the next demo the manager says: "Why does nothing happen when I press this button?" You look at the code — everything works. The problem is that unit tests check code, not system behavior. This is where BDD (Behaviour-Driven Development) comes to the rescue — a methodology that blurs the line between "technical" testing and "business logic."

BDD in Swift is not just a buzzword. It's a way to write tests that can be read not only by a developer but also by an analyst, tester, or even a client. In this article, we'll break down how to implement behaviour-driven development in your iOS project using the Quick and Nimble frameworks, and show live examples in Swift.



What BDD Is in Simple Terms

Imagine you're not writing tests, but a script for a movie. The script has characters (user, system), events (button press, request submission), and an expected outcome (an alert appears, UI updates). BDD is exactly that kind of "script," but for your application.

The core idea: tests should describe system behavior from the user's perspective. Instead of "the saveUser() method returns true," you write "when the user enters a valid email and clicks 'Save,' the system shows a 'Success' message."



How BDD Differs from TDD

Many people confuse BDD with TDD (Test-Driven Development). The difference is simple:

  • TDD — write a test, then code, then refactor. Focus on correctness of implementation.
  • BDD — write a behavior scenario, then code that implements that behavior. Focus on value for the user.

BDD is an evolution of TDD. If TDD answers the question "does the code work correctly?", BDD answers "does the code do what the business needs?"



Tools for BDD in Swift

In the Swift world, there are two main pillars for behaviour-driven development:

  • Quick — a framework for writing tests in BDD style. It replaces standard XCTest and provides describe/context/it syntax.
  • Nimble — a library for matchers (assertions). Instead of XCTAssertEqual, you write expect(value).to(equal(5)) — it reads like an English sentence.

These tools work great together and allow you to write tests that look like a specification.



Installing Quick and Nimble via Swift Package Manager

Add dependencies to your Package.swift or via Xcode: File → Add Packages → enter the Quick repository URL: https://github.com/Quick/Quick.git and Nimble: https://github.com/Quick/Nimble.git.

Example for Package.swift:

// swift-tools-version:5.9import PackageDescription

let package = Package( name: "MyApp", dependencies: [ .package(url: "https://github.com/Quick/Quick.git", from: "7.0.0"), .package(url: "https://github.com/Quick/Nimble.git", from: "13.0.0"), ], targets: [ .target(name: "MyApp"), .testTarget( name: "MyAppTests", dependencies: ["MyApp", "Quick", "Nimble"] ) ])


Writing Your First BDD Test in Swift

Let's say we have a simple LoginService that checks login and password. Instead of writing a boring XCTest, let's create a test in BDD style.



Service Code (for context)

class LoginService {    func authenticate(username: String, password: String) -> Result<User, LoginError> {        guard !username.isEmpty else { return .failure(.emptyUsername) }        guard !password.isEmpty else { return .failure(.emptyPassword) }        guard pas                

Blogs

Book Recommendations