-
Notifications
You must be signed in to change notification settings - Fork 127
/
testing.slide
866 lines (454 loc) · 19 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
Testing in Go language
Golang Testing Frameworks
02 Nov 2019
Tags: golang, go, testing
Pavel Tišnovský
Red Hat, Inc.
https://github.com/RedHatOfficial/GoCourse
@RedHat
* Sources
- [[https://github.com/RedHatOfficial/GoCourse]]
.image ./common/qr_address.png
* 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
- Expect-like frameworks and libraries
- REST API testing
- Performance analysis
* Test pyramid and ice-cream cone
[[https://watirmelon.files.wordpress.com/2018/02/automated-testing-pyramid-with-eye.jpg?w=840]]
[[https://watirmelon.files.wordpress.com/2018/02/software-testing-icecream-cone-antipattern.jpg?w=840]]
* Unit testing
- Standard `testing` package
- Helper tool `go-carpet`
- Extensions (`testify`)
- Alternative package `oglematchers`
- Combination of `ogletest` and `oglematchers`
- Package `assertions`
- Package `goblin`
* Standard "testing" package
- Based on [[https://pkg.go.dev/testing]] package
- Sources containing tests need to end with `_test.go`
- Each test needs to be stored in a function named `func` `TestXxx(*testing.T)`
- Basic methods for `testing.T` structure (data type)
* Standard "testing" package
- There are no "assertions" in the `testing` package!
- `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 as many files, as necessary
- So called table-driven tests
- Usually array/slice of structs with inputs and expected output is used
* How to start unit tests
- Basic command: `go` `test`
- Usually: `go` `test` `./...`
- It is possible to specify regexp to select just subset of tests
* Simplest example
.code testing/testing/tests01/add.go
.code testing/testing/tests01/add_test.go
* It is possible to test local functions/methods
.code testing/testing/tests02/add.go
.code testing/testing/tests02/add_test.go
* Tests in its own package
- this is preferred way
- need proper project setup
.code testing/testing/tests01B/go.mod
* Tests in its own package
.code testing/testing/tests01B/add.go
.code testing/testing/tests01B/add_test.go
* Local functions and tests in its own package
- local functions are not visible by default
- need global symbols pointing to local one
* Local functions and tests in its own package
.code testing/testing/tests02B/add.go
.code testing/testing/tests02B/add_test.go
* Local functions and tests in its own package
- helper file
.code testing/testing/tests02B/export_test.go
* Test failures (due to an error in source code)
.code testing/testing/tests03/add.go
.code testing/testing/tests03/add_test.go
* Tests can be stopped at any time
.code testing/testing/tests04/add_test.go
* Table-driven tests
.code testing/testing/tests05/add_test.go /^package main/,/^func TestAdd/
* Table-driven tests (cont.)
.code testing/testing/tests05/add_test.go /^func TestAdd/,/^}/
* Refactoring
.code testing/testing/tests07/add_test.go /^package main/,/^func TestAdd/
* Refactoring (cont.)
.code testing/testing/tests07/add_test.go /^func TestAdd/,/^}/
* Normal/fast/slow tests
- Normal tests
.code testing/testing/tests08/add_test.go
* Normal/fast/slow tests
- Fast tests
.code testing/testing/tests08/add_fast_test.go /^package main/,/^func checkAdd/
* Normal/fast/slow tests
- Fast tests (cont.)
.code testing/testing/tests08/add_fast_test.go /^func checkAdd/,/^}/
* Normal/fast/slow tests
- Fast tests (cont.)
.code testing/testing/tests08/add_fast_test.go /^func TestAddBasic/,/^}/
.code testing/testing/tests08/add_fast_test.go /^func TestAddNegative/,/^}/
* Normal/fast/slow tests
- Slow tests
.code testing/testing/tests08/add_slow_test.go /^package main/,/^func checkAdd/
* Normal/fast/slow tests
- Slow tests (cont.)
.code testing/testing/tests08/add_slow_test.go /^func checkAdd/,/^}/
* Normal/fast/slow tests
- Slow tests (cont.)
.code testing/testing/tests08/add_slow_test.go /^func TestAddMinValue/,/^}/
.code testing/testing/tests08/add_slow_test.go /^func TestAddMaxValue/,/^}/
* Normal/fast/slow tests
- Slow tests (cont.)
.code testing/testing/tests08/add_slow_test.go /^func TestAddMinMaxValue/,/^}/
* Code coverage
- Basic code coverage report can be displayed by using just standard tools
$ go test -v -cover
=== RUN TestAdd
--- PASS: TestAdd (0.00s)
PASS
coverage: 50.0% of statements
ok _/home/tester/tests01 0.004s
* Code coverage as HTML page
- It is possible to export code coverage report as HTML page (with colors, selection etc. etc.)
go test -coverprofile coverage
go tool cover -html coverage
* Code coverage as HTML page
.image ./testing/coverage.png _ 600
* Helper tool `go-carpet`
- Show test coverage in terminal for Go source files
- With -256colors shows coverage in various shades of green
- Installation: `go get -u github.com/msoap/go-carpet`
- (`~/go/bin` should be put into `PATH`)
- Run `go-carpet` in project directory
* Helper tool `go-carpet`
.image ./testing/go-carpet.png _ 600
* Extension for the standard `testify` package
- package `require`
- package `mock`
- package `suite`
* Package `require`
- Provides same global functions as the `assert` package
- But instead of returning a boolean result they terminate current test
- Tests written using this package are similar to unit tests in Python for example
* 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`
* Usage of package `oglematchers` - tested function
.code ./testing/oglematchers/oglematchers1/factorial.go
* First test written using `oglematchers` (1/7)
.code ./testing/oglematchers/oglematchers1/factorial_test.go /^package/,/^func TestFactorialForZero/
* First test written using `oglematchers` (2/7)
.code ./testing/oglematchers/oglematchers1/factorial_test.go /^func TestFactorialForZero/,/^func TestFactorialForOne/
* First test written using `oglematchers` (3/7)
.code ./testing/oglematchers/oglematchers1/factorial_test.go /^func TestFactorialForOne/,/^func TestFactorialForSmallNumber/
* First test written using `oglematchers` (4/7)
.code ./testing/oglematchers/oglematchers1/factorial_test.go /^func TestFactorialForSmallNumber/,/^func TestFactorialForSmallNumberNegative/
* First test written using `oglematchers` (5/7)
.code ./testing/oglematchers/oglematchers1/factorial_test.go /^func TestFactorialForSmallNumberNegative/,/^func TestFactorialForTen/
* First test written using `oglematchers` (6/7)
.code ./testing/oglematchers/oglematchers1/factorial_test.go /^func TestFactorialForTen/,/^func TestFactorialForBigNumber/
* First test written using `oglematchers` (7/7)
.code ./testing/oglematchers/oglematchers1/factorial_test.go /^func TestFactorialForBigNumber/,/^func TestFactorialForEvenBiggerNumber/
* Second test written using `oglematchers` (1/7)
.code ./testing/oglematchers/oglematchers2/factorial_test.go /^package/,/^func TestFactorialForZero/
* Second test written using `oglematchers` (2/7)
.code ./testing/oglematchers/oglematchers2/factorial_test.go /^func TestFactorialForZero/,/^func TestFactorialForOne/
* Second test written using `oglematchers` (3/7)
.code ./testing/oglematchers/oglematchers2/factorial_test.go /^func TestFactorialForOne/,/^func TestFactorialForSmallNumber/
* Second test written using `oglematchers` (4/7)
.code ./testing/oglematchers/oglematchers2/factorial_test.go /^func TestFactorialForSmallNumber/,/^func TestFactorialForSmallNumberNegative/
* Second test written using `oglematchers` (5/7)
.code ./testing/oglematchers/oglematchers2/factorial_test.go /^func TestFactorialForSmallNumberNegative/,/^func TestFactorialForTen/
* Second test written using `oglematchers` (6/7)
.code ./testing/oglematchers/oglematchers2/factorial_test.go /^func TestFactorialForTen/,/^func TestFactorialForBigNumber/
* Second test written using `oglematchers` (7/7)
.code ./testing/oglematchers/oglematchers2/factorial_test.go /^func TestFactorialForBigNumber/,/^func TestFactorialForEvenBiggerNumber/
* Combination of `ogletest` and `oglematchers`
* Package `assertions`
* Package `goblin`
.image ./testing/goblin.png _ 600
Author: https://github.com/leandroreox
* Package `goblin`
- Based on Mocha library from the JavaScript/TypeScript world
- 'It' and 'Describe' clausules
- Assertion functions are built-in
* Stub for test based on Goblin
.code ./testing/goblin/01_intro_test.go
* Checking behaviour of + operator
.code ./testing/goblin/02_passing_test.go
* More 'It' blocks in one 'Describe' block
.code ./testing/goblin/03_intro_test.go
* Example of all test steps that are passing
.code ./testing/goblin/04_all_passing_test.go
* Nested 'Describe' blocks in one test function
.code ./testing/goblin/06_nested_describe_test.go
* Tests split into more functions
.code ./testing/goblin/07_nested_struct_test.go
* Behaviour of Goblin when some test fails
.code ./testing/goblin/09_nested_struct_failed_test.go
* Behaviour of Goblin when some test fails
.code ./testing/goblin/10_split_failed_test.go
* Usage of method Goblin.Before
.code ./testing/goblin/11_before_test.go
* Usage of method Goblin.Before
.code ./testing/goblin/12_before_test.go
#############################################################
* 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
- Supports web UI
- Displays test coverage
- Test code generator is included as well
* GoConvey
- `Convey`
- `So`
- `ShouldEqual`
- `ShouldNotEqual`
- `ShouldBeBetween`
- `ShouldBeGreaterThan`
- `ShouldNotBeEmpty`
- ...
- [[https://github.com/smartystreets/goconvey/wiki/Assertions]]
- Possible to use: Convey("This isn't yet implemented", nil)
* GoConvey output
- `convey.Print`
- `convey.Printf`
- `convey.Println`
* GoConvey - tested code
.code testing/go-convey/go_convey1/factorial.go
* GoConvey - behaviour test
.code testing/go-convey/go_convey1/behaviour_test.go
* GoConvey - checking behaviour of slice
.code testing/go-convey/go_convey2/slice_test.go
* GoConvey - more checks for factorial implementation
.code testing/go-convey/go_convey3/behaviour_test.go
* 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.|
#############################################################
* Expect-like framework and libraries
- Originally an extension to the TCL scripting language
- Written by Don Libes for Unix, then ported to other operation systems
- Automates interactions with programs that expose a text terminal interface
- Also used to test such applications
- Examples: Telnet, FTP, passwd, fsck, rlogin, tip, SSH
- Used heavily in telco
* Expect-like framework and libraries
- Package goexpect [[https://github.com/google/goexpect]]
- Package go-expect [[https://github.com/Netflix/go-expect]]
- Package gexpect [[https://github.com/ThomasRooney/gexpect]]
* Package go-expect
- One of expect-like golang library to automate control of terminal or console based programs
- Unlike expect it does not spawn or manage process lifecycle
- Only focuses on expecting output and sending input through it's pseudoterminal
- Can be used in cooperation with the standard `testing` module
* Package gexpect
- Pure golang expect-like module.
- (IMHO) easiest to use
- Regexp support in 'expect' commands
- Two goroutines for `stdout` and `stdin` to make some use cases simpler
- Can be used in cooperation with the standard `testing` module
* Package goexpect
- Another expect-like golang library
- Native SSH spawner is included
- Generic spawner that can be reused by additional spawners
- Support for batch of commands (Batcher)
- Support for conditionals in brach (Caser)
- Go channel with exit status of automated/tested application
- Verbose mode (for debugging/testing etc.)
- Can be used in cooperation with the standard `testing` module
* Examples
- Bunch of examples for each expect-like library
* go-expect
* gexpect
* goexpect
#############################################################
* 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
.image ./testing/frisby.png _ 600
(MIT License)
* 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
* Checking headers in the server response (negative test)
.play testing/frisby/06_frisby_check_headers.go
* Using HTTP method POST
.play testing/frisby/07_frisby_post.go
* Sending JSON over HTTP/HTTPS
.play testing/frisby/09_frisby_post_json.go
* Checking content of HTTP response
.play testing/frisby/10_frisby_check_content.go
* Basic cookies handling
.play testing/frisby/11_frisby_cookies.go
* Method threading / Fluent API
.play testing/frisby/12_frisby_threading.go
* Using Frisby package in unit tests (passing tests)
- (can not be run directly from slides!)
.code testing/frisby/13_frisby_as_test_pass_test.go
* Using Frisby package in unit tests (failing tests)
- (can not be run directly from slides!)
.code testing/frisby/14_frisby_as_test_fail_test.go
* Using Frisby package in unit tests (combination)
- (can not be run directly from slides!)
.code testing/frisby/15_frisby_more_test.go
* Some tips and tricks for Frisby
#############################################################
* 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/]]
Go(lang) Unit Testing for Absolute Beginners
[[https://jonathanmh.com/p/golang-unit-testing-for-absolute-beginners/]]
Testing in Go by example: Part 1
https://smartystreets.com/blog/2015/02/go-testing-part-1-vanillla
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.)
Code Coverage Tutorial: Branch, Statement, Decision, FSM
[[https://www.guru99.com/code-coverage.html]]
Exploring the landscape of Go testing frameworks
[[https://bmuschko.com/blog/go-testing-frameworks/]]
Go coverage service
[[https://gocover.io/]]
GoConvey
[[http://goconvey.co/]]
GoConvey assertions
[[https://github.com/smartystreets/goconvey/wiki/Assertions]]
GoConvey FAQ
[[https://github.com/smartystreets/goconvey/wiki/FAQ]]
* Useful links (cont.)
Test-driven development
[[https://en.wikipedia.org/wiki/Test-driven_development]]
TheThreeRulesOfTdd
[[http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd]]
The Difference Between TDD and BDD
[[https://joshldavis.com/2013/05/27/difference-between-tdd-and-bdd/]]
The difference between TDD and BDD
[[https://www.hindsightsoftware.com/blog/tdd-vs-bdd/]]
Package goexpect
[[https://github.com/google/goexpect]]
Package go-expect
[[https://github.com/Netflix/go-expect]]
Package gexpect
[[https://github.com/ThomasRooney/gexpect]]
#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