home

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

index.html (8863B)


      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-09-06-gotest-tools-golden/">
     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 golden</h1><a href='https://vincent.demeester.fr/posts/2018-09-06-gotest-tools-golden/'></a>
     37         <address class="signature">
     38           <span class="date">Thu, 6 September, 2018</span>
     39           <span class="words">(500 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 	  <br/>
     55 	  
     56 	</ul>
     57       </header>
     58       
     59       
     60       
     61       
     62 
     63 <p>Let&rsquo;s continue the <a href="https://gotest.tools"><code>gotest.tools</code></a> serie, this time with the <code>golden</code> package. This is a
     64 <a href="/posts/2017-04-22-golang-testing-golden-file/"><em>quick follow-up</em> on a previous <code>golden</code> post</a>, but focused on the <code>gotest.tools</code>
     65 implementation. I&rsquo;m gonna be quicker, please read that one if <code>golden</code> files is a new
     66 concept for you.</p>
     67 
     68 <blockquote>
     69 <p>Package <code>golden</code> provides tools for comparing large mutli-line strings.</p>
     70 
     71 <p>Golden files are files in the <code>./testdata/</code> sub-directory of the package under test.</p>
     72 </blockquote>
     73 
     74 <p>In the previous article, we described the problem, and how to fix it by writing a small
     75 helper. Well, that small helper is in <code>gotest.tools/golden</code> now, and it has a tiny bit
     76 more features.</p>
     77 
     78 <p>One of the difference between the <code>gotest.tools</code> implementation and the previous post is
     79 the flag name. In <code>gotest.tools/golden</code>, the flag is <code>-test.update-golden</code> (was just
     80 <code>-test.update</code> before). Just as before, if the <code>-test.update-golden</code> flag is set then the
     81 actual content is written to the golden file, before reading it and comparing.</p>
     82 
     83 <p>There is two ways to use the <code>golden</code> package:</p>
     84 
     85 <ul>
     86 <li>on it&rsquo;s own, using <code>golden.Assert</code> or <code>golden.AssertBytes</code></li>
     87 <li>as a <code>cmp.Comparison</code>, with <code>golden.String</code> or <code>golden.Bytes</code></li>
     88 </ul>
     89 
     90 <h2 id="assert-and-assertbytes"><code>Assert</code> and <code>AssertBytes</code></h2>
     91 
     92 <p>Using <code>Assert</code> functions should be straightforward. Both <code>Assert</code> function compares the
     93 actual content to the expected content in the golden file and returns whether the
     94 assertion was successful (true) or not (false).</p>
     95 
     96 <ul>
     97 <li><code>Assert</code> uses string. Note that this one <strong>removes carriage return</strong> before comparing to
     98 depend as less as possible of the system (<code>\n</code> vs <code>\r\n</code> 😅)</li>
     99 <li><code>AssertBytes</code> uses raw data (in the form of <code>[]byte</code>)</li>
    100 </ul>
    101 <div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="nx">golden</span><span class="p">.</span><span class="nf">Assert</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="s">&#34;foo&#34;</span><span class="p">,</span> <span class="s">&#34;foo-content.golden&#34;</span><span class="p">)</span>
    102 <span class="c1">// Could also be used to check some binary format
    103 </span><span class="c1"></span><span class="nx">golden</span><span class="p">.</span><span class="nf">AssertBytes</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="p">[]</span><span class="nb">byte</span><span class="p">(</span><span class="s">&#34;foo&#34;</span><span class="p">),</span> <span class="s">&#34;foo-content.golden&#34;</span><span class="p">)</span></code></pre></div>
    104 <h2 id="bytes-and-string"><code>Bytes</code> and <code>String</code></h2>
    105 
    106 <p>As written in a <a href="/posts/2018-08-16-gotest-tools-assertions/">previous post (about the <code>assert</code> package)</a>, I prefer to use <code>cmp.Comparison</code>.</p>
    107 
    108 <blockquote>
    109 <p>All those helper functions have a equivalent function in the <code>cmp</code> package that returns a
    110 <code>Comparison</code>. I, personally, prefer to use <code>assert.Check</code> or <code>assert.Assert</code> in
    111 combination with <code>cmp.Comparison</code> as it allows me to write all my assertions the same way,
    112 with built-ins comparison or with my own — i.e. <code>assert.Assert(t, is.Equal(…), &quot;message&quot;</code>
    113 or <code>assert.Assert(t, stackIsUp(c, time…), &quot;another message&quot;)</code>.</p>
    114 </blockquote>
    115 
    116 <p>The <code>golden</code> package gives us that too, in the form of <code>Bytes</code> and <code>String</code>. Using the
    117 <code>assert.Check</code> or <code>assert.Assert</code> functions with those is equivalent to their <em>helper</em>
    118 counter-part <code>golden.Assert</code> and <code>golden.AssertBytes</code>.</p>
    119 <div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="nx">assert</span><span class="p">.</span><span class="nf">Assert</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="nx">golden</span><span class="p">.</span><span class="nf">String</span><span class="p">(</span><span class="s">&#34;foo&#34;</span><span class="p">,</span> <span class="s">&#34;foo-content.golden&#34;</span><span class="p">))</span>
    120 <span class="c1">// Could also be used to check some binary format
    121 </span><span class="c1"></span><span class="nx">assert</span><span class="p">.</span><span class="nf">Assert</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="nx">golden</span><span class="p">.</span><span class="nf">Bytes</span><span class="p">([]</span><span class="nb">byte</span><span class="p">(</span><span class="s">&#34;foo&#34;</span><span class="p">),</span> <span class="s">&#34;foo-content.golden&#34;</span><span class="p">))</span></code></pre></div>
    122 <h2 id="conclusion">Conclusion…</h2>
    123 
    124 <p>… that&rsquo;s a wrap. As for <a href="/posts/2018-09-01-gotest-tools-skip/"><code>skip</code></a>, this is a small package, so the post was going to be
    125 quick. <code>golden</code> package just solve a specific problem (read <a href="/posts/2017-04-22-golang-testing-golden-file/">Golang testing — golden file</a>)
    126 in a simple way.</p>
    127 
    128       
    129     </article>
    130     <hr />
    131     <div class="prev-next">
    132       
    133       <a class="paging-link prev" href="/posts/2018-09-14-gotest-tools-fs/" title="Golang testing — gotest.tools fs">← Previous post</a>
    134       
    135 
    136       
    137       <a class="paging-link next" href="/posts/2018-09-01-gotest-tools-skip/" title="Golang testing — gotest.tools skip">Next post →</a>
    138       
    139     </div>
    140 
    141   </div>
    142 </div>
    143 
    144 <footer>
    145   <nav>
    146     
    147     <a href="/">home</a>
    148     <span class="text-muted"> | </span>
    149     
    150     <a href="/about">about</a>
    151     <span class="text-muted"> | </span>
    152     
    153     <a href="/archive">archive</a>
    154     <span class="text-muted"> | </span>
    155     
    156     <a href="/categories">categories</a>
    157     <span class="text-muted"> | </span>
    158     
    159     <a href="/tags">tags</a>
    160     <span class="text-muted"> | </span>
    161     
    162     <a href="https://twitter.com/vdemeest">twitter</a>
    163     <span class="text-muted"> | </span>
    164     
    165     <a href="https://github.com/vdemeester">github</a>
    166     <span class="text-muted"> | </span>
    167     
    168     <a href="https://vincent.demeester.fr/index.xml">rss</a>
    169   </nav>
    170   <br/>
    171   <address>
    172     <span class="copyright">
    173       Content and design by Vincent Demeester
    174       (<a rel="licence" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Some rights reserved</a>)
    175     </span><br />
    176     <span class="engine">
    177       Powered by <a href="https://gohugo.io/">Hugo</a> and <a href="https://github.com/kaushalmodi/ox-hugo/">ox-hugo</a>
    178     </span>
    179   </address>
    180 </footer>
    181 </body>
    182