Python
BDD
Behavior-Driven Development
Software Testing
Python Programming

Practicing BDD with python

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Practicing BDD in Python is less about choosing a library and more about writing executable examples of behavior that matter to users. A good BDD workflow keeps scenarios readable by non-developers while still producing tests that engineers trust.

In Python, common tools include behave and pytest-bdd. The ideas are the same in both: write feature files in Gherkin, implement step definitions in Python, and keep scenarios focused on business behavior rather than internal code structure.

Start with a Small Feature

A useful BDD scenario describes one concrete behavior and its expected outcome. For example, an e-commerce discount rule can be written in Gherkin like this:

gherkin
1Feature: Checkout totals
2  Scenario: Applying a percentage discount
3    Given a cart subtotal of 100
4    When I apply a 10 percent discount
5    Then the final total should be 90

This format is readable by developers, testers, and product people. It also forces you to talk about system behavior in domain language instead of naming implementation methods.

Implement the Scenario with pytest-bdd

One practical Python setup is pytest-bdd because it integrates naturally with pytest.

python
1from pytest_bdd import scenarios, given, when, then, parsers
2
3scenarios("checkout.feature")
4
5@given(parsers.parse("a cart subtotal of {amount:d}"), target_fixture="state")
6def subtotal(amount):
7    return {"subtotal": amount, "total": amount}
8
9@when(parsers.parse("I apply a {percent:d} percent discount"))
10def apply_discount(state, percent):
11    state["total"] = state["subtotal"] * (100 - percent) / 100
12
13@then(parsers.parse("the final total should be {expected:d}"))
14def check_total(state, expected):
15    assert state["total"] == expected

The point is not the framework syntax. The point is that the scenario and the implementation are close enough to keep the behavior clear.

Keep Steps Focused on Domain Language

Step definitions should describe concepts that matter to the business, not details of your codebase. A step like Given a cart subtotal of 100 is better than Given the CartService.calculate_total method is called.

That distinction is the core of good BDD practice. If the scenario reads like an implementation walkthrough, you lose most of the communication value that BDD is supposed to create.

Avoid Giant End-to-End Setups

A BDD suite should not be a pile of fragile browser tests unless the scenario truly needs full-stack behavior. Most business rules can be tested at the service or API level with lightweight state setup.

For example, you can keep the domain logic in a plain Python function and let the BDD steps call it:

python
1def apply_percentage_discount(subtotal, percent):
2    return subtotal * (100 - percent) / 100
3
4assert apply_percentage_discount(100, 10) == 90

This keeps failures easy to diagnose. The scenario expresses the rule, while the code underneath stays small and deterministic.

Reuse Steps Carefully

It is tempting to create extremely generic step definitions so every phrase can be reused. That usually backfires. Overly generic steps become hard to understand and easy to misuse.

Instead, aim for reusable domain phrases. Reuse is good when it reflects real shared language, not when it hides different behaviors behind the same vague wording.

BDD Is a Collaboration Practice

The most effective BDD teams do not treat feature files as developer-only test artifacts. They use examples to clarify requirements before implementation, then keep those examples in sync with the product's real behavior.

In that sense, BDD is partly a testing technique and partly a conversation discipline. Python gives you the tooling, but the quality of the scenarios still depends on how well the team defines behavior together.

Common Pitfalls

  • Writing scenarios that mirror code structure instead of user-visible behavior.
  • Packing too many assertions and branches into one scenario.
  • Building brittle end-to-end setups for rules that could be tested much more simply.
  • Creating vague, hyper-generic steps that reduce readability.
  • Letting feature files drift away from the actual product behavior over time.

Summary

  • BDD in Python works best when scenarios describe business behavior in plain language.
  • Tools like pytest-bdd and behave are useful, but the scenario design matters more than the library choice.
  • Keep step definitions close to domain concepts instead of implementation details.
  • Prefer lightweight, deterministic test setups for most behavior rules.
  • Treat BDD as a collaboration practice, not just a syntax for tests.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.