<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Testing on Cheat Sheet</title><link>/tags/testing/</link><description>Recent content in Testing on Cheat Sheet</description><generator>Hugo</generator><language>en</language><atom:link href="/tags/testing/index.xml" rel="self" type="application/rss+xml"/><item><title>testing</title><link>/programming/go/std/testing/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/go/std/testing/</guid><description>&lt;h1 id="testing"&gt;testing&lt;a class="anchor" href="#testing"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The &lt;code&gt;testing&lt;/code&gt; package provides support for automated testing of Go packages. It is intended to be used in concert with the &lt;code&gt;go test&lt;/code&gt; command.&lt;/p&gt;
&lt;h2 id="test-functions"&gt;Test Functions&lt;a class="anchor" href="#test-functions"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;func&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#6639ba"&gt;TestXxx&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#1f2328"&gt;t&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#0550ae"&gt;*&lt;/span&gt;&lt;span style="color:#1f2328"&gt;testing&lt;/span&gt;&lt;span style="color:#1f2328"&gt;.&lt;/span&gt;&lt;span style="color:#1f2328"&gt;T&lt;/span&gt;&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;Test functions must start with &lt;code&gt;Test&lt;/code&gt; followed by a capitalized identifier&lt;/li&gt;
&lt;li&gt;Must accept a single parameter of type &lt;code&gt;*testing.T&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="key-methods"&gt;Key Methods&lt;a class="anchor" href="#key-methods"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;t.Fail()&lt;/code&gt;: Marks the test as failed, but continues execution&lt;/li&gt;
&lt;li&gt;&lt;code&gt;t.FailNow()&lt;/code&gt;: Marks the test as failed and stops execution of the current goroutine immediately via &lt;code&gt;runtime.Goexit()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;t.Errorf(format string, args ...any)&lt;/code&gt;: Equivalent to &lt;code&gt;t.Logf&lt;/code&gt; + &lt;code&gt;t.Fail()&lt;/code&gt;. Logs an error and marks the test as failed, but continues execution&lt;/li&gt;
&lt;li&gt;&lt;code&gt;t.Fatalf(format string, args ...any)&lt;/code&gt;: Equivalent to &lt;code&gt;t.Errorf&lt;/code&gt; + &lt;code&gt;t.FailNow()&lt;/code&gt; (&lt;code&gt;t.Logf&lt;/code&gt; + &lt;code&gt;t.FailNow()&lt;/code&gt;). Logs an error and stops execution of the current test goroutine immediately&lt;/li&gt;
&lt;li&gt;&lt;code&gt;t.Skip(args ...any)&lt;/code&gt;: Marks the test as skipped and stops execution immediately&lt;/li&gt;
&lt;li&gt;&lt;code&gt;t.Helper()&lt;/code&gt;: Marks the calling function as a test helper. When printing file and line information in test failures, the line inside the helper function is skipped, reporting the caller&amp;rsquo;s line instead&lt;/li&gt;
&lt;li&gt;&lt;code&gt;t.Cleanup(f func())&lt;/code&gt;: Registers a teardown function (eg. closing DB connections, releasing RPC clients) that executes in LIFO order &lt;strong&gt;after&lt;/strong&gt; &lt;code&gt;t.Context()&lt;/code&gt; is canceled. See &lt;a href="#cleanup-tcleanup"&gt;Cleanup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;t.Context() context.Context&lt;/code&gt;: Returns a context that is automatically canceled as soon as the test body finishes (&lt;strong&gt;before&lt;/strong&gt; &lt;code&gt;t.Cleanup&lt;/code&gt; callbacks run). See &lt;a href="#test-contexts-tcontext"&gt;Test Contexts&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="failure-handling-in-goroutines"&gt;Failure Handling in Goroutines&lt;a class="anchor" href="#failure-handling-in-goroutines"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;t.Fatalf&lt;/code&gt; and &lt;code&gt;t.FailNow&lt;/code&gt; call &lt;code&gt;runtime.Goexit()&lt;/code&gt;, which only terminates the calling goroutine. Never call &lt;code&gt;t.Fatalf&lt;/code&gt; or &lt;code&gt;t.FailNow&lt;/code&gt; inside a background goroutine (&lt;code&gt;go func()&lt;/code&gt;); doing so leaves the main test goroutine running or hanging and violates &lt;code&gt;go vet&lt;/code&gt;. Always use &lt;code&gt;t.Errorf&lt;/code&gt; followed by &lt;code&gt;return&lt;/code&gt; inside worker goroutines.&lt;/p&gt;</description></item></channel></rss>