Tests
Tests are important. Tests check, if you application works as expected. Tests in Go are so easy, it's easier to write tests than not to do it.
Normally you will not see any logs from tests, unles you set the -v
(verbose) flag.
Tests are executed automatically when you name your files *_test.go
.
Let's dive in with an easy example:
calculator_test.go | |
---|---|
If you run go test ./...
the output is:
? example [no test files]
--- FAIL: Test_add (0.00s)
calculator_test.go:9: not correct, expected 2 got 3
FAIL
FAIL example/calculator 0.153s
FAIL
Let's fix the test:
calculator_test.go | |
---|---|
Now the tests are running properly:
Setup and Teardown
To setup a test use a function called TestMain
.
To teardown a tests files and data use t.Cleanup(func() { ... })
.
By running the tests with:
the output is:
? reader_writer [no test files]
Setting up...
Running tests...
=== RUN Test_add
Teardown add test
--- PASS: Test_add (0.00s)
PASS
Clean up all tests...
ok reader_writer/calculator 0.152s
Public API Testing
If you want to test your public exposed functions or methods you can create a "*_test" package. This will, by definition, create a new package, which can only access exported functions and methods.
Let's check that:
calculator/calculator_test/calculator_test.go | |
---|---|
Testify
In a lot of projects you will see the package github.com/stretchr/testify/assert
.
This is because it brings a more "convenient" way of testing with assertions, mocking, and suite.
Let's check some examples: