BDD in C#: When Tests Speak the Language of Business
\n\nImagine: 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\nSound 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\nIn 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\nWhat is BDD and Why Do You Need It?
\n\nBDD (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\nThe key idea of BDD: "Don't check that the code works. Check that the system behaves as the business expects."
\n\nUnlike 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\nThe 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
Now let's see what this looks like in C# code.
\n\nTools for BDD in C#
\n\nTo 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
We will use SpecFlow + xUnit. Installation is simple — via NuGet:
\n\ndotnet add package SpecFlow\ndotnet add package SpecFlow.xUnit\ndotnet add package xunit\ndotnet add package xunit.runner.visualstudio\n\nAfter installation, create a Features folder in your project — this is where your .feature files with scenarios will live.
Writing Your First BDD Scenario in Gherkin
\n\nGherkin is a behavior description language. It uses keywords: Feature, Scenario, Given, When, Then, And, But.
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\nCreate a file Payment.feature in the Features folder:
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\nNote: here