Instruction file imported from kopia/kopia (
.github/instructions/go-testing.instructions.md). Copyright stays with the author.
Testing
Test Organization
- Name test files with
_test.gosuffix - Keep tests in the same package for white-box testing and use
_testpackage name suffix for black-box testing - Place test files next to the code they test
Writing Tests
- Use table-driven tests for multiple test cases
- Name tests descriptively using
Test_functionName_scenario - Use subtests with
t.Runfor better organization - Test both success and error cases
- Have separate top-level tests for the success and error cases
- Use
stretchr/testify/requirepackage for checking expected results
Test Helpers
- Mark helper functions with
t.Helper() - Create test fixtures for complex setup
- Use
testing.TBinterface for functions used in tests and benchmarks - Clean up resources using
t.Cleanup()