I will be following TDD approach, where I write the test first, and then write the main code to make it pass eventually
- Test file : xxx_test.go
- Test func : TestXxx
- Benchmark : BenchmarkXxx
- Example : ExampleXxx
- Custom Test runner: TestMain
- Follow the AAA rule for EACH test
- Arrange
- Act
- Assert (assert only one set of actions)
- DRY (Don't Repeat Yourself) also applies to Tests
t.Error()
=t.Log()
+t.Fail()
//t.Fail() fails the current test but continues with rest of the testst.Fatal()
=t.Log()
+t.FailNow()
// t.Fatal() and t.FailNow() fails & stops ALL the tests
go test
//for normal testing in the current dirgo test -v
//for verbose outputgo test <package_name>
// to test the packagego test -run <func_name>
//to run tests only on a specific function instead of the whole filego test -timeout <duration>
//times out & panics the test if not finished in the specified timeout i.e. 2s, 1m, etc.go test -v -short
//Skip some long running tests (Goes withif testing.Short(){t.Skip("Skipping")}
in the Test func)
go test -cover
//to see test coveragego test -coverprofile=coverage.out; go tool cover -html=coverage.out
//this will open a html in browser with showing which lines of code are covered in green (and not covered with red) (sample image below)
go test -bench .
//run benchmark in the current dirgo test -v -bench .
//verbose