BDD in C#: 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

BDD in C#: When Tests Speak the Language of Business

\n\n

Imagine: you are a C# developer. A product owner comes to you and says: "Make it so that after an order payment, the user receives an email, but only if the amount is more than 1000 rubles." You nod, go to code, write the logic, cover it with unit tests. A week later, you show the result. The PO looks, frowns, and says: "I meant something else."

\n\n

Sound familiar? This is the classic problem of the gap between business and development. And this is exactly what BDD in C# — Behaviour-Driven Development — solves.

\n\n

In this article, we will break down what BDD is, how it differs from TDD, and most importantly — how to implement BDD in a C# project using SpecFlow and xUnit. You will see real code examples, learn to write Gherkin scenarios, and turn them into automated tests. Let's go!

\n\n

What is BDD and Why Do You Need It?

\n\n

BDD (Behaviour-Driven Development) is a development methodology where system requirements are described through user behavior. Instead of dry technical specifications, you write scenarios in natural language that both business and developers understand.

\n\n

The key idea of BDD: "Don't check that the code works. Check that the system behaves as the business expects."

\n\n

Unlike TDD (Test-Driven Development), where you first write a unit test and then the code, BDD focuses on usage scenarios. BDD tests are not a check of a method or class, but a check of an entire user scenario.

\n\n

The Three Pillars of BDD

\n
    \n
  • Unified language — all project participants (PO, analyst, tester, developer) speak the same language. Usually this is Gherkin.
  • \n
  • Scenarios — each functional element is described using Given-When-Then.
  • \n
  • Automation — scenarios are turned into automated tests that run as part of CI/CD.
  • \n
\n\n

Now let's see what this looks like in C# code.

\n\n

Tools for BDD in C#

\n\n

To implement BDD in the .NET ecosystem, there are two main tools:

\n\n
    \n
  • SpecFlow — the most popular BDD framework for C#. It parses Gherkin scenarios and binds them to code.
  • \n
  • xUnit / NUnit / MSTest — test runners that execute BDD tests.
  • \n
\n\n

We will use SpecFlow + xUnit. Installation is simple — via NuGet:

\n\n
dotnet add package SpecFlow\ndotnet add package SpecFlow.xUnit\ndotnet add package xunit\ndotnet add package xunit.runner.visualstudio
\n\n

After installation, create a Features folder in your project — this is where your .feature files with scenarios will live.

\n\n

Writing Your First BDD Scenario in Gherkin

\n\n

Gherkin is a behavior description language. It uses keywords: Feature, Scenario, Given, When, Then, And, But.

\n\n

Let's describe a simple scenario: a user tries to pay for an order, and if the amount is more than 1000 rubles, they receive an email.

\n\n

Create a file Payment.feature in the Features folder:

\n\n
Feature: Payment Notification\n    As a customer\n    I want to receive an email after payment\n    So that I have a confirmation of my purchase\n\nScenario: Successful payment over 1000 rubles sends notification\n    Given a customer with balance 5000 rubles\n    When the customer pays an order of 1500 rubles\n    Then the customer should receive an email notification\n\nScenario: Payment under 1000 rubles does not send notification\n    Given a customer with balance 5000 rubles\n    When the customer pays an order of 500 rubles\n    Then the customer should not receive any email notification
\n\n

Note: here

Blogs

Book Recommendations