ClaudeChatGPTCopilotDeveloper & CodeIntermediate

Unit Test Writing Framework

Write comprehensive unit tests using AAA pattern (Arrange, Act, Assert) with edge cases, mocks, and coverage targets.

Updated June 2026

unit-test-writing-framework.txt
Copy & Download at the bottom ↓
You are a software engineer who writes exemplary unit tests. Your principles: (1) tests document behavior — the test name should say what the code does, (2) Arrange-Act-Assert — clear structure in every test, (3) each test tests ONE thing — if it fails, you know exactly what broke, (4) tests must be fast and independent — no dependency on order or shared state, (5) test behavior, not implementation — tests should not break when you refactor.

Create unit tests for:

**Language:** [e.g.: TypeScript, Python, Go, Java]
**Test framework:** [e.g.: Jest, Vitest, pytest, Go testing, JUnit]
**Code to test:** [paste the function/class/module]
**Context:** [what this code does in the system]
**External dependencies:** [database, APIs, filesystem — what needs to be mocked]
**Known special cases:** [edge cases you know exist]

Deliver:

**1. Code analysis** - What it does, inputs, outputs, side effects
**2. Scenario matrix** - Table with scenario, input, output, type
**3. Complete tests** - Code organized by describe/context
**4. Error tests** - Null input, wrong types, failing dependency
**5. Coverage achieved** - Lines and branches covered
**6. Maintenance tips** - How to maintain the tests as the code evolves

When to Use

When implementing tests for the first time in a project

To standardize how the team writes tests

When test coverage is low or tests are fragile

Before refactoring critical code

How to Use This Prompt

1

Copy the prompt below into Claude or ChatGPT

2

Paste the function or module you want to test

3

Receive the complete tests with explanations

4

Implement and run in your test runner

Example Input

Language: TypeScript
Framework: Jest
Code: Function calculateDiscount(price, couponCode, userTier)
- Applies discount based on coupon + user tier
- Premium = 10% extra, VIP = 20% extra
- Coupon can be percentage or fixed value
- Minimum final price is R$1.00

Expected Output

describe('calculateDiscount', () => {
  describe('without coupon', () => {
    it('returns original price for regular user without coupon', () => {
      const result = calculateDiscount(100, null, 'regular');
      expect(result).toBe(100);
    });

    it('applies 10% discount for premium user', () => {
      const result = calculateDiscount(100, null, 'premium');
      expect(result).toBe(90);
    });

    it('applies 20% discount for VIP user', () => {
      const result = calculateDiscount(100, null, 'vip');
      expect(result).toBe(80);
    });
  });

  describe('edge cases', () => {
    it('never returns value less than R$1.00', () => {
      const result = calculateDiscount(5, 'SAVE99', 'vip');
      expect(result).toBe(1);
    });

    it('throws error for negative price', () => {
      expect(() => calculateDiscount(-10, null, 'regular'))
        .toThrow('Price must be positive');
    });
  });
});

Ready to use this prompt?