2018-07-28-gotest-tools-intro.org (3279B)
1 #+TITLE: Golang testing — gotest.tools introduction 2 #+date: <2018-07-28 Sat> 3 #+filetags: feature go testing 4 #+setupfile: ../templates/post.org 5 6 * Introduction 7 8 I already wrote 2 previous posts about golang and testing. It's something I care deeply 9 about and I wanted to continue writing about it. It took me a bit more time than I 10 thought, but getting back to it. Since the [[http://vincent.demeester.fr/posts/2017-04-22-golang-testing-golden-file/][last post]], Daniel Nephin and I worked (but 11 mainly Daniel 🤗) on bootstrapping a testing helper library. 12 13 Let me introduce it to you this library : [[https://gotest.tools][=gotest.tools=]]. As described in the [[https://godoc.org/gotest.tools][godoc]] package comment, =gotest.tools= is a 14 collection of packages to augment =testing= and support common patterns. It's an enhanced and growing version of the 15 initial helpers we (the docker/moby maintainers) wrote initially in [[https://github.com/docker/docker][=docker/docker=]] repository. We are using in quite some 16 project here at [[https://github.com][Docker]]. 17 18 There is a bunch of packages that will all have their own post (linked here when available) : 19 20 - [[file:2018-08-16-gotest-tools-assertions.org][=assert=]] (with =assert/cmp= and =assert/opt=) that provides assertions for comparing expected values to actual values. 21 - =env= that provides functions to test code that read environment variable or the current working directory. 22 - [[file:2018-09-14-gotest-tools-fs.org][=fs=]] that provides tools for creating temporary files, and testing the contents and structure of a directory. 23 - [[file:2018-09-06-gotest-tools-golden.org][=golden=]] that provides tools for comparing large multi-line strings. 24 - [[file:2018-09-18-gotest-tools-icmd.org][=icmd=]] that executes binaries and provides convenient assertions for testing the results. 25 - [[file:2019-03-23-gotest-tools-poll.org][=poll=]] that provides tools for testing asynchronous code. 26 - [[file:2018-09-01-gotest-tools-skip.org][=skip=]] that provides functions for skipping a test and printing the source code of the condition used to skip the test. 27 28 There is also experimental package, using the =x= notation (as the golang team uses, for example with =golang.org/x/sync=) : 29 30 - =x/subtest= that provides a =TestContext= to subtests which handles cleanup and provides a =testing.TB= and =context.Context=. 31 32 There is already some good =testing= helpers in the Go ecosystem : [[https://github.com/stretchr/testify][=testify=]], [[http://labix.org/gocheck][=gocheck=]], [[https://github.com/onsi/ginkgo][=ginkgo=]] and a lot more — so 33 why create a new one ? There is multiple reason for it, most of them can be seen in the following [[https://github.com/gotestyourself/gotest.tools/issues/49#issuecomment-362436026][GitHub issue]]. 34 35 [[https://github.com/dnephin/][Daniel]] also wrote a very useful converter if your code base is currently using =testify= : =gty-migrate-from-testify=. 36 37 #+BEGIN_SRC sh 38 $ go get -u gotest.tools/assert/cmd/gty-migrate-from-testify 39 # […] 40 $ go list \ 41 -f '{{.ImportPath}} {{if .XTestGoFiles}}{{"\n"}}{{.ImportPath}}_test{{end}}' \ 42 ./... | xargs gty-migrate-from-testify 43 #+END_SRC 44 45 In the next post, let's dig into the assertion part of the library, package =assert= 👼.