Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing in go

itchy
December 02, 2014

Testing in go

Techniques for testing in go

itchy

December 02, 2014
Tweet

More Decks by itchy

Other Decks in Programming

Transcript

  1. Our App • Send appointment reminder text messages • Find

    patients with scheduled appointments 1,7 or 28 days out • Send a text message with appointment reminder
  2. Unit Testing • Any file that ends in _test.go •

    Same package • Include testing • func starts with Test
  3. Stay in the zone — Karl Seguin • The difference

    between a method that's easy to test and not is often a matter of whether you are passing interfaces (good) or concrete types (bad) • Since functions are first class types in Go, functions can also be passed around to decouple implementations
  4. SOLID — Uncle Bob • Single Responsibility • Open/Closed •

    Liskov substitution • Interface (narrow) • Dependency Inversion
  5. • http://godoc.org/github.com/qur/withmock • install Bazaar • go get github.com/qur/withmock •

    go get github.com/qur/withmock/mocktest • $> withmock go test • $> mocktest
  6. Benchmarks • funcs start with Benchmark • Pass *testing.B •

    b.N is calculated by the compiler to insure that the 
 loop runs for at least 1 second • go test -bench=.
  7. Traps — Dave Cheney • Don’t change the value of

    the func inside the for loop • Don’t pass b.N as a param • Always store results in a package level var so it is not skipped 
 by compiler optimizations
  8. More… • $> go help test • $> go help

    testflag • $> go test run=regexp
  9. Profiling Go Programs • When CPU profiling is enabled, the

    Go program stops about 100 times per second and records a sample consisting of the program counters on the currently executing goroutine's stack. • With go test lib you can use • -cpuprofile • -memprofile
  10. But not in OS X • https://code.google.com/p/go/issues/detail?id=6047
 
 Multithreaded CPU

    profiles do not work on OS X, but worse they give little indication that they are broken.
  11. Race Detector • When the -race command- line flag is

    set, the compiler instruments all memory accesses with code that records when and how the memory was accessed, while the runtime library watches for unsynchronized accesses to shared variables. When such "racy" behavior is detected, a warning is printed
  12. • http://dave.cheney.net/2013/06/09/writing-table-driven-tests-in-go • http://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go • https://github.com/davecheney/fib • http://blog.golang.org/cover • http://blog.golang.org/profiling-go-programs

    • http://justsomething.co/the-34-most-hilarious-pinterest-fails-ever/ • kexchange.com/questions/4499/could-the-crashed-mars-express-lander-be-imaged- by-the-mro-orbiter • http://uafcornerstone.net/wp-content/uploads/2012/07/Red-Green- Regatta-2011-032.jpg • http://news.3yen.com/wp-content/images/Admiral-Ackbar-Itzza-trap.jpg • http://www.poetryfoundation.org/harriet/wp-content/uploads/ 2013/03/4-1-13_Madrid.jpg • http://graphics8.nytimes.com/images/2011/10/11/sports/baseball/11sausages-pic/ 11sausages-pic-articleLarge.jpg