Using GitHub Copilot to Draft Test Cases Safely, Agent Mode Too
GitHub Copilot can produce a complete-looking test in seconds, and that speed is exactly why it needs watching. I've seen engineers accept suggestions wholesale, push them to CI, and then spend the afternoon chasing failures caused by an assertion Copilot made up from an API that looked like theirs but wasn't. The draft looked right. It just wasn't.
That's no reason to avoid Copilot in test work. It's a reason to have a deliberate workflow. I treat every suggestion as a first draft from someone fast and confident who has never seen my system. That matters even more now that Copilot has an agent mode that can create files, run commands and write a whole test module in one go. The more it writes per step, the more your review process carries.
Below is the workflow I use: how to give Copilot the right context, what to write in the repository instruction file, the mistakes that keep breaking API suites, and the review checks I run before anything gets committed.
Learn Node.js, Cucumber, GitHub Copilot, APIs, CI/CD, and modern automation by building a complete framework.
Give Copilot context before it starts suggesting
Copilot's suggestions are only as good as what it can see. Open an empty file, type def test_, and it's guessing from general patterns. The result is usually valid Python and useless as a test: a generic happy-path check that has nothing to do with your contract.
I give it context in three ways before I accept anything:
- Keep the contract visible. Open the OpenAPI spec or a real captured response in a neighbouring tab, or add the file to the chat context. A real response shape beats any comment.
- Write the intent first. A docstring like "POST /orders without customer_id returns 422 with a validation error naming the field" is a far better prompt than the function name alone.
- Show it your client and fixtures. If Copilot can see your
api_clientfixture, it calls that instead of inventing raw requests with hardcoded URLs.
Without context, a typical suggestion is a stub:
def test_create_order():
response = requests.post("/orders", json={"item": "widget"})
assert response.status_code == 200
With a docstring and a visible fixture, the draft is much closer to useful:
def test_create_order_without_customer_id_returns_422(api_client):
"""POST /orders without customer_id returns 422 and names the missing field."""
payload = {"item_id": "abc123", "quantity": 2} # customer_id deliberately omitted
response = api_client.post("/orders", json=payload)
assert response.status_code == 422
assert "customer_id" in response.text
It's still a draft. The status code and the error shape have to be checked against your real API. But it's worth reviewing now. In a Behave project, the same idea applies: keep the .feature file open next to the steps and the suggestions follow your scenario wording. I go deeper on that in prompting an AI assistant to write Behave step definitions.
Put your conventions in the instruction file
Context in open tabs helps one session. For conventions that should apply every time, Copilot reads repository-wide custom instructions from .github/copilot-instructions.md. Chat and agent mode both use it, so it's the right place for test rules:
## API test conventions
- Use the api_client fixture from tests/conftest.py. No raw requests calls in tests.
- Expected status codes and fields come from openapi.yaml, not from REST convention
or from whatever the API currently returns.
- Every test that creates data uses a unique value (uuid4) and cleans up in a
fixture teardown.
- Never weaken an assertion, add skip/xfail, or edit an existing test to make a
failure pass. Report the mismatch instead.
- One behaviour per test function; name it after the behaviour.
Keep it short and specific. A rule like "write good tests" changes nothing. A rule like "expected status codes come from openapi.yaml" changes what gets generated. Commit the file and review changes to it the same way you review code, because it shapes every suggestion your team gets.
The three mistakes that break API suites
Invented field names. Copilot will assert on fields your API doesn't return, because similar APIs return them. If your endpoint returns order_id and the draft reads response.json()["id"], you get a KeyError, not a meaningful failure. Check every asserted field against a real response.
Status codes from convention, not from your contract. Copilot tends to use the textbook codes: 201 for create, 204 for delete. Real APIs vary. If your spec says 200 on create, an asserted 201 is a wrong test that looks completely reasonable, which is why it gets through review.
Data that leaks between tests. Drafts often hardcode emails, usernames and IDs and never clean up. Against a shared environment, the second run fails with a conflict the test wasn't built to handle. Generate unique values per run and delete what you create in teardown. Sharing test data across a Behave suite safely covers the isolation rules in more detail.
Reviewing drafts, especially from agent mode
A single inline suggestion is easy to read. An agent-mode session that writes twelve tests and reports "all passing" is a different review job. The fact that everything passes tells you very little: a test that asserts on the current response will always pass. Before I accept a draft from either mode, I check:
- Does every asserted field exist in a real response I've seen?
- Does every expected status code come from the spec, not convention?
- Is the test data unique per run, and is there teardown for anything created?
- Can the test fail? I change the expected value or break the behaviour on purpose and confirm it goes red.
The last check is the one people skip and the one that catches the most. Generated tests sometimes assert on nothing meaningful, or mock out the very thing they claim to test. Making a test fail on purpose shows that right away.
For agent sessions I also ask for a summary listing each test, the behaviour it covers and where the expected values came from. If the answer is "from the response," that test needs a second look. The broader review checklist is in reviewing AI-suggested test coverage before you commit it.
Keep the suite consistent over time
The bigger long-term risk isn't one bad test. It's drift: different naming, different request styles and different assertion patterns spreading across files because each suggestion copied whatever was nearby. Copilot repeats the patterns it sees, so give it good ones:
- Write the first one or two tests in a module by hand. They become the examples everything else copies.
- Route every call through a shared client. If the suite uses
api_client.get(), suggestions do too. - Keep the instruction file current when conventions change, or Copilot will keep producing the old style.
A well-structured suite gets better suggestions, and a messy one gets more mess. If you want to build that structure with Copilot from the first test, my API testing courses cover it in both Python with Behave and Node with Cucumber.