To use AI for writing unit tests, give the coding assistant a rule and an example whose answer you already know. For free shipping on orders of $50 or more, the most revealing test is an order of exactly $50.
The Short Answer
| Order subtotal | Expected shipping fee | What the case checks |
|---|---|---|
| $49.99 | $5 | The fee still applies one cent below the threshold |
| $50.00 | $0 | The offer includes the threshold itself |
| $50.01 | $0 | Orders above the threshold qualify too |
What should you give the coding assistant?
Suppose a shop advertises free shipping on orders of $50 or more. Every smaller order costs $5 to ship. The checkout code already has a function called shipping_fee that accepts a subtotal in cents and returns the fee in cents. There's no shopping cart to construct for this exercise. One integer goes in and one comes back.
An assistant looking only at the function can write a test that describes what the shop charges today. That's a problem if the customer sees “$50 or more” on the offer and a developer has written “over $50” in the code. Both versions agree about a $60 order. They even agree at $49.99. The one customer who can tell them apart has exactly $50 in the cart.

Here's a prompt for Copilot Chat once you've opened the module and a nearby test file:
Add a pytest test for shipping_fee in checkout.py. The offer says orders of $50 or more ship free. Otherwise shipping costs $5. Inputs and outputs are integer cents. Check 4999, 5000 and 5001 cents. Show me the proposed tests and leave checkout.py alone.
Why specify cents? A test that passes 50 into this function represents a fifty-cent order. It may run perfectly and still answer a question nobody meant to ask. Giving the assistant a nearby test also shows it where the project imports its checkout code, which saves you from debugging an example written for a different directory layout.
What should a generated pytest test look like?
For a module you can import as checkout, ask for one test function with two parameters, subtotal and expected_fee. Inside that function the entire check is assert shipping_fee(subtotal) == expected_fee.
The pytest decorator above the function supplies the examples: @pytest.mark.parametrize("subtotal, expected_fee", [(4999, 500), (5000, 0), (5001, 0)]). Each pair becomes a separate test result, so a failure at exactly $50 has a name of its own in the report. You can see which case broke even when you run the whole file at once.
Read the middle row first. You can explain the zero to the person who wrote the offer without showing them a line of production code. That's a good sign. If the assistant instead calculates the expected fee by importing the same function twice under different names, both sides of the assertion can agree on any answer, including a $5 charge at the threshold.

A table works here because each row asks the same question. If you're working in a Python project that uses the standard library's unittest framework, you can express the same expectation with self.assertEqual(shipping_fee(5000), 0). Keep the convention that will look familiar to the next person who opens the file.
How can you check that the test would catch a mistake?
Save the test as test_checkout.py beside the example module and run python -m pytest -q test_checkout.py in the project's Python environment. If the middle row already fails, read the actual fee before asking the assistant to change anything. A result of 500 means a customer who qualifies for the offer would pay five dollars. An import error means the test hasn't reached checkout yet.
When all rows pass, try a small experiment in a disposable working copy. In the condition that grants free shipping, change subtotal >= 5000 to subtotal > 5000. Run the same command again. Exactly one row should fail. Restore the original comparison and run the tests once more.

In a Code project in Unstoppable Code, each task gets its own Git worktree, a separate working copy on its own branch. That gives this experiment a place away from your other edits. Before committing, read the final diff and confirm the deliberate change is gone. If you want a second pair of eyes on the patch, here's how to review a branch with Claude and Codex. For work across several branches, see Git worktree tools for AI coding.
Frequently Asked Questions
Should I ask AI to write tests before the implementation exists? Yes, if you can describe the input and the result a caller should receive. For the shipping offer, the team can agree on the $50 example before anyone picks a comparison operator.
Does this unit test prove checkout shows the correct fee? It checks the function's return value. A checkout screen that passes the wrong subtotal or displays cents as dollars needs a separate check at the point where those pieces meet.
What if nobody knows whether exactly $50 qualifies? Ask whoever owns the offer to decide what the customer should see. Save that answer next to the example before asking an assistant to turn it into a permanent test.
Sources
- GitHub Docs, Writing tests with GitHub Copilot, accessed December 1, 2026.
- pytest, Parametrizing test functions, accessed December 1, 2026.
- Python, unittest assertions, accessed December 1, 2026.
