How I write my BDD files

In my last post, I said BDD can make code review 6x faster. A lot of people asked me how I write my feature files, so here it is.
I don't write "product language" BDD
I don't write my BDD files the way the literature teaches. This is the classic example from the Behat quick start:
Scenario: Buying a single product under £10
Given there is a "Sith Lord Lightsaber", which costs £5
When I add the "Sith Lord Lightsaber" to the basket
Then I should have 1 product in the basket
And the overall basket price should be £9
To me, the problem is reuse. Each of these steps exists only for this feature, so every new feature needs a new set of steps and a lot of code behind them. I prefer the opposite: as many reusable steps as possible. An HTTP client is a good example, because the same few steps can test any endpoint.
Tests as clients of my service
So my tests are clients of my service. If the service is REST, I create a REST client. If it is GraphQL, a GraphQL client. If it has a worker, a message producer that triggers the job. Everything is at the integration level, the test talks to the service the same way a real consumer would.
A box around the service
To avoid flaky tests, I like to put a box around my project, so it doesn't talk to any external service during the test. So I mock all the infrastructure I can: the database, Kafka, external APIs. But I do it with Testcontainers, so my mocks are real infrastructure. I also refresh the containers per feature, or even per scenario, so no test inherits state from another. Nothing is mocked in code. When the test saves something, it is really persisted in the database. When it publishes a message, it is really published.
Mocking HTTP calls
The only thing I really mock is HTTP calls to external services. I've used a few mock servers for this, like WireMock, Mockoon and MSW. Even then, the test defines the exact request it will send and the response it expects. The mock server understands this request and response, and when they match in the test, it returns exactly the behavior configured.
In Go, I don't use an external mock server. I use httptest, which records every call. I define the request and the response in the test, and the server understands it and answers correctly.
What a feature file looks like
Here is a simple example of a pet store API:
Feature: Pets
POST /pets adds a pet to the store.
A microchip already registered to another pet answers 409.
Scenario: Adding a pet
Given I am signed in as a new account
When I send "POST" to "/pets" with:
"""
{
"name": "Rex",
"species": "dog",
"microchip": "985141000123456"
}
"""
Then the response is 201 with:
"""
{
"pet": {
"name": "Rex",
"species": "dog",
"microchip": "985141000123456",
"status": "available",
"createdAt": "@datetime@"
}
}
"""
When I run the query:
"""
select name, species, microchip, status from pets
"""
Then the query returns:
"""
{
"rows": [
{
"name": "Rex",
"species": "dog",
"microchip": "985141000123456",
"status": "available"
}
]
}
"""
Scenario: The same microchip again answers 409 and changes nothing
Given I am signed in as a new account
And I send "POST" to "/pets" with:
"""
{ "name": "Rex", "species": "dog", "microchip": "985141000123456" }
"""
When I send "POST" to "/pets" with:
"""
{ "name": "Max", "species": "dog", "microchip": "985141000123456" }
"""
Then the response is 409 with:
"""
{ "message": "a pet with this microchip already exists" }
"""
When I run the query:
"""
select count(*) as pets from pets
"""
Then the query returns:
"""
{ "rows": [ { "pets": 1 } ] }
"""
As you can see, the steps are generic: send a request, check the response, run a query. And every scenario checks what the client receives and what was really saved in the database.
Why this makes review faster
And this is the link with my last post. When I review a change, I don't start with the code the AI generated. I start with the feature file. It shows me, in requests and responses, exactly what the service does, and since it runs against real infrastructure, I can trust it.
But a good BDD suite doesn't mean a good codebase. BDD tells me the behavior is right, not that the code is well written. So I have other safeguards: fitness functions to validate the architecture, a lot of linters, and a hexagonal architecture with well-defined layers. The codebase is full of examples, so when the AI writes a new feature, 80 to 90% of it already follows them. Sometimes it does something a bit different, but nothing far from the architecture.
That's why I don't feel the need to review every line. The architecture guides the code, and BDD guarantees the behavior. So I review the behavior.