Turning VS Code Snippets Into a Test Toolkit Alongside Copilot
Typing out a full request, a status check and an assertion block from scratch for every new test wastes time I'd rather spend on the test logic. VS Code snippets remove that friction. But most testers I talk to either don't use them, or have one or two they copied from a tutorial and never touched again.
You might reasonably ask whether snippets still matter when Copilot suggests whole tests as you type. I think they matter more, for one reason: a snippet is deterministic. It produces your team's agreed pattern exactly, every time, while an AI suggestion produces a plausible pattern that may or may not match the rest of your suite. I use both. Snippets lay down the structure I've decided on, and the assistant fills in the details inside it. The conventions a snippet encodes are also the conventions your coding agents should follow, so writing them down once helps twice.
In this article I'll cover what belongs in a snippet, a set of pytest and Behave snippets with the syntax gotchas fixed, a few snippets for testing AI features, and how to share and maintain the whole library.
Learn Python, Behave, GitHub Copilot, APIs, and CI/CD by building a real framework you can finish in a weekend.
What makes a good test snippet
The common mistake is making snippets for the wrong things. A snippet earns its place when it captures something structurally repetitive: always the same shape, with a few parts that change. For API testing, the high-value targets are:
- Test scaffolding: the function, a docstring naming the behaviour, and a request plus assertion already in place.
- Fixtures: the decorator, the scope choice and the yield-based teardown.
- Behave steps: decorator, signature and context access.
- Assertion clusters: when you always check status, content type and a key field together.
What doesn't belong is anything that varies so much you spend longer editing the output than typing it fresh. A snippet with fifteen tab stops is usually a helper function in disguise. Write the function instead.
Put shared snippets in the workspace
Personal snippets live in your user settings (Snippets: Configure Snippets in the Command Palette, then choose the language). Team snippets belong in the repo, in a file under .vscode/ whose name ends in .code-snippets, such as .vscode/api-tests.code-snippets. A file named python.json in .vscode/ isn't read as snippets. In a .code-snippets file, add a "scope" field to each snippet so it only appears in Python files.
{
"API test": {
"scope": "python",
"prefix": "aptest",
"body": [
"def test_${1:behaviour}(${2:api}):",
" \"\"\"${3:What this test protects against.}\"\"\"",
" response = ${2:api}.${4|get,post,put,patch,delete|}(\"${5:/endpoint}\"${6:, json=payload})",
" assert response.status_code == ${7:200}",
" $0"
],
"description": "Scaffold an API test"
},
"pytest fixture": {
"scope": "python",
"prefix": "pfix",
"body": [
"@pytest.fixture(scope=\"${1|function,class,module,session|}\")",
"def ${2:name}(${3}):",
" ${4:value = create()}",
" yield ${5:value}",
" ${6:cleanup(value)}"
],
"description": "Fixture with scope and teardown"
}
}
A few syntax details. ${4|get,post,...|} is a choice, so tabbing to it gives you a dropdown of methods. Using ${2:api} twice mirrors the value: edit it once and both places update. $0 is where the cursor ends up. Be careful with braces inside a placeholder's default text: ${4:payload = {}} ends the placeholder at the first }. Escape it as \\} in the JSON, or keep braces out of defaults. The fixture snippet's yield-and-cleanup shape is there on purpose, so teardown isn't forgotten. For more on fixture design, see pytest fixtures for API testing beyond the basics.
Behave step snippets
Step definitions are structurally identical every time, which makes them ideal snippets. I encode the conventions I want to keep, like storing the response on context.response and using typed parameters from Behave's default parse matcher:
"Behave then status": {
"scope": "python",
"prefix": "bthen",
"body": [
"@then(\"the response status should be {status:d}\")",
"def step_then_status(context, status):",
" assert context.response.status_code == status, context.response.text",
"$0"
],
"description": "Then step asserting status"
},
"Behave when request": {
"scope": "python",
"prefix": "bwhen",
"body": [
"@when(\"${1:I request the order list}\")",
"def step_${2:request_orders}(context):",
" context.response = context.api.${3|get,post,put,patch,delete|}(\"${4:/orders}\")",
"$0"
],
"description": "When step that stores the response"
}
Including context.response.text as the assertion message costs nothing and makes every failure show the body. If your suite uses different conventions, change the snippets to match. A snippet that encodes a pattern you don't use is just noise. Writing step definitions that stay reusable covers the conventions worth encoding.
Snippets for testing AI features
If your suite covers an LLM feature or an agent's tools, the patterns are newer and easier to get wrong under time pressure, which makes them good snippet candidates. Two I use:
"Pass-rate test": {
"scope": "python",
"prefix": "aprate",
"body": [
"@pytest.mark.llm",
"def test_${1:behaviour}(generate):",
" runs = ${2:10}",
" passes = sum(${3:check}(${4:feature}(${5:sample}, generate)) for _ in range(runs))",
" assert passes / runs >= ${6:0.9}, f\"{passes}/{runs} runs passed\"",
"$0"
],
"description": "Model-dependent test judged by pass rate"
},
"Tool contract test": {
"scope": "python",
"prefix": "aptool",
"body": [
"def test_${1:tool}_rejects_${2:bad_input}():",
" result = ${1:tool}(${3:**bad_args})",
" assert result[\"error\"] == \"${4:invalid_arguments}\"",
"$0"
],
"description": "Agent tool returns a structured error"
}
The pass-rate snippet bakes in the decisions that matter: the model is injected through a generate fixture, the test is marked so it stays out of the default run, and the result is judged across several runs with a message showing the actual count. The tool snippet captures the rule that a tool given bad arguments should return a structured error the agent can act on, not raise an exception or return an empty result.
Keep snippets and agent instructions in sync
Here's where snippets and coding agents connect. Every convention a snippet encodes (responses on context.response, a docstring naming the failure mode, a marker on model-dependent tests) is a convention an agent should follow when it writes tests. If they're only in snippets, tests you write follow them and tests Copilot writes don't. I mirror the important ones in .github/copilot-instructions.md and change both in the same pull request. When someone proposes a new convention, the review question is "does this need a snippet, an instruction line, or both?" For the prompting side of step definitions, see prompting an AI assistant to write Behave step definitions.
Keeping the library healthy
- Version-control it. Workspace snippets get reviewed like code. Personal ones can live in a dotfiles repo.
- Use consistent prefixes.
apfor API tests,bfor Behave,pffor fixtures, so typing the prefix shows only test snippets. - Delete outdated snippets. A snippet that generates an old pattern is worse than none. It spreads inconsistency at the speed of a keystroke.
- Watch for the third copy-paste. When you paste the same block for the third time, make it a snippet, or a helper if it varies too much.
The snippets that make a difference are the ones built from your own suite's patterns, and kept aligned with what your agents are told to do. If you'd like to build a full Behave or pytest project in VS Code with GitHub Copilot alongside, my API testing courses walk through it.