forked from RedHatOfficial/GoCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.slide
316 lines (186 loc) · 6.87 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
Go language course
Golang Testing Frameworks
02 Nov 2019
Tags: golang, go, testing
Pavel Tišnovský
Red Hat, Inc.
https://github.com/RedHatOfficial/GoCourse
@RedHat
* Gophers
#The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/)
#Source https://golang.org/doc/gopher/fiveyears.jpg
#The design and this image is licensed under the Creative Commons 3.0 Attributions license.
.image ./common/fiveyears.jpg _ 900
* Golang Testing Frameworks
- Unit testing
- BDD testing
- REST API testing
- Performance analysis
* Unit testing
- Standard `testing` package
- Helper tool `go-carpet`
- Extensions (`testify`)
- Alternative package `oglematchers`
- Combination of `ogletest` and `oglematchers`
- Package `assertions`
* Standard `testing` package
- Each test needs to be stored in a function `func` `TestXxx(*testing.T)`
- Basic Methods for `testing.T` structure (data type)
- `Log(args` `...interface{})`
- `Skip(args` `...interface{})`
- `Error(args` `...interface{})`
- `Fatal(args` `...interface{})`
- `Fail()`
- `FailNow()`
* Standard `testing` package
- Formatting output
- `Logf(format` `string,` `args` `...interface{})`
- `Skipf(format` `string,` `args` `...interface{})`
- `Errorf(format` `string,` `args` `...interface{})`
- `Fatalf(format` `string,` `args` `...interface{})`
* Unit test files
- Usually stored in `package_test.go`
- Can be split into many files, as necessary
* How to start unit tests
- basic command: `go` `test`
- usually: `go` `test` `./...`
* Helper tool `go-carpet`
* Extension `testify` package
** Package `require`
- provides same global functions as the assert package
- but instead of returning a boolean result they terminate current test
** Package `mock`
- provides a mechanism for writing mock objects
- they can be used in place of real objects when writing test code
** Package `suite`
* Extensions (`testify`)
* Alternative package `oglematchers`
* Combination of `ogletest` and `oglematchers`
* Package `assertions`
#############################################################
* BDD testing
- GoConvey
- Godog
* BDD testing
- Test pyramid
- BDD: behavior-driven development
- Simple DSL
* GoConvey
- Test steps are written directly as Go source code
- Pros: support in IDE, most issues can be detected by the compiler
- Cons: no proper separation from Go language
* Godog
- Based on Gherkin language
- Pros: behaviour tests are separated from the code
- Cons: "glue" between test scenarios and test step implementations
* Gherkin language
- Feature/Scenario/Scenario Outline
- Given/when/then
- Variable parameters in tests
- Tables as data source
- Tables for multiple test runs with variable parameters
* Gherkin language
- Based on natural language + a few keywords
- usually English is used
- translated into other languages as well
- It is quite similar to Python, AsciiDoc etc.
- indentation
- based on keywords, not on special characters
- tables "drawn" in ASCII
- It is not tightly bound with any real programming language
- it can be used by non-developers
* Given-When-Then
- Semi-structured way to write down test cases
* Three clauses
- `Given`
- `When`
- `Then`
* The same clause on more consecutive lines?
- `And`
* Gherkin language - an example
Given the customer has logged into their current account
And the balance is shown to be 100 euros
When the customer transfers 75 euros to their savings account
Then the new current account balance should be 25 euros
* Test scenario parts
- Keywords/clauses:
- `Given`, `And`, `When`, `Then`
- The rest is written in "plain English":
- Or in other supported language (ANY language in case of Godog)
- Contains variable parts as well
- `100`, `75`, `25`, `test`
* Tables
- has two purposes in Gherkin language
- specify list of values used later in tests
- specify multiple test scenarios with the same sentences, but with different parameters/variables.
* Scenario outlines
Scenario Outline: Check the user search feature, perform the search for more users
Given GitHub is accessible
When I search for user with nick <nick>
Then I should receive 200 status code
And I should receive proper JSON response
And I should find the user with full name <fullname>
And I should find that the user works for company <company>
Examples: users
|nick|fullname|company|
|torvalds|Linus Torvalds|Linux Foundation|
|brammool|Bram Moolenaar|Zimbu Labs|
|tisnik|Pavel Tišnovský|Red Hat, Inc.|
#############################################################
* REST API testing
- in many aspects like software testing at UI level
- instead of input-output testing, REST API is validated
- very important in niche where Go language is used
- supports HTTP and HTTPS as well
* REST API testing frameworks
- several packages/frameworks are available
- Frisby
- RestIt
* Frisby
- All basic HTTP methods (GET, POST, PUT, PATCH, DELETE)
- Built-in assertions
- JSON parser
- XML parser
- Based of 'fluent API'
- The whole test can be written as a chain of method of `*Frisby` object
- Can be used as-is or as part of unit tests (usually not recommended)
* Checking REST API with just GET method available
.play testing/frisby/02_frisby_basic_usage.go
* The same example, but with fluent API
.play testing/frisby/01_frisby_basic_usage.go
* Reporting similar to unit tests output
.play testing/frisby/03_frisby_basic_usage.go
* Behaviour when some tests fail
.play testing/frisby/04_frisby_failures.go
* Checking headers in the server response
.play testing/frisby/05_frisby_check_headers.go
* Using HTTP method POST
* Sending JSON over HTTP/HTTPS
* Checking content of HTTP response
* Basic cookies handling
* Using Frisby package in unit tests
#############################################################
* Performance analysis
- very important part of testing
#############################################################
* Useful links
An introduction to programming in Go: Testing
[[https://www.golang-book.com/books/intro/12]]
Package testing
[[https://golang.org/pkg/testing/]]
Code Coverage Tutorial: Branch, Statement, Decision, FSM
[[https://www.guru99.com/code-coverage.html]]
Go Test Your Code: An introduction to testing in Go
[[https://medium.com/rate-engineering/go-test-your-code-an-introduction-to-effective-testing-in-go-6e4f66f2c259]]
Comparative Analysis Of GoLang Testing Frameworks
[[https://www.slideshare.net/DushyantBhalgami/comparative-analysis-of-golang-testing-frameworks]]
* Useful links (cont.)
Exploring the landscape of Go testing frameworks
[[https://bmuschko.com/blog/go-testing-frameworks/]]
#last slide
* More Gophers
#The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/)
#Source https://golang.org/doc/gopher/bumper.png
#The design and this image is licensed under the Creative Commons 3.0 Attributions license.
.image ./common/bumper.png _ 900