CursorCode generationLevel 2: Context

How Do You Use Test-Driven Development With Cursor's Agent?

Have Agent write the tests first and confirm they fail, then commit them. Then tell it to make all tests pass without modifying them. This gives it a target it can check by itself, which Cursor's course says is when agents do their best work.

Prompt to copy
We're doing test-driven development. Don't write the function yet, and don't create mock implementations of code that doesn't exist.

Write tests for [function name] that cover:
- [expected behavior with example input and output]
- [error case and the exact error type]
- [edge case or limit]

Follow the test patterns in @[existing test file]. Then run the tests and confirm they fail for the right reason.

--- send this after you've committed the tests ---

Make all tests in @[new test file] pass. Follow the service patterns in @[service file]. Do NOT modify the tests. Keep going until they all pass, then show me the command you ran and its output.
  1. 1Write tests only
  2. 2Confirm they fail
  3. 3Commit the tests
  4. 4Code until they pass
  5. 5Review and commit
Cursor's suggested order. Committing the tests is what fixes the requirements in place.

Try it now

  1. 1Open Agent with Cmd+I (Ctrl+I) and send the first half of the prompt for one small function.
  2. 2Check that the tests fail because the code doesn't exist yet, and commit them from the Source Control panel.
  3. 3Send the second half. It worked if the suite passes and the diff shows no changes to the test file.

Use it when

Logic with clear inputs and outputs: pricing, parsing, permissions, validation, data transforms. It also suits a change you're nervous about, because you end up with tests that stay after the feature ships.

Skip it when

Layout and visual work, where tests say little about whether it looks right. Use the browser or a screenshot instead. Also skip it for exploratory prototypes you expect to throw away, since the requirements aren't known yet.

Why it works

Agent can run commands, so a failing test is concrete feedback: it sees what went wrong, adjusts and tries again. Without tests, the course says, it has no way to know whether its changes work. Committing the tests first matters because it locks in your requirements. If it later edits a test to get to green, the change shows up plainly in the diff. Cursor calls this especially useful for backend code, where you can't check correctness by looking at a screen.

Example

Cursor's course uses a discountCode() function. Valid codes reduce the price, expired codes raise InvalidCodeError, fixed-amount codes work, and the price never drops below zero. You point Agent at src/__tests__/pricing.test.ts for the style, commit the failing tests, then ask it to make them pass following src/services/PricingService.ts.

Going further

Make this a habit rather than a prompt. Write the steps as a skill, then press Option+Enter (Alt+Enter on Windows) when you choose it from the / menu. It then runs as a Custom Mode, staying in context on every turn until you exit, which Cursor's docs suggest for a team playbook like /tdd. And be careful with passing tests. Cursor's course warns that tests can check the wrong behavior, so ask "What edge cases aren't covered by these tests?" before you merge.

Common mistakes

  • Not saying it's TDD. Cursor warns the agent may otherwise create mock functions for code that doesn't exist yet.
  • Skipping the commit, so a test change gets lost among the code changes when you review.
  • Omitting "do not modify the tests". An agent chasing green may change the assertion rather than the code.
  • Running the entire slow suite on every attempt. Cursor's course points out that this cost repeats across every agent, so name a single test file while iterating.

Sources

Checked 6 November 2026. Products change often, so confirm details with the vendor.

Related

Beyond the course