Prompting an AI Agent to Write Behave Step Definitions
GitHub Copilot and coding agents can produce Behave step definitions in seconds. Fast and useful are not the same thing, though. I've seen teams paste a scenario into a chat window, accept the first answer, and end up with steps that run but are too specific to reuse, store state in the wrong place, or quietly collide with steps that already exist.
What I've learned is that a good prompt for step definitions is really a small design brief. It tells the assistant which framework you use, how the project is laid out, where shared state lives and what "reusable" means on your team. Once you write that brief down, you stop retyping it, and you can hand the same brief to an agent that writes and runs the steps on its own.
In this article I'll cover what goes in the brief, how to stop the hard-coded step trap, how to move the brief into a repository instructions file, and the checks I run before any generated step gets merged. The feature file stays the specification; the assistant writes the glue.
Learn Python, Behave, GitHub Copilot, APIs, and CI/CD by building a real framework you can finish in a weekend.
Give the assistant context before the Gherkin
An assistant generating steps in a vacuum makes assumptions: how you use context, whether there's a shared HTTP client, where step files live. Those guesses are often wrong for your project. The minimum context I include:
Framework: Python Behave, default "parse" step matcher
Step files: features/steps/, one module per feature area
Shared state: on the context object only (context.response, context.token)
HTTP client: an ApiClient created in environment.py before_all as context.api
Naming: snake_case step functions
Reuse: step text must be parameterised so one step covers many scenarios
Then I add one real step from the codebase. A single example teaches conventions faster than any amount of description: the decorator, the parse format string, the assertion style and the naming all at once.
# Existing step for reference. Match this style.
from behave import then
@then("the response status code is {status_code:d}")
def step_check_status(context, status_code):
assert context.response.status_code == status_code, context.response.text
Only after that do I paste the Gherkin. The scenario is the contract; the context tells the assistant how to fulfil it in this project rather than in a generic one.
Prompt against the hard-coded trap
The most common failure in generated Behave steps is over-specificity. Given When I create a user named "alice", an assistant left to itself often writes a step whose text is that exact sentence, with "alice" baked into the function. It works once and is useless for the next scenario. I add an explicit rule:
Parameterisation rule: any value that could differ between scenarios
(names, endpoints, status codes, field names, amounts) MUST be a parse
parameter such as {name} or {count:d}. Never hard-code scenario values.
With that in place, the output looks more like this:
from behave import when
@when('I create a user named "{username}"')
def step_create_user(context, username):
context.response = context.api.post("/users", json={"username": username})
Two more rules belong in the brief. All data shared between steps lives on context, never in module-level variables, so a Then step can see what a When step produced and nothing leaks between scenarios. And say whether setup belongs in environment.py hooks or in Given steps; the generated code is structured very differently depending on the answer. I cover the reuse side in more depth in writing step definitions that stay reusable.
Move the brief into the repository
Retyping context into a chat window works for one-off prompts. Once an agent is writing steps, the brief belongs in a file the agent reads every time. Copilot reads repository-wide guidance from .github/copilot-instructions.md, and most coding agents have an equivalent instructions file. Mine for a Behave project looks like this:
## Behave conventions
- Feature files in features/ are the specification. Never edit a scenario
to make it pass. If a scenario looks wrong, stop and say so.
- Step modules live in features/steps/. Search existing steps before
writing a new one; reuse or generalise instead of duplicating.
- Use parse parameters for every value that could vary.
- Shared state goes on context. No module-level globals.
- HTTP calls go through context.api (features/support/api_client.py).
- Then steps assert documented behaviour: status, schema, business fields.
- After writing steps, run: behave --dry-run, then behave on the feature.
The first rule is the one that matters most. An agent whose goal is "make the scenario pass" will, sooner or later, take the shortest route and change the scenario. Writing down that the feature file is off-limits turns that from a silent edit into a question you get to answer.
Let Behave check the agent's work
Behave gives you cheap, mechanical checks that are perfect for an agent's self-verification loop, and for your review afterwards:
- Undefined steps.
behave --dry-runparses the features and matches steps without executing them, and reports any step text with no definition. Zero undefined steps is the first gate. - Ambiguous steps. Behave raises an error when two step patterns can match the same text, typically when a generated step overlaps an existing one. Treat it as a design problem: merge the steps rather than rewording one to dodge the match.
- Unused steps. The
steps.usageformatter (behave --dry-run -f steps.usage) lists which steps are used where and which are never used. Generated code tends to leave orphans behind.
Then run the scenarios for real. A step file that passes a dry run but has a Then step that asserts nothing will still pass a real run, which is why the human review below is not optional.
Review generated steps before merging
A good brief means review is quick, not that review is skipped. My checklist for every batch:
- State: everything shared is on
context; no globals; nothing that needs to survive a scenario boundary. - Parameters: no literal values that belong to one scenario.
- Assertions: each
Thenchecks something specific. "No exception was raised" is not an assertion. - Snapshotting: expected values come from the scenario or the documented contract, not copied from whatever the API returned while the agent was running it.
- Scope: the diff touches step files and helpers only. If a
.featurefile changed, find out why before anything else.
When something fails a check, a targeted follow-up fixes it faster than manual edits: "step_check_body stores parsed JSON in a local variable. Store it on context.response_json and update the steps that read it." One more habit that helps: ask the assistant to list each step text, its parameters and what it reads or writes on context before it writes any code. The outline makes design problems visible while they're cheap to fix. For a broader look at what goes wrong in generated tests, see where AI-generated tests get API testing wrong.
Where this leaves you
The split that works for me is simple. People write and approve the scenarios. The agent writes the step definitions against them, using a brief that lives in the repository, and verifies with Behave's own checks. A human reviews the diff with a short checklist. Generated steps become a starting point that's close to mergeable, and your time goes into the specification instead of boilerplate. If you'd like to build this workflow end to end with Behave, VS Code and GitHub Copilot, it's what my API testing courses are built around.