testing-best-practices

Enforce parallel execution, fake objects, and factory injection in Go tests.

10|14|Updated Feb 26, 2026
One-click install
npx skills add https://github.com/openkaiden/kdn --skill testing-best-practices-openkaiden
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: testing-best-practices
Source: https://github.com/openkaiden/kdn/tree/main/.agents/skills/testing-best-practices
Command: npx skills add https://github.com/openkaiden/kdn --skill testing-best-practices-openkaiden

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Tests in Go projects can become slow and flaky due to improper concurrency and tight coupling. This guide prescribes parallel execution, the use of fake objects over mocks, and factory injection patterns to improve test reliability and maintainability.

Core Features & Use Cases

  • Parallel testing: Enable t.Parallel() to run tests concurrently, reducing suite duration.
  • Fakes over mocks: Use lightweight fake implementations to gain full control over behavior and dependencies.
  • Factory injection: Inject factories to decouple code paths and enable deterministic testing.
  • Use Case: Apply these patterns to a multi-package Go project to accelerate CI, simplify test setup, and reliably reproduce edge cases.

Quick Start

Run tests with go test ./... and enable parallel testing by calling t.Parallel() at the start of each Test function.

Frequently Asked Questions about testing-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix flaky and slow Go tests in my CI pipeline?

Fix flaky Go tests by enforcing parallel execution with t.Parallel(), replacing mocks with controlled fake objects, and using factory injection to decouple dependencies for deterministic, reproducible outcomes.

What is the difference between using fakes and mocks for Go unit testing?

Fakes are lightweight, custom implementations that give you full control over behavior and dependencies, whereas mocks often enforce tight coupling. Using fakes in Go testing ensures deterministic and reproducible outcomes.

How do I set up factory injection for Go integration tests?

Set up factory injection by injecting factories into your Go test functions to decouple code paths. This enables deterministic testing across multi-component systems and ensures reproducible edge cases.

Can I use t.Parallel() with t.Setenv() for concurrent Go testing?

t.Parallel() enables concurrent Go test execution, but t.Setenv() has limitations in parallel contexts. This pattern prescribes specific factory injection techniques to ensure deterministic outcomes when managing environments.

When should I enable parallel execution in Go test suites?

Enable parallel execution by calling t.Parallel() at the start of each Test function to run tests concurrently. This is suitable for multi-package Go projects and significantly reduces overall CI suite duration.