index.html (7218B)
1 <!DOCTYPE html> 2 3 <html lang="en"> 4 5 <head> 6 <meta charset="utf-8"> 7 <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 8 9 <link rel="start" href="https://vincent.demeester.fr" /> 10 11 <title>Vincent Demeester</title> 12 <link rel="canonical" href="https://vincent.demeester.fr/posts/2018-07-28-gotest-tools-intro/"> 13 <link href="https://vincent.demeester.fr/index.xml" rel="alternate" type="application/rss+xml" title="Vincent Demeester" /> 14 15 <link rel="openid.server" href="https://indieauth.com/openid" /> 16 <link rel="openid.delegate" href="http://vincent.demeester.fr/" /> 17 <link rel="shortcut icon" type="image/x-icon" href="favicon.ico"> 18 19 <link rel="stylesheet" href="/css/screen.css" type="text/css" /> 20 <link rel="stylesheet" href="/css/sbrain.css" type="text/css" /> 21 <link rel="stylesheet" href="/css/syntax.css" type="text/css" /> 22 23 </head> 24 25 <body lang=""/> 26 27 28 29 30 31 32 <div id="main-container"> 33 <div id="page"> 34 <article class="post"> 35 <header> 36 <h1 class="emphnext">Golang testing — gotest.tools introduction</h1><a href='https://vincent.demeester.fr/posts/2018-07-28-gotest-tools-intro/'></a> 37 <address class="signature"> 38 <span class="date">Sat, 28 July, 2018</span> 39 <span class="words">(400 Words)</span> 40 </address> 41 <ul class="tag_box inline"> 42 43 <li class="category"><a href="/categories/#developement">developement</a></li> 44 45 46 47 48 49 <li class="tag tag-testing"><a href="/tags/#testing">testing<span>11</span></a></li> 50 51 52 <li class="tag tag-golang"><a href="/tags/#golang">golang<span>12</span></a></li> 53 54 55 <li class="tag tag-featured"><a href="/tags/#featured">featured<span>2</span></a></li> 56 57 <br/> 58 59 </ul> 60 </header> 61 62 I already wrote 2 previous posts about golang and testing. It’s something I care deeply about and I wanted to continue 63 writing about it. It took me a bit more time than I thought, but getting back to it. Since the <a href="http://vincent.demeester.fr/posts/2017-04-22-golang-testing-golden-file/">last post</a>, Daniel Nephin 64 and I worked (but mainly Daniel 🤗) on bootstrapping a testing helper library. 65 66 <p>Let me introduce it to you this library : <a href="https://gotest.tools"><code>gotest.tools</code></a>. As described in the <a href="https://godoc.org/gotest.tools">godoc</a> package comment, <code>gotest.tools</code> is a 67 collection of packages to augment <code>testing</code> and support common patterns. It’s an enhanced and growing version of the 68 initial helpers we (the docker/moby maintainers) wrote initially in <a href="https://github.com/docker/docker"><code>docker/docker</code></a> repository. We are using in quite some 69 project here at <a href="https://github.com">Docker</a>.</p> 70 71 <p>There is a bunch of packages that will all have their own post (linked here when available) :</p> 72 73 <ul> 74 <li><a href="/posts/2018-08-16-gotest-tools-assertions/"><code>assert</code></a> (with <code>assert/cmp</code> and <code>assert/opt</code>) that provides assertions for comparing expected values to actual values.</li> 75 <li><code>env</code> that provides functions to test code that read environment variable or the current working directory.</li> 76 <li><a href="/posts/2018-09-14-gotest-tools-fs/"><code>fs</code></a> that provides tools for creating temporary files, and testing the contents and structure of a directory.</li> 77 <li><a href="/posts/2018-09-06-gotest-tools-golden/"><code>golden</code></a> that provides tools for comparing large multi-line strings.</li> 78 <li><a href="/posts/2018-09-18-gotest-tools-icmd/"><code>icmd</code></a> that executes binaries and provides convenient assertions for testing the results.</li> 79 <li><code>poll</code> that provides tools for testing asynchronous code.</li> 80 <li><a href="/posts/2018-09-01-gotest-tools-skip/"><code>skip</code></a> that provides functions for skipping a test and printing the source code of the condition used to skip the test.</li> 81 </ul> 82 83 <p>There is also experimental package, using the <code>x</code> notation (as the golang team uses, for example with <code>golang.org/x/sync</code>) :</p> 84 85 <ul> 86 <li><code>x/subtest</code> that provides a <code>TestContext</code> to subtests which handles cleanup and provides a <code>testing.TB</code> and <code>context.Context</code>.</li> 87 </ul> 88 89 <p>There is already some good <code>testing</code> helpers in the Go ecosystem : <a href="https://github.com/stretchr/testify"><code>testify</code></a>, <a href="http://labix.org/gocheck"><code>gocheck</code></a>, <a href="https://github.com/onsi/ginkgo"><code>ginkgo</code></a> and a lot more — so 90 why create a new one ? There is multiple reason for it, most of them can be seen in the following <a href="https://github.com/gotestyourself/gotest.tools/issues/49#issuecomment-362436026">GitHub issue</a>.</p> 91 92 <p><a href="https://github.com/dnephin/">Daniel</a> also wrote a very useful converter if your code base is currently using <code>testify</code> : <code>gty-migrate-from-testify</code>.</p> 93 <div class="highlight"><pre class="chroma"><code class="language-sh" data-lang="sh">$ go get -u gotest.tools/assert/cmd/gty-migrate-from-testify 94 <span class="c1"># […]</span> 95 $ go list <span class="se">\ 96 </span><span class="se"></span> -f <span class="s1">'{{.ImportPath}} {{if .XTestGoFiles}}{{"\n"}}{{.ImportPath}}_test{{end}}'</span> <span class="se">\ 97 </span><span class="se"></span> ./... <span class="p">|</span> xargs gty-migrate-from-testify</code></pre></div> 98 <p>In the next post, let’s dig into the assertion part of the library, package <code>assert</code> 👼.</p> 99 100 101 </article> 102 <hr /> 103 <div class="prev-next"> 104 105 <a class="paging-link prev" href="/posts/2018-08-16-gotest-tools-assertions/" title="Golang testing — gotest.tools assertions">← Previous post</a> 106 107 108 109 <a class="paging-link next" href="/posts/2017-04-22-golang-testing-golden-file/" title="Golang testing — golden file">Next post →</a> 110 111 </div> 112 113 </div> 114 </div> 115 116 <footer> 117 <nav> 118 119 <a href="/">home</a> 120 <span class="text-muted"> | </span> 121 122 <a href="/about">about</a> 123 <span class="text-muted"> | </span> 124 125 <a href="/archive">archive</a> 126 <span class="text-muted"> | </span> 127 128 <a href="/categories">categories</a> 129 <span class="text-muted"> | </span> 130 131 <a href="/tags">tags</a> 132 <span class="text-muted"> | </span> 133 134 <a href="https://twitter.com/vdemeest">twitter</a> 135 <span class="text-muted"> | </span> 136 137 <a href="https://github.com/vdemeester">github</a> 138 <span class="text-muted"> | </span> 139 140 <a href="https://vincent.demeester.fr/index.xml">rss</a> 141 </nav> 142 <br/> 143 <address> 144 <span class="copyright"> 145 Content and design by Vincent Demeester 146 (<a rel="licence" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Some rights reserved</a>) 147 </span><br /> 148 <span class="engine"> 149 Powered by <a href="https://gohugo.io/">Hugo</a> and <a href="https://github.com/kaushalmodi/ox-hugo/">ox-hugo</a> 150 </span> 151 </address> 152 </footer> 153 </body> 154