Built-in Testing in Go Is More Than Just Passable

A presentation at KCDC 2021 in September 2021 in Kansas City, MO, USA by Scott McAllister

Slide 1

Slide 1

KCDC — SEPT 17, 2021 BUILT-IN TESTING IN GO IS MORE THAN JUST PASSABLE Scott McAllister PagerDuty @stmcallister

Slide 2

Slide 2

Slide 3

Slide 3

Slide 4

Slide 4

go-pagerduty library @stmcallister

Slide 5

Slide 5

Slide 6

Slide 6

Slide 7

Slide 7

Slide 8

Slide 8

The Go programming language is an open source project to make programmers more productive.

Slide 9

Slide 9

Really, Really Brief Introduction to Go ● Released by Google in 2009 ● Designed to help solve Google’s problems ● Main Characteristics ○ Open-Source ○ Statically Typed ○ Concurrency Support ○ Powerful Standard Library ○ Built-in Testing ○ Garbage Collection

Slide 10

Slide 10

Testing Evaluate and verify that the code does what is expected @stmcallister

Slide 11

Slide 11

Unit Testing Testing the smallest piece of code that can be logically isolated @stmcallister

Slide 12

Slide 12

Integration Testing Testing combined modules as a group @stmcallister

Slide 13

Slide 13

Testing in Go @stmcallister

Slide 14

Slide 14

TESTING IN GO Test code typically lives in the same package as the code it tests @stmcallister

Slide 15

Slide 15

TESTING IN GO Test code typically lives in the same package as the code it tests Name of test files end in *_test.go @stmcallister

Slide 16

Slide 16

TEST FUNCTIONS You can define methods on any type: func TestHelloEmpty(t *testing.T) { msg, err := Hello(“”) if msg != “” || err == nil { t.Fatalf(Hello("") = %q, %v, want "", error, msg, err) } } @stmcallister Function names begin with ‘Test’ Functions receive pointer to testing.T

Slide 17

Slide 17

Running Go Tests go test @stmcallister

Slide 18

Slide 18

Subtests @stmcallister

Slide 19

Slide 19

Additional Resources go-test-demos https://github.com/stmcallister/go-test-demos go-pagerduty https://github.com/PagerDuty/go-pagerduty @stmcallister

Slide 20

Slide 20