Structuring a Behave Project So It Scales Past 50 Scenarios
A Behave project with twenty scenarios can survive almost any structure. One feature file, one steps file, an environment.py at the root, and everything works. Somewhere past fifty scenarios that changes. Step files grow to hundreds of lines, nobody can find the step they need so they write a near-duplicate, ambiguous step errors start appearing, and the only way to run "just the order tests" is to remember a file path.
Behave is flexible enough to support a layout that scales, but it won't choose one for you. The decisions that matter are made early: how features are grouped, how step definitions are organised, where helper code lives and how tags are used. They're cheap to make at twenty scenarios and expensive to retrofit at two hundred.
Structure matters even more once GitHub Copilot or a coding agent works in the project. An agent finds existing steps, helpers and conventions by reading the repository. A predictable layout means it reuses what's there; a sprawling one means it adds more sprawl. In this article I'll walk through the layout I use, the step hygiene that keeps it healthy, a tag scheme for CI, and how to make the structure legible to an agent.
Learn Python, Behave, GitHub Copilot, APIs, and CI/CD by building a real framework you can finish in a weekend.
A directory layout that holds up
Group feature files by business domain in subdirectories. Behave finds feature files in subdirectories of features/, so this needs no configuration:
features/
environment.py
auth/
login.feature
token_refresh.feature
orders/
create_order.feature
cancel_order.feature
search/
product_search.feature
steps/
common_steps.py
auth_steps.py
order_steps.py
search_steps.py
test_support/
api_client.py
factories.py
payloads.py
A few points about this layout. Behave loads every Python file in features/steps/ into one shared step registry, so a step defined in auth_steps.py can be used by any feature. You don't import step modules into each other, and you shouldn't. Splitting step files by domain is about humans finding things, not about visibility.
Helper code lives outside the steps folder, in an ordinary Python package: the API client, payload builders and data factories. Make it importable in the usual way for your project, for example by installing the project in editable mode or setting the path in your test runner configuration. Steps then stay thin: parse the step text, call a helper, store the result on context.
Keep environment.py thin too. It should create the client, initialise scenario state and handle cleanup by calling helpers, not contain them. When it grows past a screen or two, it's usually collecting logic that belongs in test_support/.
Step hygiene when you have dozens of steps
Step definitions are where Behave projects rot. The cause is almost always steps that are too specific, with data embedded in the step text:
from behave import when
# Brittle: one step per value
@when("I submit an order for 3 items")
def step_submit_order_three(context):
context.response = context.api.post("/orders", json={"quantity": 3})
# Scalable: one step for every value
@when("I submit an order for {quantity:d} items")
def step_submit_order(context, quantity):
context.response = context.api.post("/orders", json={"quantity": quantity})
With the default parse matcher, {quantity:d} converts the value to an integer for you. Parameterise from the start and the number of steps grows much more slowly than the number of scenarios.
Two more habits keep a large step library manageable. Group related scenario state rather than scattering dozens of attributes on context: a context.order dict initialised in before_scenario makes it obvious which domain a step touches. And run behave --dry-run -f steps.usage now and then. It lists each step definition with where it's used, plus any that are unused, which is the quickest way to find dead and near-duplicate steps. I cover reuse in more depth in writing step definitions that stay reusable.
Tags as a scaling tool
At a hundred scenarios against a real or sandboxed API, a full run takes long enough that you need to choose what runs when. Tags make that explicit. I use three layers:
- Domain tags such as
@auth,@orders,@search, applied at feature level. - Criticality tags:
@smokefor the small set that runs on every pull request,@regressionfor the broader nightly set. - State tags:
@wipfor scenarios being written, excluded in CI.
@orders
Feature: Order creation
@smoke
Scenario: Create a valid order with a single item
Given I am authenticated as a standard user
When I submit an order for 1 items
Then the response status code is 201
And the response contains an order id
@regression
Scenario: Reject an order above the maximum quantity
Given I am authenticated as a standard user
When I submit an order for 1000 items
Then the response status code is 422
A smoke run is then behave --tags=smoke. Excluding @wip uses different syntax depending on your Behave version: older releases use --tags=~wip, newer ones support tag expressions like --tags="not wip". Check which your version accepts and put the exact commands in your CI configuration so nobody has to remember them.
The discipline problem is smoke creep. Everything feels critical, so everything gets @smoke, and the fast check stops being fast. Keep smoke to the handful of scenarios that prove the system is fundamentally working. Document the approved tags in the repository so the team, and any agent, uses the same ones. For how tags map onto pipeline stages, see tagging API tests so CI can run the right subset.
Make the structure legible to an agent
When I ask an agent to "add scenarios for order cancellation", it has to answer the same questions a new team member would: where do feature files go, which steps already exist, where does helper code live, which tags apply. If the answers are only in people's heads, the agent guesses, and its guesses become your new conventions.
I write the structure down in the repository instructions file the agent reads, .github/copilot-instructions.md for Copilot or the equivalent for other coding agents:
## Behave project layout
- Features: features/<domain>/*.feature. Domains: auth, orders, search.
- Steps: features/steps/<domain>_steps.py; generic steps in common_steps.py.
- Before writing a step, run: behave --dry-run -f steps.usage
and reuse or generalise an existing step instead of adding a near-duplicate.
- Helpers and payload builders go in test_support/, not in step files.
- Tags: one domain tag per feature; @smoke only with reviewer approval;
new scenarios start as @wip.
- Never edit an existing scenario to make it pass. Report the mismatch.
Two of those rules do most of the work. Checking the step catalogue before writing new steps stops the duplicate-step sprawl that agents produce quickly. And keeping scenarios off-limits means the feature files remain the specification people agreed on, while the agent does the implementation underneath. In review, I check that new steps landed in the right file, that no scenario text changed unexpectedly, and that the dry run reports no undefined steps.
Decide early, write it down
None of this is complicated. Domain folders, thin parameterised steps, helpers in a real package and a small tag vocabulary will carry a Behave project well past fifty scenarios. Writing those decisions into the repository makes them hold for everyone who works in it, including the agents. If you want to set up a Behave project this way from the start, with VS Code and GitHub Copilot, my API testing courses walk through it.