Retext
GeminiAI AssistantUnit Testing

Gemini 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 Gemini: attach files or images the prompt refers to, or paste the text below the instructions. Below you'll find the prompt, how to run it in Gemini, 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
- Reference material: [paste it below, or attach the file]

How to use this prompt

  1. 1

    Open Gemini

    Start a new chat in Gemini and paste the prompt.

  2. 2

    Give it context

    Attach files or images the prompt refers to, or paste the text below the instructions.

  3. 3

    Review and iterate

    Reply in the same chat to refine the result.

  4. 4

    Save what you repeat

    For a prompt you run often, save it as a Gem with custom instructions.

Working in Gemini

  • •Gemini accepts images and files as well as text, so attach a screenshot when the task is visual.
  • •Ask for the exact output format; otherwise answers tend to come with extra headings and summaries.
  • •If you use Google Docs or Sheets, ask for output you can paste straight in, such as a table.

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.
  • •If you use Google Docs or Sheets, ask for output you can paste straight in, such as a table.
  • •Avoid this: Mocking so much that the test only checks the mocks, not anything the function actually does.

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.

Does Gemini need the material pasted in?

Attach or paste anything the prompt refers to. It can't see your files unless you share them in the chat.

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.