Go test
Go have a built-in test instructions go test
and testing
package, but the joint is given a minimum complete test experience, the standard tool chain contains performance testing and test-based statements.
This time I intend to use Goland
the IDE to unit test
Characteristics of unit tests
- The first and only argument must be
t *testing.T
- Function names
Test
begin with, followed by a capital letter at the beginning of a word or phrase. - President usual method being tested so
TestValidateClient
- Call
t.Error
ort.Fail
to indicate an error (example callt.Errorf
to provide more details) t.Log
It can be used to provide debugging information without fail- Test code must be maintained at a named
something_test.go
file, for example:addition_test.go
Although I wrote this, in fact, it should be placed in the same directory, for example, the test alarm_utils.go
should be writtenalarm_utils_test.go
start testing
Test file code like the following
package main
import "testing"
func TestSum(t *testing.T) {
tables := []struct {
x int
y int
n int
}{
{1, 1, 2},
{1, 2, 3},
{2, 2, 4},
{5, 2, 7},
}
for _, table := range tables {
total := Sum(table.x, table.y)
if total != table.n {
t.Errorf("Sum of (%d+%d) was incorrect, got: %d, want: %d.", table.x, table.y, total, table.n)
}
}
}
Add a Configurations
follows
Of course, you can also choose a folder, I have here a file is selected, choose the folder he will automatically find the line with the rules of the test file.
Then Run it wants to, can also Debug