BDD in C++: Behaviour-Driven Development for Bold Programmers

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

What is BDD and Why It's Not Just Another Fad?

\n\n

Have you ever written code that works perfectly from the compiler's perspective but completely fails to do what the business needs? Familiar with the situation when a manager looks at a finished feature and says, "Well, I thought it would be different"? If so — welcome to the world of BDD (Behaviour-Driven Development). It's not just a methodology, but a bridge between "I want the button to be green" and "here's a test that fails if the button isn't green".

\n\n

Unlike classic TDD (Test-Driven Development), where we think of tests as function verification, BDD focuses on system behavior from the user's perspective. In the context of C++, this is especially valuable: the language is complex, projects are huge, and rewriting logic after a misunderstanding with the client is an expensive pleasure. BDD in C++ helps kill two birds with one stone: get documentation that even a project manager can understand, and a set of automated tests that won't let business logic break.

\n\n

In this article, we'll break down how to implement behaviour-driven development in a real C++ project. No fluff, with code and specific techniques.

\n\n

BDD Basics: The Gherkin Language and Its Role in C++

\n\n

The heart of BDD is scenarios written in the Gherkin language. They look something like this:

\n\n
Feature: Transferring funds between accounts\n  Scenario: Successful transfer with sufficient balance\n    Given the user has account A with a balance of 1000 rubles\n    And the user has account B with a balance of 0 rubles\n    When the user transfers 500 rubles from account A to account B\n    Then the balance of account A becomes 500 rubles\n    And the balance of account B becomes 500 rubles
\n\n

Yes, this isn't C++. But that's the magic: the scenario is written in human language, and then "bound" to real C++ code through steps. Each line of Given, When, Then turns into a function that calls your classes and checks the results.

\n\n

For C++, there are several frameworks that can work with Gherkin. The most popular ones:

\n\n
    \n
  • Catch2 with BDD style — lightweight, built into Catch2, requires no external parsers.
  • \n
  • Cucumber-CPP — a full implementation of Cucumber for C++, parses .feature files directly.
  • \n
  • Boost.Test — also supports BDD-like syntax, but is less flexible.
  • \n
\n\n

Personally, I recommend starting with Catch2: it's already in 90% of C++ projects, and for BDD you don't need to install anything extra. If the project is large and requires integration with business analysts — go with Cucumber-CPP.

\n\n

BDD with Catch2: Writing Scenarios Directly in Code

\n\n

Catch2 provides the macros SCENARIO, GIVEN, WHEN, THEN, and AND_WHEN/AND_THEN. This isn't pure Gherkin, but the style is very close. Let's write a test for a simple bank account.

\n\n

First — the account class:

\n\n
// account.h\n#pragma once\n#include <string>\n#include <stdexcept>\n\nclass Account {\npublic:\n    explicit Account(std::string owner, double initial_balance = 0.0)\n        : owner_(std::move(owner)), balance_(initial_balance) {}\n\n    void deposit(double amount) {\n        if (amount <= 0) throw std::invalid_argument("Amount must be positive");\n        balance_ += amount;\n    }\n\n    void withdraw(double amount) {\n        if (amount > balance_) throw std::runtime_error("Insufficient funds");\n        if (amount <= 0) throw std::invalid_argument("Amount must be positive");\n        balance_ -= amount;\n    }\n\n    double balance() const { return balance_; }\n    const std::string& owner() const { return owner_; }\n\nprivate:\n    std::string owner_;\n    double balance_ = 0.0;\n};
\n\n

And now — a BDD test using Catch2:

\n\n
// tes                

Blogs

Book Recommendations