Histogram與Summaries都適合拿來observe request duration or response size. 使用histogram and summary時, 除了有_count與_sum的matrics以外, 還會有buckets or quantile 可以拿來計算quantiles
go test set pwd as package directory use relative path “test-fixtures” directory as a place to store test data(loading config, model data, binary data …etc)
1
2
3
4
funcTestAdd(t *testing.T) {
data := filepath.Join("test-fixtures", "add_data.json")
// Do something with data
}
Golden files(Test flag)
expected output放在.golden file中 當update flag為true時, 代表需要更新expected output file Very useful to test complex structures
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var update = flag.Bool("update", false, "update golden files")
funcTestAdd(t *testing.T) {
// ... table (probably!)
for _, tc := range tcs {
actual := doSomething(tc)
golden := filepath.Join("test-fixtures", tc.Name+".golden")
if *update {
ioUtil.WriteFile(golden, actual, 0644)
}
expected, _ := ioutil.ReadFile(golden)
if !bytes.Equal(actual, expected) {
//FAIL!
}
}
}
1
2
go test
go test -update
Global State
Do not use global state. If you have to do, Use default, and let test can easily mock it.
1
2
3
4
5
const defaultPort = 1000
type ServerOpts {
Port int// default it to defaultPort somewhere
}
Test Helpers
TestTempFile
Test helper接受testing.T參數, helper中的錯誤直接在helper中處理掉 helper return a function to clean