Skip to main content
IntermediateQuality & SecurityEst. Time: 25-35 min

Automated Testing & TDD Cycle

Automate the Red-Green-Refactor development cycle with strict test verification and zero unverified claims.

The Problem

AI models frequently claim tests are passing without actually running the test binary. Unchecked assertions and obsolete test suites create a false sense of security.

Who It Is For

Engineers who prioritize high software quality, high test coverage, and bulletproof regressions.

Prerequisites & Tools

Prerequisites Checklist:
  • Configured test runner (Node Test Runner, Jest, Vitest, PHPUnit, Paratest, PyTest)
Tool / CommandTypeRole in Tutorial
create_testsmcpDraft comprehensive test scenarios before coding
run_testsmcpExecute test runner and capture stdout/stderr evidence
analyze_changemcpAnalyze change impact and suggest targeted regression tests (mode: tests)

Example Task Contract

Exact start_task payload representing this workflow in DevWorkflow local-first governance.

AI Tool Call: start_task payload
{
  "description": "Implement coupon code validation service using strict TDD",
  "type": "feature",
  "plan": [
    "Write failing tests for expired, invalid, and minimum-spend coupon codes",
    "Implement CouponValidator domain service",
    "Run test runner and verify 100% green suite"
  ],
  "definitionOfDone": [
    "Failing test verified failing before implementation",
    "All coupon validation rules covered by unit tests",
    "Test suite passes with 0 failures and >90% coverage"
  ]
}

Step-by-Step Tutorial Workflow

Follow these sequential actions with validated copyable commands and expected outputs.

1

Draft Test Scenarios (Red Phase)

Create test files first. Run the test command and verify that the tests fail as expected.

Copyable Command
Terminal Command
node --test tests/calculator.test.js
Expected Terminal Output
✖ calculateDiscount throws expected error (0.4ms)
  AssertionError: expected 15, got undefined
2

Implement Passing Logic (Green Phase)

Write the minimum code required to turn all tests green.

Copyable Command
Terminal Command
node --test tests/calculator.test.js
Expected Terminal Output
✔ calculateDiscount applies tier percentage (1.1ms)
ℹ tests 6 · pass 6 · fail 0
3

Refactor & Verify (Refactor Phase)

Clean up code structure without altering public behavior. Ensure tests remain 100% green.

Copyable Command
Terminal Command
npm test
Expected Terminal Output
All test suites passing.

Verification Steps

Verify that your work genuinely satisfies the task contract before closing.

Inspect test execution logs directly to verify actual pass count
Verify tests test assertions, not empty mocks

Limitations & Troubleshooting

Issue: Paratest / Parallel test runner conflict on SQLite database
Resolution: Use unique memory databases (`:memory:`) or transaction rollback wrappers per test process.