testing in go

47
Testing in Go TDC 2016 - São Paulo Eduardo Bonet

Upload: eduardo-felipe-ewert-bonet

Post on 20-Mar-2017

271 views

Category:

Technology


0 download

TRANSCRIPT

Testing in GoTDC 2016 - São Paulo

Eduardo Bonet

$ whoamiebonet.me

AgendaObjectives

The Testing package

Table Testing

Testing HTTP

Other Testing Packages

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

Plugin support for tests

Atom (go-plus) Vim (vim-go) IntelliJ

Running full suites

Run specific test

Code Coverage

Others Keybindings for quick switch

Vim-goCoverage

Test Resultsvim-go

CoverageIntelliJ

Test ResultsIntelliJ

Example: Simplified Blackjack

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)

Table TestingUsing anonymous structs to represent test cases

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 - BlackJackhttps://golang.org/pkg/net/http/httptest/

Utililty package to test http.

Provides the struct *ResponseRecorder, that implements the interface *ResponseWriter

Other Packages

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

Ginkgo - Results

GoConveyhttp://goconvey.co/ (2334 )

BDD alike

Pretty browser interface

Reload tests on file changes

Custom DSL

goconvey Results

goconvey Failed test

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