2.5 Writing & Running Tests
Course: Claude Code - Essentials Section: Core Workflows Video Length: 2-5 minutes Presenter: Daniel Treasure
Opening Hook
Tests are your safety net. They catch bugs before users do, and they give you confidence to refactor fearlessly. Claude Code can write tests for you. In this video, we'll show how Claude generates tests, how to run them, and how tests help you ship code with confidence.
Key Talking Points
- Claude Generates Tests from Code
- Ask: "Write unit tests for this function"
- Claude analyzes the code and writes tests covering the main cases
- Tests are created in the project's test framework (Jest, pytest, etc.)
-
Show on screen: Ask Claude to write tests; see the test file appear
-
Different Test Types
- Unit tests: Test individual functions in isolation
- Integration tests: Test how components work together
- Edge case tests: Cover boundary conditions and error scenarios
- Claude can write any of these based on your request
-
Show: Ask for "edge case tests" and see different test cases
-
Running Tests and Reading Results
- Tests run with your project's test framework:
npm test,pytest, etc. - Pass/fail results are clear and easy to interpret
- Claude helps you understand failures
-
Show: Run tests; show passing and (optionally) a failing test
-
Improving Test Coverage
- Ask Claude: "What paths aren't tested?" or "Add tests for error scenarios"
- Claude identifies gaps and fills them
- Comprehensive tests prevent regressions
-
Show: Run coverage report (if available); highlight what's untested
-
Tests Give You Confidence
- Tests act as regression detection: they catch when you break something
- When refactoring (like in 2.2), run tests first to confirm you didn't break anything
- Tests document expected behavior
- Show: Make a change that would break a test; show the test catches it
Demo Plan
Demo Project: Continue with the same project from 2.1-2.4
Setup: - Project has Jest or Pytest already installed (or Claude can add it) - At least one function/module worth testing (use something from earlier videos) - Terminal ready to run tests
On-Camera Sequence (2-3 minutes):
- Ask Claude to Write Tests
Write unit tests for the getUserName function - Claude creates a test file (e.g.,
getUserName.test.jsortest_getUserName.py) - Show the test file with multiple test cases
- Explain: these test different inputs and scenarios
-
~40 seconds
-
Run the Tests
bash npm test # or pytest - Show tests running
- All tests pass (or one fails, demonstrating failure output)
-
~30 seconds
-
Add Edge Case Tests
Add tests for error scenarios and null inputs - Claude adds more test cases
- Covers boundary conditions
- Show the expanded test file
-
~30 seconds
-
Run Tests Again
bash npm test - Show all tests passing
- Brief coverage report (if available)
-
~20 seconds
-
(Optional) Break and Fix
- Make a small deliberate change to the code that breaks a test
- Run tests again
- Show the failure message
- Ask Claude to fix it
- Run tests one more time to confirm all pass
- ~30 seconds (only if time permits)
Code Examples & Commands
Asking Claude to write tests:
Write unit tests for the calculateTotal function
Add tests for edge cases and error scenarios
Generate integration tests for the API endpoints
Write tests for error handling
Create a test file for the authentication module
Running tests:
npm test # JavaScript/Node
pytest # Python
python -m pytest # Python (explicit)
./gradlew test # Java/Gradle
Reading test output: - PASS/FAIL for each test - Number of tests passed/failed - Coverage percentage (if enabled) - Detailed error messages for failures
Example test structure (JavaScript with Jest):
describe('calculateTotal', () => {
test('sums positive numbers', () => {
expect(calculateTotal([1, 2, 3])).toBe(6);
});
test('handles empty array', () => {
expect(calculateTotal([])).toBe(0);
});
test('throws error on null input', () => {
expect(() => calculateTotal(null)).toThrow();
});
});
Gotchas & Tips
-
Tests Require Setup – Make sure your project has a test framework installed (Jest, Pytest, Mocha, etc.). If not, Claude can help add one.
-
Test Names Matter – Claude writes descriptive test names like "should return 0 for empty array." These help you understand what's being tested.
-
Red-Green-Refactor Cycle – Best practice: write tests first (they fail), then write code to make them pass, then refactor. Claude can assist all three steps.
-
Coverage Doesn't Mean Completeness – 100% code coverage doesn't mean the code is bug-free. Well-written tests focus on likely failure modes.
-
Run Tests Before Committing – In real workflows, tests run automatically before merge (CI/CD). In these videos, you run them manually.
-
Mocking and Dependencies – For advanced testing (mocking external APIs, databases), Claude can help set up. For this intro, focus on simple unit tests.
Lead-out
Tests verify that your code works. But how do other developers know what your code does? That's documentation. In the next video, we'll show how Claude helps you write clear, comprehensive documentation that keeps your project understandable.
Reference Material
- Common Workflows - Writing Tests: https://code.claude.com/docs/en/common-workflows
- Quickstart - Run Tests: https://code.claude.com/docs/en/quickstart
- Best Practices Guide: https://code.claude.com/docs/en/best-practices
Relevant Articles & Posts
- Testing Patterns: Joe Njenga's "17 Best Claude Code Workflows" (Medium)
- Production Testing: Lukasz Fryc's "15 Tips from Running 6 Projects" (DEV Community)
- Official Resource: Common Workflows - Claude Code Docs (https://code.claude.com/docs/en/common-workflows)
Additional Notes
- Use a real, meaningful function for testing (not
add(a,b)unless it's your actual demo project) - Show passing tests first; then (optionally) a failure to demonstrate how Claude helps fix it
- Emphasize: Claude doesn't replace thinking about test cases—it accelerates writing them
- Note: comprehensive testing is part of "well-configured projects" that ship features 5-10x faster (from recent articles prep)
- Mention that in production, tests often run automatically in CI/CD pipelines (covered in advanced sections if applicable)