-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.slide
137 lines (91 loc) · 2.89 KB
/
testing.slide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
Testing in Go
# Libraries and best practices
5 Apr 2018
Tags: go, testing, unit-testing, integration-testing
Iaroslav Ciupin
Go Developer, Ellation
iaroslav.ciupin@ellation.com
http://ellation.com
* Agenda
- Standard library
- testify
- Testing web services
- gock
- go-sqlmock
- Integration tests
* Testing using standard library
func_test.go
.code -numbers code/func_test.go
go test ./...
go test ./<package_name> -run="Test.*"
: test files names .*_test.go + located in same package
: test cases are considered functions func Test.*
: run tests: all, for package, concrete functions
* github.com/stretchr/testify
github.com/stretchr/testify/assert
assert.Equal(t, 123, 123, "they should be equal")
assert.NoError(t, err, "no error expected")
assert.Contains(t, "ABC", "A", "string should contain A")
assert.Len(t, []int{1, 2}, 2, "slice should have length 2")
assert.NotNil(t, obj, "should not be nil")
assert.IsType(t, MyType{}, obj, "should be of type MyType")
assert.NotPanics(t, func, "should not panic")
and other ...
: testify - library with a bunch of testing helpers
: assert - has plenty of asserting functions with optional description
github.com/stretchr/testify/require
Same as assert, but calls `t.FailNow()` (stops test immediately)
* github.com/stretchr/testify/assert
.code -numbers code/assert_test.go
: create assert/require objects for re-use
: more readable tests
* Data-driven testing
.code -numbers code/data_test.go /^func Test/,/^}/
--- FAIL: TestF (0.00s)
data_test.go:18:
Error Trace: data_test.go:18
Error: Not equal:
expected: 44
actual : 43
Test: TestF
Messages: input 42
FAIL
: No built-in or 3-rd party support for data-driven testing :(
: Actually, you don't even need it!
: Have to use good old for-loop
: Good practice to mark test case in test description
: Lesson learnt: always think about failed runs, not only success runs
* Benchmarking using standard library
.code -numbers code/bench_test.go /^func Bench/,/^}/
go test -bench=Benchmark.* -benchmem -benchtime=10s
goos: darwin
goarch: amd64
pkg: github.com/iaroslav-ciupin/testing-in-go/code
BenchmarkAnswer-8 200000000 76.9 ns/op 352 B/op 1 allocs/op
PASS
ok github.com/iaroslav-ciupin/testing-in-go/code 23.039s
* Coverage?
TODO
* github.com/stretchr/testify/mock
TODO
: mocking dependencies
: setting/asserting expectations
* github.com/stretchr/testify/suite
TODO
: structuring more complex testing logic
: beforeAll, beforeEvery, afterAll, afterEvery
: built-in assertions
* Simple web server
.image testing-in-go.png
* httptest request and server
TODO
* github.com/h2non/gock
TODO
- easy HTTP request/response mocking
* github.com/DATA-DOG/go-sqlmock
: NOT AN IN-MEMORY DATABASE!
: matches SQL regex
: network/database error testing
* integration testing
TODO
No library/framework :(