go test
go test -cover
go test -coverprofile=result.out
go tool cover -html=result.out -o result.html
Do this...
- Follow TDD steps: write test, write code, improve test, improve code and iter!
- Use interfaces.
- Use Table Driven Tests (TDT).
- Use go native libraries.
- Build tests of differents packages.
- Do not change the behaviour of your tests (do not change your tests) when refactor the code.
- Do not forget integration tests.
Do not do this...
- Giving confusing information when a test fails. Solution: be explicit and give context when tests fails.
- If your code is difficult to test, your code is difficult to use. Solution: make easy and clean code.
- Not following the typical structure of a test. Solution: write setup and preconditions, then actions, follow with assertions and finally clean and tier down. Do not mix this steps!
- Prefer DRY (dont repeat yourself) to DAMP (descriptive and meaningful phrases). Solution: repeat code if you need. Make a very smart test could make it complicated to understand.
- Include error cases in Table Driven Tests. Solution: create tests cases for testing errors.
- Violate encapsulation. Solution: do not make public methods, attributes, constants, etc only cause you need it on tests. Build a simple interfaces and methods.