20 tested prompts across 4 stages. Works with ChatGPT, Claude, and Gemini.

Write comprehensive unit tests faster using Cursor to improve code coverage and catch bugs before they reach production. This guide walks you through every stage of Cursor for Unit Tests, from Plan your testing strategy all the way through Build a testing culture, with a tested, copy-ready prompt at each step. Each stage targets a specific phase of the process so you always know exactly what to ask and what output to expect. Works with ChatGPT, Claude, and Gemini and any other major AI tool.
Stage 1
Start here to decide what to test, in what order, and what good coverage looks like.
Identify what to test
Here is my code: [PASTE CODE]. What are the most important things to test? Identify the key behaviors, edge cases, and failure modes that unit tests should cover. Prioritize by risk.
Choose testing patterns
What testing patterns and approaches should I use for this type of code: [DESCRIBE CODE TYPE: SERVICE LAYER / PURE FUNCTIONS / REACT COMPONENTS / API HANDLERS]? Recommend the right test structure.
Define test coverage goals
For this module: [PASTE CODE], what test coverage should I aim for? Which lines and branches are most critical to cover and which are lower priority? Be practical, not dogmatic about 100%.
Set up a test file
Set up a test file for [FILE NAME] using [JEST / VITEST / PYTEST / OTHER]. Create the basic structure: imports, describe blocks for each function, and placeholder test cases I should fill in.
Plan mocking strategy
This code depends on [LIST DEPENDENCIES: DATABASE, EXTERNAL API, FILESYSTEM]. What should I mock and how? Suggest a mocking strategy that keeps tests fast and isolated without mocking too much.
Stage 2
These prompts help you write unit tests that are clear, fast, and genuinely useful.
Write tests for a function
Write unit tests for this function using [TESTING FRAMEWORK]: [PASTE FUNCTION]. Cover: the happy path, edge cases (empty input, null, boundary values), and error cases. Add a test for each.
Write tests for a class
Write unit tests for this class: [PASTE CLASS]. Test each public method, the constructor, state changes, and interaction between methods.
Write tests for async code
Write unit tests for this async function: [PASTE ASYNC CODE]. Cover: successful resolution, error/rejection handling, and any timeout or retry behavior. Use proper async testing patterns.
Write integration tests
Write integration tests for this module that test multiple components working together: [PASTE CODE]. These should be slightly higher level than unit tests but still fast and deterministic.
Test with mocks
Write tests for this code that mock its dependencies: [PASTE CODE WITH DEPENDENCIES]. Mock [LIST DEPENDENCIES] and test that the code correctly calls and handles responses from each.
Stage 3
Use these prompts to strengthen your tests and remove brittle or redundant coverage.
Review existing tests
Review these tests and tell me what they are missing: [PASTE TESTS]. Are they testing the right things? What edge cases are not covered? What would fail to catch common bugs?
Make tests more readable
Improve the readability of these tests: [PASTE TESTS]. Use descriptive test names, the Arrange-Act-Assert pattern, and clear assertions. Rename vague test descriptions.
Reduce test duplication
These tests have a lot of duplicated setup code: [PASTE TESTS]. Refactor them to use [BEFOREEACH / TEST FIXTURES / PARAMETERIZED TESTS] to reduce repetition while keeping clarity.
Add parameterized tests
Convert these similar test cases into a parameterized test: [PASTE REPEATED TEST CASES]. Use [TEST.EACH / PYTEST.MARK.PARAMETRIZE / EQUIVALENT] to test all the cases with less code.
Fix flaky tests
This test is flaky — it sometimes passes and sometimes fails: [PASTE TEST]. What are the most likely causes of flakiness here, and how do I fix it?
Stage 4
Use these prompts to improve how your team thinks about and prioritises testing.
Write tests first (TDD)
I want to build [FEATURE] using TDD. Before I write any implementation code, write the tests that define what the code should do. Use [TESTING FRAMEWORK]. I'll implement the code to make these pass.
Reach a coverage target
My current test coverage is [X]% and I want to reach [Y]%. Here are the uncovered lines: [PASTE COVERAGE REPORT EXCERPT]. Write tests specifically targeting the uncovered code.
Create test helpers
I have repeated patterns in my tests: [DESCRIBE THE PATTERN]. Create test helper functions or factories that reduce this duplication without making tests harder to understand.
Document test strategy
Write a brief testing strategy document for this codebase: [DESCRIBE PROJECT]. Cover: what we test, how, what we mock, coverage goals, and how to run tests. Keep it under one page.
Review a PR's tests
Review the tests in this PR: [PASTE TESTS]. Does the test coverage match the scope of the code changes? Are the right things being tested? Suggest any missing test cases.
Cursor reads your implementation code and generates tests that match its actual behavior. You can open a file and ask Cursor to write tests for a specific function, and it will generate a test file with relevant test cases, mocks, and edge cases already considered.
Cursor works with any testing framework. For JavaScript/TypeScript, Jest and Vitest are the most common. For Python, pytest. For Go, the standard testing package. Tell Cursor which framework you are using and it will generate tests in the right format.
TDD (test first) produces better-designed, more testable code. Test-after is more practical when requirements are unclear. Most developers use a mix: write tests for bug fixes always, write tests during development for critical code, add tests retroactively for uncovered areas.
Test coverage percentage is a proxy metric, not a goal. Better question: do your tests catch the bugs that matter? Cover every public API, every non-trivial branch, and every error path. 80% coverage of the right code beats 100% coverage of trivial code.
Yes. Give Cursor the legacy function and describe its expected behavior. It will write characterization tests that document current behavior — useful as a safety net before refactoring. These tests may capture bugs too, which is a bonus.
AI Prompts for Cursor for Debugging
Debugging code can be time-consuming and frustrating for developers.
See promptsAI Prompts for Cursor for Code Review
Code review is where most developers either speed through too fast or get lost in line-by-line nitpicking that misses bigger structural problems.
See promptsAI Prompts for ChatGPT for Unit Tests
Write comprehensive unit tests faster using ChatGPT to improve coverage and catch bugs before they reach production..
See prompts