/cursor-tutorials

How to generate tests using Cursor

Learn how to generate effective tests using Cursor with simple steps that boost code quality, speed development, and streamline your workflow.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to generate tests using Cursor

To generate tests in Cursor, you highlight the code you want tests for, press the ⌘K (or Ctrl+K on Windows/Linux) command palette, choose an action like “Write tests”, and Cursor will create the test file or insert tests into an existing one. The tool analyzes the code locally, drafts the tests, and lets you review, edit, or reject changes before they go into your project. You still run the tests using your local environment (Jest, Pytest, Mocha, etc.), and Cursor just helps write them faster.

 

What “Generating Tests in Cursor” Actually Means

 

Cursor is just a VS Code–based editor with AI built in. It doesn’t run tests for you and it doesn’t know your environment unless your project already has a test setup. When we say “generate tests,” we mean letting Cursor draft test code in the right files using whatever testing framework your project already uses.

  • If you have Jest in a Node/React project, Cursor generates Jest tests.
  • If you have Pytest in a Python project, Cursor generates Pytest tests.
  • If you have no test setup, Cursor can scaffold one, but you still have to install dependencies in your terminal.

 

How to Generate Tests, Step by Step

 

Here’s the practical workflow everyone uses in real projects.

  • Select the code you want tested (a function, class, API handler, etc.). Cursor works best when you give it a specific target.
  • Press ⌘K / Ctrl+K to bring up the AI actions menu.
  • Choose “Write tests” (or type it in). Cursor analyzes the selected code and creates a diff with proposed test code.
  • Review the diff carefully. Cursor sometimes assumes wrong imports or makes unrealistic mocks. Fix these before accepting.
  • Click Accept to apply the generated tests to your codebase.
  • Run tests normally using your local tools, like:
npm test
pytest

 

An Example: Node + Jest

 

Say you have a simple Node function:

// add.js
export function add(a, b) {
  return a + b;
}

You highlight this function, run "Write tests", and Cursor might generate something like this:

// add.test.js
import { add } from './add.js';

test('adds two numbers', () => {
  expect(add(2, 3)).toBe(5);
});

test('handles negative numbers', () => {
  expect(add(-1, -4)).toBe(-5);
});

This is real, working Jest code. You still run it with your normal test runner:

npm test

 

An Example: Python + Pytest

 

Same idea for Python:

# math_utils.py
def multiply(a, b):
    return a * b

Cursor might generate:

# test_math_utils.py
from math_utils import multiply

def test_multiply_basic():
    assert multiply(2, 3) == 6

def test_multiply_negative():
    assert multiply(-2, 4) == -8

And you run it like:

pytest

 

Tips to Make Cursor’s Test Generation Actually Good

 

  • Give Cursor context. Select the function or put the test file side‑by‑side. Cursor works best when the relevant code is visible.
  • Tell Cursor the framework. If your project uses Jest but also includes Mocha somewhere, explicitly say “Write Jest tests”.
  • Keep test files small. Cursor rewrites more accurately when files aren’t huge.
  • Use the Chat sidebar if the action isn’t enough. You can paste the function and say “Generate failing + passing tests for Jest”.
  • Always review the diff. Cursor occasionally invents imports or mocks that don’t exist. You catch these during the review.

 

What Cursor Does NOT Do

 

  • It doesn’t execute your tests. That’s your test runner (Jest, Pytest, etc.).
  • It doesn’t guarantee correctness. It gives you a draft; you validate it.
  • It doesn’t set up your environment automatically. Example: Jest must already be installed.

 

That’s the entire real workflow: Cursor helps you draft tests very fast, but the environment, framework, installs, and correctness are still your responsibility. With good prompts and careful review, it becomes one of the most productive parts of the editor.

Still stuck?
Copy this prompt into ChatGPT and get a clear, personalized explanation.

This prompt helps an AI assistant understand your setup and guide you through the fix step by step, without assuming technical knowledge.

AI AI Prompt

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022