www

My personal website(s)
Log | Files | Refs

2018-07-28-gotest-tools-intro.html (5457B)


      1 <!DOCTYPE html>
      2 <html lang="en">
      3 <head>
      4 <!-- Sep 03, 2024 -->
      5 <meta charset="utf-8" />
      6 <meta name="viewport" content="width=device-width, initial-scale=1" />
      7 <title>Golang testing — gotest.tools introduction</title>
      8 <meta name="author" content="Vincent Demeester" />
      9 <meta name="generator" content="Org Mode" />
     10 <link rel='icon' type='image/x-icon' href='/images/favicon.ico'/>
     11 <meta name='viewport' content='width=device-width, initial-scale=1'>
     12 <link rel='stylesheet' href='/css/new.css' type='text/css'/>
     13 <link rel='stylesheet' href='/css/syntax.css' type='text/css'/>
     14 <link href='/index.xml' rel='alternate' type='application/rss+xml' title='Vincent Demeester' />
     15 </head>
     16 <body>
     17 <main id="content" class="content">
     18 <header>
     19 <h1 class="title">Golang testing — gotest.tools introduction</h1>
     20 </header><section id="outline-container-Introduction" class="outline-2">
     21 <h2 id="Introduction">Introduction</h2>
     22 <div class="outline-text-2" id="text-Introduction">
     23 <p>
     24 I already wrote 2 previous posts about golang and testing. It&rsquo;s something I care deeply
     25 about and I wanted to continue writing about it. It took me a bit more time than I
     26 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 and I worked (but
     27 mainly Daniel 🤗) on bootstrapping a testing helper library.
     28 </p>
     29 
     30 <p>
     31 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
     32 collection of packages to augment <code>testing</code> and support common patterns. It&rsquo;s an enhanced and growing version of the
     33 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
     34 project here at <a href="https://github.com">Docker</a>.
     35 </p>
     36 
     37 <p>
     38 There is a bunch of packages that will all have their own post (linked here when available) :
     39 </p>
     40 
     41 <ul class="org-ul">
     42 <li><a href="2018-08-16-gotest-tools-assertions.html"><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>
     43 <li><code>env</code> that provides functions to test code that read environment variable or the current working directory.</li>
     44 <li><a href="2018-09-14-gotest-tools-fs.html"><code>fs</code></a> that provides tools for creating temporary files, and testing the contents and structure of a directory.</li>
     45 <li><a href="2018-09-06-gotest-tools-golden.html"><code>golden</code></a> that provides tools for comparing large multi-line strings.</li>
     46 <li><a href="2018-09-18-gotest-tools-icmd.html"><code>icmd</code></a> that executes binaries and provides convenient assertions for testing the results.</li>
     47 <li><a href="2019-03-23-gotest-tools-poll.html"><code>poll</code></a> that provides tools for testing asynchronous code.</li>
     48 <li><a href="2018-09-01-gotest-tools-skip.html"><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>
     49 </ul>
     50 
     51 <p>
     52 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>) :
     53 </p>
     54 
     55 <ul class="org-ul">
     56 <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>
     57 </ul>
     58 
     59 <p>
     60 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
     61 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>.
     62 </p>
     63 
     64 <p>
     65 <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>.
     66 </p>
     67 
     68 <div class="org-src-container">
     69 <pre class="src src-sh">$ go get -u gotest.tools/assert/cmd/gty-migrate-from-testify
     70 # […]
     71 $ go list \
     72      -f '{{.ImportPath}} {{if .XTestGoFiles}}{{"\n"}}{{.ImportPath}}_test{{end}}' \
     73      ./... | xargs gty-migrate-from-testify
     74 </pre>
     75 </div>
     76 
     77 <p>
     78 In the next post, let&rsquo;s dig into the assertion part of the library, package <code>assert</code> 👼.
     79 </p>
     80 </div>
     81 </section>
     82 </main>
     83 <footer id="postamble" class="status">
     84 <footer>
     85      <small><a href="/" rel="history">Index</a> • <a href="/sitemap.html">Sitemap</a> • <a href="https://dl.sbr.pm/">Files</a></small><br/>
     86      <small class='questions'>Questions, comments ? Please use my <a href="https://lists.sr.ht/~vdemeester/public-inbox">public inbox</a> by sending a plain-text email to <a href="mailto:~vdemeester/public-inbox@lists.sr.ht">~vdemeester/public-inbox@lists.sr.ht</a>.</small><br/>
     87      <small class='copyright'>
     88       Content and design by Vincent Demeester
     89       (<a rel='licence' href='http://creativecommons.org/licenses/by-nc-sa/3.0/'>Some rights reserved</a>)
     90     </small><br />
     91 </footer>
     92 </footer>
     93 </body>
     94 </html>