How to Write Unit Tests With AI
Quick Answer
Paste your function or class, specify your testing framework, and ask AI to generate tests for all behaviors including edge cases. AI produces a solid first pass in minutes. Your job is to review that the tests actually verify meaningful behavior.
What This Workflow Helps You Do
- Write comprehensive tests faster
- Identify edge cases you might have missed
- Go from zero tests to solid coverage quickly
- Learn testing patterns for your language and framework
Step-by-Step Process
Step 1: Generate tests for a function (5 minutes)
Write unit tests for this [language] function using [testing framework].
Function to test:
[paste your function]
Please:
1. Test all expected behaviors (happy path)
2. Test edge cases (empty input, null values, boundary conditions, large inputs)
3. Test error cases (invalid input, expected exceptions)
4. Use descriptive test names that explain what is being tested
5. Add a brief comment above each test explaining what scenario it covers
Use [testing framework] conventions. Group related tests logically.
Step 2: Generate tests for a class or module (10 minutes)
Write unit tests for this [language] class using [testing framework].
[paste your class/module]
For each public method:
1. Test the primary use case
2. Test at least 2 edge cases per method
3. Test that errors are thrown or handled correctly
4. Test interactions between methods where relevant
Set up appropriate mocks for any external dependencies.
Use [testing framework] best practices throughout.
Step 3: Identify missing test coverage (5 minutes)
Review these existing tests and identify what's missing.
Code being tested:
[paste function or class]
Existing tests:
[paste your current tests]
Please identify:
1. Behaviors that aren't tested at all
2. Edge cases that are missing
3. Error scenarios that should be tested
4. Any tests that might give false confidence (testing implementation rather than behavior)
Then write the missing tests.
Step 4: Write tests for a bug fix (5 minutes)
Write a failing test first, before or after fixing the bug, to prevent regression.
I fixed a bug in this code. Write a regression test that would have caught this bug.
The bug: [describe what was going wrong]
The fix: [describe what you changed]
The fixed code: [paste the relevant function]
Write:
1. A test that specifically targets the bug scenario
2. The test should fail against the buggy code and pass against the fixed code
3. A descriptive test name that explains the specific bug being prevented
Step 5: Improve test quality
Review these unit tests and improve their quality.
[paste your tests]
Check for:
1. Tests that could pass even if the code is broken
2. Tests that are too tightly coupled to implementation details
3. Missing assertions
4. Redundant tests that add no coverage value
5. Tests that are hard to understand
Rewrite any weak tests and explain what you improved and why.
Testing Checklist
- Happy path (normal input) is tested
- Edge cases are tested (empty, null, boundary values)
- Error cases are tested
- Test names describe what scenario they cover
- Tests would actually fail if the code had the bug they’re testing
- No tests rely on implementation details that could change
Common Mistakes
Tests that always pass. AI occasionally writes tests that pass trivially, always assert on specific values, not just that something ran. After generating, briefly check that a test would fail if you broke the function.
Testing the implementation, not the behavior. Good tests specify what a function should do, not how it does it. If your test breaks every time you refactor, it’s testing implementation.
Skipping the review. AI-generated tests need human review. Check that the test names describe the scenario accurately and that the assertions are meaningful.
Continue learning
Explore related guides, tools, workflows, and prompts that help you go deeper into this topic.
More step-by-step AI workflows.
Open workflowCopy-paste prompts for every use case.
View promptsLearn the fundamentals behind these workflows.
Read guideLearn how this AI tool fits into practical workflows.
View toolLearn how this AI tool fits into practical workflows.
View toolLearn how this AI tool fits into practical workflows.
View toolBrowse more AI workflows
Explore step-by-step guides for writing, research, marketing, content, and more.
Frequently Asked Questions
Can AI write unit tests for my code?
Yes. AI is very effective at generating unit tests. It can analyze a function, identify the expected behaviors and edge cases, and write tests for all of them. Always review tests to ensure they test real behavior, not just make assertions pass.
What testing frameworks can AI write tests for?
AI can write tests for virtually any framework: Jest, Vitest, pytest, JUnit, RSpec, Go testing, and more. Specify your framework and language in the prompt.
How do I make sure AI-generated tests are good?
Check that tests verify real behavior, not just that the code runs. Ensure edge cases are covered. Make sure tests would actually fail if the code had a bug. Run the tests and see if they pass, if they all pass immediately without any failures, some may be testing nothing meaningful.
Last updated: