Retext
CursorIDEUnit Testing

Cursor 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 Cursor: use @ to attach the files or folders the prompt is about, so Cursor reads them instead of guessing. Below you'll find the prompt, how to run it in Cursor, a worked example and answers to common questions.

Installs as a snippet — type ;unittest to paste this prompt anywhere on your Mac.
Prompt Template
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
- Files involved: [list the files, or reference them in the chat]
- How I build and test: [e.g. npm test]

How to use this prompt

  1. 1

    Open Cursor

    Open the chat panel in Cursor and paste the prompt as a new message.

  2. 2

    Give it context

    Use @ to attach the files or folders the prompt is about, so Cursor reads them instead of guessing.

  3. 3

    Review and iterate

    Review each proposed diff before you accept it, then reply in the same chat to adjust.

  4. 4

    Save what you repeat

    Put rules you repeat (stack, style, test command) in a project rules file so every chat starts with them.

Working in Cursor

  • •Cursor works on files in your open project, so name the files and functions you mean.
  • •For a small, local change, select the code and use the inline edit command instead of the chat panel.
  • •Long chats drift. Start a new chat for a new task and re-attach only the files it needs.

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.
  • •Cursor works on files in your open project, so name the files and functions you mean.
  • •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.

  1. Click Add to Retext. The prompt installs as a snippet with the trigger ;unittest.
  2. Change the trigger in Retext if you prefer a different one.
  3. 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.

Should I paste code into Cursor or reference the file?

Reference the file with @. Cursor reads the current version from your project, which avoids pasting stale code and keeps the prompt short.

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.