Amazon Q Unit Testing Prompt Template
You get isolated unit tests that mock external calls correctly, assert on behavior, and run fast without network or database access. This version is set up for Amazon Q: name the AWS services, regions and resources involved, and paste the relevant code or configuration. Below you'll find the prompt, how to run it in Amazon Q, a worked example and answers to common questions.
Write unit tests for the function below. Isolate it from everything external. ## Code under test Language and test tools: [e.g. Python 3.12, pytest, unittest.mock] Module path: [e.g. weather.py] [paste the function] ## Dependencies to mock [e.g. HTTP calls, database, clock, random, file system] ## Behavior to cover - Success: [what it returns] - Failures: [what should raise or return an error] - Edge cases: [e.g. missing fields, empty response] ## Instructions - Patch dependencies where the module under test looks them up, not where they are defined. - Assert on the returned value and on how mocks were called (arguments, call count). - No real network, database or sleeping. - One behavior per test, with descriptive names. ## Output format 1. A complete test file 2. A one-line note on each mock and why it is needed Context - Reference material: [paste it below, or attach the file]
How to use this prompt
- 1
Open Amazon Q
Open Amazon Q in your IDE or the AWS console and paste the prompt.
- 2
Give it context
Name the AWS services, regions and resources involved, and paste the relevant code or configuration.
- 3
Review and iterate
Ask follow-ups in the same chat, and test changes in a non-production account first.
Working in Amazon Q
- •Amazon Q is built for AWS work, so name the services (for example Lambda, S3, IAM) your question involves.
- •Never paste credentials or secret values; describe the IAM setup instead.
- •For infrastructure changes, ask for the change as code (CloudFormation, CDK or Terraform) so you can review it.
Worked example
What you give it
Python 3.12, pytest, unittest.mock. Module: weather.py
import requests
def get_temperature(city):
resp = requests.get("https://api.example.com/weather", params={"city": city}, timeout=5)
resp.raise_for_status()
return resp.json()["temp_c"]
Mock: the HTTP call. Cover success and an HTTP error from the server. Tests run in CI with no network access, so nothing can reach the real API.What a good answer looks like (shortened)
1. test_weather.py
from unittest.mock import patch
import pytest
import requests
from weather import get_temperature
@patch("weather.requests.get")
def test_returns_temperature(mock_get):
mock_get.return_value.json.return_value = {"temp_c": 21.5}
assert get_temperature("Oslo") == 21.5
mock_get.assert_called_once_with(
"https://api.example.com/weather", params={"city": "Oslo"}, timeout=5
)
@patch("weather.requests.get")
def test_raises_on_http_error(mock_get):
mock_get.return_value.raise_for_status.side_effect = requests.HTTPError("500")
with pytest.raises(requests.HTTPError):
get_temperature("Oslo")
2. Mock note: weather.requests.get is patched because that is the name get_temperature looks up at call time. No request leaves the machine.Tips for better results
- •Always give the Module path. Correct patch targets depend on it, and a wrong path is the most common reason mocked tests hit the network.
- •List the clock and random under Dependencies to mock if the function uses them. Tests that depend on today's date fail on a different day.
- •Never paste credentials or secret values; describe the IAM setup instead.
- •Avoid this: Patching a dependency where it is defined instead of where the code under test imports it, so the real call still runs.
Get the free prompt pack
Our best coding prompts in one Markdown file, delivered by email. The templates here stay free either way — the pack just saves you the copying.
Variations
JavaScript
Replace Code under test tools with: TypeScript, Vitest, vi.mock. Mock [module] at the top of the file and reset mocks between tests.
Class with dependencies
Add this line to Instructions: Inject dependencies through the constructor in tests instead of patching, and show the minimal refactor needed.
Coverage gap
Add this section: ## Coverage report [paste uncovered lines] and write tests only for those lines.
Save it as a Retext snippet
Retext is a free text expander for Mac. Save this prompt once, then type ;unittest in any app (ChatGPT, Claude, your editor, a browser tab) and the full prompt is pasted in.
- Click Add to Retext. The prompt installs as a snippet with the trigger ;unittest.
- Change the trigger in Retext if you prefer a different one.
- Type the trigger wherever you want the prompt, then fill in the brackets.
Frequently asked questions
What is the difference between unit tests and integration tests in a prompt?
A unit test prompt asks to mock everything outside the function so the test is fast and isolated. An integration test prompt asks to use the real database or service so you can see the parts working together.
Is this prompt only for AWS projects?
Amazon Q is most useful when the work involves AWS services. The prompt still works for general code, but say what stack you use.
Why does my mocked test still make a network call?
Usually the patch target is wrong. Patch the name in the module that uses it, like weather.requests.get, not requests.get in general.
How do I reuse this prompt without copying it every time?
Click Add to Retext to save it as a snippet. Then type ;unittest in any Mac app, including your AI tool, and the full prompt is pasted in.
Related Templates
Get the free prompt pack
Our best coding prompts in one Markdown file, delivered by email. The templates here stay free either way — the pack just saves you the copying.