golang-testing
Original:🇨🇳 Chinese
Translated
Structured workflow for writing high-quality Go tests. Applicable scenarios: When a user explicitly requests test generation for specific code, or when new Go code is submitted to the repository.
2installs
Sourcehomily707/agents
Added on
NPX Install
npx skill4agent add homily707/agents golang-testingTags
Translated version includes tags in frontmatterSKILL.md Content (Chinese)
View Translation Comparison →Workflow
- Write test function schema: Refer to the definition in reference/schema.yaml, and strictly comply with the schema standard
- Confirm schema with users: Modify according to user requirements, and start coding only after users are satisfied
- Minimum implementation: First implement 1 happy path + 1 critical failure case to ensure repeatability.
- Deliver runnable content: Provide command and environment description, users execute manually and return results.
go test ... - Expand test cases: Boundary → Exception → Concurrency/→ Regression (must be supplemented when fixing bugs).
-race - Finalization: Update Schema (reasons for implemented/unimplemented items) + final operation instructions (including optional parameters).
Best Practice
Principles
- Test behavior, not implementation: Prioritize verification of externally observable behaviors (return values/errors/side effects/external interactions), avoid binding internal details and increasing refactoring costs
- Repeatable, stable, maintainable: Tests should produce consistent results on any machine at any time; failure information can quickly locate problems
- Layered testing: Use unit tests to ensure correct logic and high coverage (fast, stable, isolated dependencies), use integration tests to ensure correct key links (real/near-real dependencies, reduce reliance on mock), use e2e tests to ensure core user paths are available (isolated environment, small and precise, strong observability)
- Isolate external dependencies when mocking: For boundary dependencies such as DB/HTTP/time/random/file system, control the test environment through interfaces and dependency injection
Structure
- Group by topic/behavior, one Function per group
- Within each function group, use t.Run subtests to separate different assertion structures
- Use the same assertion structure within each subtest, and use table-driven to cover more inputs and boundary cases
- Adopt AAA (Arrange-Act-Assert) or Given-When-Then structure for segmentation to reduce reading cost
- Avoid "serial if/complex logic" in tests, extract complex preparation processes into helper functions
Techniques
- For auxiliary functions, mark , and adopt FailFast strategy for errors
t.Helper() - Use golden file for complex output judgment, and provide an update switch
- mock
- When mocking an interface, use pure func to implement the interface, which can more conveniently mock different results
- When mocking a particularly complex interface, use struct embedding of the interface, and then override the method to be mocked
- Verify key interactions when mocking
Must Do
- Add race flag when concurrency is involved
- Cover error paths and critical boundaries
- Tests must be repeatable
- Test failure information must be clear, easy to understand and locate
Don't
- Do not directly test private functions (test through public API)
- Do not use in tests: Replace with channel, conditional wait, controllable clock (Clock/now injection).
time.Sleep() - Avoid flaky tests