<p>I'm using the <a href="https://github.com/stretchr/testify"><code>testify</code></a> package a lot when writing tests using <a href="https://golang.org">Go</a>. I find they make my test code much clearer, more concise and easier to read.</p> <p>However, when you use it in your tests, you need to be very careful when choosing between <a href="https://github.com/stretchr/testify#assert-package"><code>assert</code></a> and <a href="https://github.com/stretchr/testify#require-package"><code>require</code></a>. They look the same but are different in the way they behave. Which one you choose depends on the behaviour you are looking for.</p> <p>If you use <a href="https://github.com/stretchr/testify#assert-package"><code>assert</code></a>, your test will look like:</p> <div class="highlight"><pre><span></span><span class="kn">package</span> <span class="nx">yours</span> <span class="kn">import</span> <span class="p">(</span> <span class="s">&quot;testing&quot;</span> <span class="s">&quot;github.com/stretchr/testify/assert&quot;</span> <span class="p">)</span> <span class="kd">func</span> <span class="nx">TestSomething</span><span class="p">(</span><span class="nx">t</span> <span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span> <span class="p">{</span> <span class="nx">assert</span><span class="p">.</span><span class="nx">Equal</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="mi">123</span><span class="p">,</span> <span class="mi">123</span><span class="p">,</span> <span class="s">&quot;they should be equal&quot;</span><span class="p">)</span> <span class="nx">assert</span><span class="p">.</span><span class="nx">NotEqual</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="mi">123</span><span class="p">,</span> <span class="mi">456</span><span class="p">,</span> <span class="s">&quot;they should not be equal&quot;</span><span class="p">)</span> <span class="nx">assert</span><span class="p">.</span><span class="nx">Nil</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="nx">object</span><span class="p">)</span> <span class="k">if</span> <span class="nx">assert</span><span class="p">.</span><span class="nx">NotNil</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="nx">object</span><span class="p">)</span> <span class="p">{</span> <span class="nx">assert</span><span class="p">.</span><span class="nx">Equal</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="s">&quot;Something&quot;</span><span class="p">,</span> <span class="nx">object</span><span class="p">.</span><span class="nx">Value</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <p>With <a href="https://github.com/stretchr/testify#assert-package"><code>assert</code></a>, you will get a log entry for each assertation.</p> <p>Changing it to use <a href="https://github.com/stretchr/testify#require-package"><code>require</code></a> looks similar until you run the actual tests:</p> <div class="highlight"><pre><span></span><span class="kn">package</span> <span class="nx">yours</span> <span class="kn">import</span> <span class="p">(</span> <span class="s">&quot;testing&quot;</span> <span class="s">&quot;github.com/stretchr/testify/require&quot;</span> <span class="p">)</span> <span class="kd">func</span> <span class="nx">TestSomething</span><span class="p">(</span><span class="nx">t</span> <span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span> <span class="p">{</span> <span class="nx">require</span><span class="p">.</span><span class="nx">Equal</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="mi">123</span><span class="p">,</span> <span class="mi">123</span><span class="p">,</span> <span class="s">&quot;they should be equal&quot;</span><span class="p">)</span> <span class="nx">require</span><span class="p">.</span><span class="nx">NotEqual</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="mi">123</span><span class="p">,</span> <span class="mi">456</span><span class="p">,</span> <span class="s">&quot;they should not be equal&quot;</span><span class="p">)</span> <span class="nx">require</span><span class="p">.</span><span class="nx">Nil</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="nx">object</span><span class="p">)</span> <span class="k">if</span> <span class="nx">require</span><span class="p">.</span><span class="nx">NotNil</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="nx">object</span><span class="p">)</span> <span class="p">{</span> <span class="nx">require</span><span class="p">.</span><span class="nx">Equal</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="s">&quot;Something&quot;</span><span class="p">,</span> <span class="nx">object</span><span class="p">.</span><span class="nx">Value</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <p>With <a href="https://github.com/stretchr/testify#require-package"><code>require</code></a>, as soon as you run the tests, the first requirement which fails interrupts and fails the complete test. So, if the first requirement fails, the rest of the test will be skipped.</p> <p>An alternative to testify is the <a href="https://github.com/matryer/is"><code>is</code></a> package from <a href="https://github.com/matryer">Mat Ryer</a>. The biggest difference is that this library only supports the same flow as the <a href="https://github.com/stretchr/testify#assert-package"><code>assert</code></a> package. The advantage is that the output is much more concise and the API is a lot more straightforward. I guess I'll have to give it a go when writing tests again…</p>

Related Posts

  • Using environment variables in Go tests
  • Testing exceptions in pytest
  • Parsing a key pair from a PEM file in Go
  • Pointer vs value receivers
  • Handling Unix timestamps in JSON