testing in go
TRANSCRIPT
Objectives
Present the basis for efficient test in Go focusing on the standard library, but with a quick glance at some alternatives.
Why Testing?
Improves focus while developing
Avoids breaking old cold
Verify if machines are working properly
Documentation
…….
go test
go test // testing the local package
go test some/pkg // testing a specific package
go test -v some/pkg -run ^TestSum$ // runs the specified tests
go test --cover // Code coverage
https://golang.org/src/cmd/go/test.go
Testing in GoTests are written on files ending with "_test.go"
A Test is a function that starts with Test* and has only the parameter *testing.T
Plugin support for tests
Atom (go-plus) Vim (vim-go) IntelliJ
Running full suites
Run specific test
Code Coverage
Others Keybindings for quick switch
Simplified BlackJackUses a deck.
Cards from 2 to 9 are worth their face value
Figures worth 10 points
Ace is worth 11 points
Ace + Figure = Blackjack
If over 21, the hand is busted.
Implementing testing - Score Computation
A Hand is represented by a string, where each char represents a card ("AJ" is a blackjack)
[master] blackjack_test.go
[master] blackjack_test.go
REPETITION
[master] blackjack_test.go
[master] blackjack_test.go
[master] blackjack_test.go
[master] blackjack_test.go
[master] blackjack_test.go
Auxiliary FunctionsTesting Floats
[master] main_test.go
Auxiliary FunctionsTesting Floats
[master] main_test.goPRECISÃO N
UMÉRICA
Auxiliary FunctionsTesting Floats
[master] main_test.go
Auxiliary Functions
valor, existe := mapa["key"]
[master] main_test.go
Testing HTTP - BlackJackLet's create a simple BlackJack API that computes the hand result:
GET /blackjack
● Parameters○ hand [string] : the hand
● Returns○ [422], if 'hand' is not found or invalid○ [200, result] if 'hand' is valid
Testing HTTP - BlackJack
[master] blackjack.go
Testing HTTP - BlackJackhttps://golang.org/pkg/net/http/httptest/
Utililty package to test http.
Provides the struct *ResponseRecorder, that implements the interface *ResponseWriter
Testing HTTP - BlackJack
[master] blackjack_test.go
Testando HTTP - BlackJack
[master] blackjack_test.go
Ginkgohttp://onsi.github.io/ginkgo/ (1075 )
BDD
Works with go test, but has it's own structure
Uses a custom lib for assertion (Gomega)
Rerun testes on change
GoConveyhttp://goconvey.co/ (2334 )
BDD alike
Pretty browser interface
Reload tests on file changes
Custom DSL
Testifyhttps://github.com/stretchr/testify (2025 )
Run within go test
Additional testing functionality, mostly shortcuts
Mocking
ConclusionTesting in go is very easy
For microservices, the standard library is more than sufficient
For larger apps, GoConvey or Ginkgo can speed up development process and documentation, at the cost of losing integration with IDEs
Thank you!
Slides at ebonet.me/talksCode at github.com/ebonet/gotesting