AI-Powered Testing — Write Tests with AI
Copilot + Claude Code TDD, Playwright, test generation
Writing tests is one of the most tedious parts of development — and one of the highest-value places to use AI. Generating test cases, writing boilerplate assertions, and creating diverse test data are all mechanical tasks that AI handles well. This guide covers the complete AI testing workflow from unit tests to Playwright E2E.
What You'll Learn
- Generating unit tests with GitHub Copilot (inline and chat)
- Writing Playwright E2E tests with Claude
- TDD workflow with AI: write test first, AI writes code to pass
- Generating realistic test data for edge cases
- Using AI to identify gaps in test coverage
Generating Unit Tests with GitHub Copilot
Method 1: Inline Comment Trigger
Open any function file. Below the function, type a comment:
// Tests for calculateGSTAmount function
Press Enter and Copilot suggests the test suite. Accept with Tab. This generates basic happy-path tests. Add more comments to generate edge cases:
// Test when price is 0
// Test when GST rate is 0
// Test with floating point prices
Method 2: Copilot Chat
Select a function, right-click → Copilot → Generate Tests. Or open Copilot Chat and ask:
Write comprehensive Jest unit tests for this function.
Cover: normal cases, edge cases, and error cases.
Include at least one test for null/undefined inputs.
Mock any external dependencies.
Method 3: Test Completions
When you write it('should...' or test('...)`, Copilot completes the test body based on the description. This works well when you want to control the test names but let AI write the assertions.
Example — Before Copilot completion:
it('should return 0 when cart is empty',
After Copilot completion:
it('should return 0 when cart is empty', () => {
const cart = { items: [], userId: '123' };
const total = calculateCartTotal(cart);
expect(total).toBe(0);
});
🇮🇳 India Note: When generating tests for India-specific functions (GST calculation, PAN validation, Aadhaar check digits, IFSC code parsing), explicitly tell Copilot the Indian format rules in the test descriptions. "Test with a valid Indian PAN format (5 letters, 4 digits, 1 letter)" generates much better test cases.
Writing Playwright E2E Tests with Claude
For full Playwright test suites, Claude is more thorough than Copilot because it can plan across multiple interactions and understand complex user flows.
Prompt structure for E2E tests:
Write Playwright TypeScript E2E tests for the user registration flow.
Current URL: /register
Form fields:
- Name (text, required)
- Email (text, required, must be valid format)
- Phone (text, required, must be 10 digits)
- Password (password, required, min 8 chars)
- Confirm Password (password, must match)
- Submit button
Success: redirect to /dashboard with welcome toast
Error: inline validation messages appear below fields
Write tests for:
1. Happy path — valid form submission
2. Empty form submission
3. Invalid email format
4. Phone number less than 10 digits
5. Password mismatch
6. Duplicate email (API returns 409 error)
Use Playwright best practices: use test fixtures, avoid hardcoded waits, use data-testid selectors if available.
Claude generates a complete test file with proper structure, selectors, assertions, and error handling.
TDD Workflow with AI
Test-Driven Development (TDD) — write tests first, then implementation — works extremely well with AI assistance:
Step 1: Write test cases (you):
Write failing Jest tests for a function `validateIndianPhoneNumber(phone: string): boolean`.
Requirements:
- Returns true for 10-digit numbers with or without +91 prefix
- Returns false for numbers with fewer than 10 digits
- Returns false for numbers with letters
- Returns false for empty strings
- Returns true for formats: '9876543210', '+919876543210', '09876543210'
Step 2: Run tests — they all fail (expected)
Step 3: Give failing tests to AI:
These tests are currently failing.
Write the implementation for validateIndianPhoneNumber that makes all of them pass.
@file:tests/validators.test.ts
Step 4: AI writes implementation to pass the tests
Step 5: Run tests — all pass (if implementation is correct)
This workflow forces the AI to write code that actually meets your requirements, not code that looks correct but has subtle gaps.
💰 Free Deal: This TDD workflow works on the free tiers of all AI tools. Copilot free (50 chat messages) plus Claude free (daily limit) give you enough interactions to do this for several functions per day at no cost.
Generating Realistic Test Data
Tests with unrealistic data miss real-world bugs. AI is excellent at generating:
Prompt for diverse test data:
Generate 20 test cases for an Indian address validation function.
Include:
- Valid addresses from different states (Maharashtra, Tamil Nadu, UP, Karnataka)
- Valid PIN codes for different regions
- Invalid PIN codes (5 digits, 7 digits, non-numeric)
- Missing required fields (no state, no PIN)
- Very long strings that might cause buffer issues
- Special characters that might cause injection vulnerabilities
- Unicode characters (Hindi text in address fields)
Format as a TypeScript array of objects matching the AddressInput type:
{ street: string, city: string, state: string, pincode: string }
For API testing, generate edge case payloads:
Generate 15 edge case JSON payloads for a payment processing API.
Normal payload: { amount: 100, currency: "INR", userId: "u_123" }
Include cases:
- Amount as string instead of number
- Negative amount
- Zero amount
- Extremely large amount (₹10 crore)
- Missing required fields
- SQL injection strings in userId
- XSS strings in userId
- Unicode in currency field
AI for Test Coverage Analysis
Use Claude or Copilot to identify what you are not testing:
Looking at this function and its current tests, identify:
1. Code paths that are not covered by any test
2. Edge cases that are missing
3. Error conditions that are not tested
4. Integration points that need their own tests
Function: @file:services/OrderService.ts
Current tests: @file:tests/OrderService.test.ts
Claude will highlight uncovered branches, missing error handling tests, and integration scenarios you have not considered.
Another useful pattern — coverage-first:
I am aiming for 80% branch coverage on this module.
Looking at the current test file, what additional tests would move coverage from current state to 80%?
@file:src/lib/utils.ts
@file:tests/utils.test.ts
Practical Testing Workflow
For a new feature or bug fix:
- Generate initial unit tests with Copilot (quick, inline)
- Add edge cases identified by Claude (more thorough analysis)
- Use TDD for complex business logic (guarantee correctness)
- Write E2E tests with Claude for critical user flows
- Ask AI to review coverage gaps before shipping
Official Resources
- GitHub Copilot Testing Guide — Official Copilot testing docs
- Playwright Documentation — Full Playwright reference
- Jest Documentation — JavaScript testing with Jest
- pytest Documentation — Python testing
- Testing JavaScript (Kent C. Dodds) — Testing fundamentals (paid but comprehensive)
Community Questions
0No questions yet. Be the first to ask!