When to Use BDD vs. Pytest for API Tests in the AI Era
This is one of the most common questions I get from teams building a new API test suite: should we write Gherkin scenarios with Behave, or plain pytest functions and call it done? Both work. I've seen clean, well-maintained suites built on each, and I've seen teams pick one for the wrong reason and end up with a framework that fights them six months later.
The question has changed shape now that GitHub Copilot and coding agents write a large share of test code. Generating a pytest test is close to free. Generating step definitions for a feature file is close to free too. What isn't free is knowing that the tests check the right behaviour. So the real decision is no longer "which framework is easier to write in?" It's "where does the specification live, and who owns it: the people deciding what the API should do, or the code an agent produced?"
In this article I'll walk through the signals that push me toward one approach or the other, what changes when an AI assistant is writing the code, and the hybrid pattern I keep coming back to on real projects.
Learn Python, Behave, GitHub Copilot, APIs, and CI/CD by building a real framework you can finish in a weekend.
The real job of BDD in an API test suite
BDD was designed to close the gap between business intent and automated verification. When it works, a product owner can read a scenario and confirm it matches what they asked for. That feedback loop is genuinely valuable, but only if it actually exists on your team.
The signal I look for: will non-engineers read, write or review these scenarios? If the answer is yes, even occasionally, BDD earns its overhead. A feature file for a payment API might look like this:
Feature: Payment authorization
Scenario: Successful card authorization
Given a valid card with sufficient funds
When the payment API receives an authorization request for $50.00
Then the response status should be 200
And the authorization code should be present in the response body
A product manager, a business analyst or a compliance reviewer can read that and know what's being tested. The step definitions underneath are just Python calling the API and asserting on JSON, but the intent is readable without the code.
Where BDD struggles with API testing is at the edges: complex data setup, heavily parameterised edge cases and deep contract checks. You can express "run this with 47 input combinations" as a scenario outline, but the examples table gets unwieldy fast and stops being readable to the people it was for. If your step definitions are getting longer than the equivalent pytest test would be, BDD is probably the wrong layer for that test. I go deeper on the framework side in how BDD and pytest fit into a mature automation strategy.
Where plain pytest wins
Pytest is my default when the audience for the tests is the engineering team. You get the full power of Python with almost no ceremony: parametrize over payloads, compose fixtures for auth and base URLs, parallelise with pytest-xdist. A typical API test is direct:
import requests
def test_get_user_returns_expected_fields(api_base_url, auth_headers):
response = requests.get(f"{api_base_url}/users/42", headers=auth_headers, timeout=10)
assert response.status_code == 200
data = response.json()
assert data["id"] == 42
assert "email" in data
No feature file, no step definitions. The test is the specification. Edge-case coverage is where pytest really pulls ahead:
import pytest
import requests
@pytest.mark.parametrize("payload, expected_status", [
({"amount": -1}, 400),
({"amount": "abc"}, 422),
({"amount": None}, 422),
({}, 400),
({"amount": 0}, 400),
])
def test_payment_rejects_invalid_amounts(api_base_url, auth_headers, payload, expected_status):
response = requests.post(f"{api_base_url}/payments", json=payload,
headers=auth_headers, timeout=10)
assert response.status_code == expected_status
The Gherkin equivalent is a sprawling examples table nobody reads for business clarity. Pytest also fails more directly: you see the assertion, the actual value and the diff, without mapping a failed step back to the code underneath. If you haven't pushed the fixture system hard yet, advanced fixture patterns for API testing are worth learning before you decide you need BDD to manage complexity.
What changes when Copilot or an agent writes the tests
This is the part most comparisons miss. With an AI assistant in the loop, writing test code stops being the bottleneck and reviewing it becomes the bottleneck. An agent will happily produce forty pytest functions in a minute. Some will assert on the wrong thing, some will assert on whatever the API currently returns, and reading all of them carefully takes longer than writing them used to.
That shifts the value of a feature file. A short, human-written scenario is a specification the agent has to satisfy, and it's much quicker to review than forty generated functions. The workflow that works well for me:
- People own the scenarios. A human writes or approves the Gherkin for the behaviour that matters to the business. That's the part worth reviewing line by line.
- The agent implements the steps. Copilot or a coding agent writes the step definitions and helpers. That's mechanical work it does well, and the scenario tells it exactly what "done" means.
- The agent writes the pytest breadth. Parametrised edge cases, contract checks and negative tests in plain pytest, where volume helps and the reviewer checks the data table rather than prose.
Whatever you choose, tell the assistant. Copilot reads repository-wide guidance from .github/copilot-instructions.md, and most coding agents have an equivalent file. A few lines there save a lot of review:
## API test conventions
- Acceptance behaviour lives in features/*.feature. Never edit a scenario to make it pass;
flag the mismatch instead.
- Step definitions call the client in tests/api_client.py; no raw requests in steps.
- Edge cases and contract checks go in tests/api/ as parametrized pytest tests.
- Assert on documented behaviour (status, schema, business fields), not on whatever the
current response happens to contain.
The last rule matters most. The most common failure I see with AI-generated API tests is a test that snapshots the current response and calls it a specification. It passes today and protects nothing.
Making the call
Here's the checklist I run through when starting a suite or reviewing one:
- Non-engineers will read or approve the tests → BDD for that layer.
- Tests map to user stories or acceptance criteria → BDD gives you traceability, and a clean contract for an agent to implement.
- The team is all engineers and the tests are for regression confidence → plain pytest.
- Heavy parametrisation or data-driven coverage → pytest. This is also where agent-generated tests add the most.
- Internal service contracts → pytest. No stakeholder needs a scenario for a 401 on a missing token.
- Hundreds of tests where CI speed matters → pytest's parallelisation and plugins give you more control.
The mistake I see most often is adopting BDD because it sounds like best practice, then maintaining feature files nobody outside engineering ever opens. That's two layers of abstraction for no extra clarity, and an agent will cheerfully generate more of them. The opposite mistake is dismissing BDD entirely, then having no readable statement of what the API is supposed to do. With AI writing the code, that gap gets wider: the tests pass, and nobody can confirm they test the right things without reading every generated function.
If you're still finding your footing with the basics, start with API testing fundamentals. It makes this decision much clearer.
The short version: use BDD where a human-readable specification creates real value, and treat it as the contract your AI assistant implements against. Use pytest everywhere else, and let the assistant help with the breadth. If you'd like to build that setup end to end, with Behave or Cucumber, VS Code and GitHub Copilot, that's exactly what my API testing courses walk through.