2018-09-06-gotest-tools-golden.html (6932B)
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 golden</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 golden</h1> 20 </header><nav id="table-of-contents" role="doc-toc"> 21 <h2>Table of Contents</h2> 22 <div id="text-table-of-contents" role="doc-toc"> 23 <ul> 24 <li><a href="#Introduction">Introduction</a></li> 25 <li><a href="#%3DAssert%3D%20and%20%3DAssertBytes%3D"><code>Assert</code> and <code>AssertBytes</code></a></li> 26 <li><a href="#%3DBytes%3D%20and%20%3DString%3D"><code>Bytes</code> and <code>String</code></a></li> 27 <li><a href="#Conclusion%E2%80%A6">Conclusion…</a></li> 28 </ul> 29 </div> 30 </nav> 31 <section id="outline-container-Introduction" class="outline-2"> 32 <h2 id="Introduction">Introduction</h2> 33 <div class="outline-text-2" id="text-Introduction"> 34 <p> 35 Let’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 36 <a href="file:///posts/2017-04-22-golang-testing-golden-file/"><i>quick follow-up</i> on a previous <code>golden</code> post</a>, but focused on the <code>gotest.tools</code> 37 implementation. I’m gonna be quicker, please read that one if <code>golden</code> files is a new 38 concept for you. 39 </p> 40 41 <blockquote> 42 <p> 43 Package <code>golden</code> provides tools for comparing large mutli-line strings. 44 </p> 45 46 <p> 47 Golden files are files in the <code>./testdata/</code> sub-directory of the package under test. 48 </p> 49 </blockquote> 50 51 <p> 52 In the previous article, we described the problem, and how to fix it by writing a small 53 helper. Well, that small helper is in <code>gotest.tools/golden</code> now, and it has a tiny bit 54 more features. 55 </p> 56 57 <p> 58 One of the difference between the <code>gotest.tools</code> implementation and the previous post is 59 the flag name. In <code>gotest.tools/golden</code>, the flag is <code>-test.update-golden</code> (was just 60 <code>-test.update</code> before). Just as before, if the <code>-test.update-golden</code> flag is set then the 61 actual content is written to the golden file, before reading it and comparing. 62 </p> 63 64 <p> 65 There is two ways to use the <code>golden</code> package: 66 </p> 67 <ul class="org-ul"> 68 <li>on it’s own, using <code>golden.Assert</code> or <code>golden.AssertBytes</code></li> 69 <li>as a <code>cmp.Comparison</code>, with <code>golden.String</code> or <code>golden.Bytes</code></li> 70 </ul> 71 </div> 72 </section> 73 <section id="outline-container-%3DAssert%3D%20and%20%3DAssertBytes%3D" class="outline-2"> 74 <h2 id="%3DAssert%3D%20and%20%3DAssertBytes%3D"><code>Assert</code> and <code>AssertBytes</code></h2> 75 <div class="outline-text-2" id="text-%3DAssert%3D%20and%20%3DAssertBytes%3D"> 76 <p> 77 Using <code>Assert</code> functions should be straightforward. Both <code>Assert</code> function compares the 78 actual content to the expected content in the golden file and returns whether the 79 assertion was successful (true) or not (false). 80 </p> 81 82 <ul class="org-ul"> 83 <li><code>Assert</code> uses string. Note that this one <b>removes carriage return</b> before comparing to 84 depend as less as possible of the system (<code>\n</code> vs <code>\r\n</code> 😅)</li> 85 <li><code>AssertBytes</code> uses raw data (in the form of <code>[]byte</code>)</li> 86 </ul> 87 88 <div class="org-src-container"> 89 <pre class="src src-go">golden.Assert(t, "foo", "foo-content.golden") 90 // Could also be used to check some binary format 91 golden.AssertBytes(t, []byte("foo"), "foo-content.golden") 92 </pre> 93 </div> 94 </div> 95 </section> 96 <section id="outline-container-%3DBytes%3D%20and%20%3DString%3D" class="outline-2"> 97 <h2 id="%3DBytes%3D%20and%20%3DString%3D"><code>Bytes</code> and <code>String</code></h2> 98 <div class="outline-text-2" id="text-%3DBytes%3D%20and%20%3DString%3D"> 99 <p> 100 As written in a <a href="file:///posts/2018-08-16-gotest-tools-assertions/">previous post (about the <code>assert</code> package)</a>, I prefer to use <code>cmp.Comparison</code>. 101 </p> 102 103 <blockquote> 104 <p> 105 All those helper functions have a equivalent function in the <code>cmp</code> package that returns a 106 <code>Comparison</code>. I, personally, prefer to use <code>assert.Check</code> or <code>assert.Assert</code> in 107 combination with <code>cmp.Comparison</code> as it allows me to write all my assertions the same way, 108 with built-ins comparison or with my own — i.e. <code>assert.Assert(t, is.Equal(…), "message"</code> 109 or <code>assert.Assert(t, stackIsUp(c, time…), "another message")</code>. 110 </p> 111 </blockquote> 112 113 <p> 114 The <code>golden</code> package gives us that too, in the form of <code>Bytes</code> and <code>String</code>. Using the 115 <code>assert.Check</code> or <code>assert.Assert</code> functions with those is equivalent to their <i>helper</i> 116 counter-part <code>golden.Assert</code> and <code>golden.AssertBytes</code>. 117 </p> 118 119 <div class="org-src-container"> 120 <pre class="src src-go">assert.Assert(t, golden.String("foo", "foo-content.golden")) 121 // Could also be used to check some binary format 122 assert.Assert(t, golden.Bytes([]byte("foo"), "foo-content.golden")) 123 </pre> 124 </div> 125 </div> 126 </section> 127 <section id="outline-container-Conclusion%E2%80%A6" class="outline-2"> 128 <h2 id="Conclusion%E2%80%A6">Conclusion…</h2> 129 <div class="outline-text-2" id="text-Conclusion%E2%80%A6"> 130 <p> 131 … that’s a wrap. As for <a href="file:///posts/2018-09-01-gotest-tools-skip/"><code>skip</code></a>, this is a small package, so the post was going to be 132 quick. <code>golden</code> package just solve a specific problem (read <a href="file:///posts/2017--04-22-golang-testing-golden-file/">Golang testing — golden file</a>) 133 in a simple way. 134 </p> 135 </div> 136 </section> 137 </main> 138 <footer id="postamble" class="status"> 139 <footer> 140 <small><a href="/" rel="history">Index</a> • <a href="/sitemap.html">Sitemap</a> • <a href="https://dl.sbr.pm/">Files</a></small><br/> 141 <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/> 142 <small class='copyright'> 143 Content and design by Vincent Demeester 144 (<a rel='licence' href='http://creativecommons.org/licenses/by-nc-sa/3.0/'>Some rights reserved</a>) 145 </small><br /> 146 </footer> 147 </footer> 148 </body> 149 </html>