Testing

Standards for unit testing, table-driven tests, and mocking in Golang.

fierzone Updated

File contents

Golang Testing Standards

Priority: P0 (CRITICAL)

Principles

  • Table-Driven Tests: The idiomatic way to write tests in Go.
  • Subtests (t.Run): Run table entries as subtests for better reporting.
  • Parallel Usage: Use t.Parallel() for independent tests to speed up execution.
  • Mock Interfaces: Mock at the boundaries (interfaces).

Tools

  • Stdlib: testing package is usually enough.
  • Testify (stretchr/testify): Assertions (assert, require) and Mocks.
  • Mockery: Auto-generate mocks for interfaces.
  • GoMock: Another popular mocking framework.

Naming

  • Test file: *_test.go
  • Test function: func TestName(t *testing.T)
  • Example function: func ExampleName()

Anti-Patterns

  • Sleeping in tests: Use channels/waitgroups or retry logic.
  • Testing implementation details: Test public behavior/interface.

References

  • Table-Driven Tests
  • Mocking Strategies

fierzone/agent-skills-standard/tree/main/skills/golang/testing commit 391bfb1f4b

Frequently asked questions

npx skillmds@latest add fierzone/testing