index.html (7394B)
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-01-gotest-tools-skip/"> 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 skip</h1><a href='https://vincent.demeester.fr/posts/2018-09-01-gotest-tools-skip/'></a> 37 <address class="signature"> 38 <span class="date">Sat, 1 September, 2018</span> 39 <span class="words">(400 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 55 <li class="tag tag-skip"><a href="/tags/#skip">skip<span>1</span></a></li> 56 57 <br/> 58 59 </ul> 60 </header> 61 62 63 64 <p>Let’s continue the <a href="https://gotest.tools"><code>gotest.tools</code></a> serie, this time with the <code>skip</code> package. This is a 65 really simple one so this should be quick.</p> 66 67 <blockquote> 68 <p><code>skip</code> provides functions for skipping a test and printing the source code of the 69 condition used to skip the test.</p> 70 </blockquote> 71 72 <p>The package consists of only one function : <code>If</code>. The idea comes mainly from 73 <a href="https://github.com/docker/docker"><code>docker/docker</code></a> integration test suite, where we wanted to skip some test (or test suites) 74 given different context. By context I mean things like the system we are running on 75 (<code>Windows</code>, <code>Linux</code>, …) or the capabilities of the running kernel or node (is <code>apparmor</code> 76 available or not on the current machine).</p> 77 78 <p>This <code>If</code> method takes a <code>testing.T</code> pointer and either a boolean, a function that 79 returns a boolean, <strong>or</strong> an expression.</p> 80 <div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="c1">// boolean 81 </span><span class="c1">// --- SKIP: TestName (0.00s) 82 </span><span class="c1">// skip.go:19: MissingFeature 83 </span><span class="c1"></span><span class="kd">var</span> <span class="nx">MissingFeature</span> <span class="kt">bool</span> 84 <span class="nx">skip</span><span class="p">.</span><span class="nf">If</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="nx">MissingFeature</span><span class="p">)</span> 85 86 <span class="c1">// function 87 </span><span class="c1">// --- SKIP: TestName (0.00s) 88 </span><span class="c1">// skip.go:19: !IsExperimentalDaemon(dockerClient): daemon is not experimental 89 </span><span class="c1"></span><span class="nx">skip</span><span class="p">.</span><span class="nf">If</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="nf">IsExperimentalDaemon</span><span class="p">(</span><span class="nx">dockerClient</span><span class="p">),</span> <span class="s">"daemon is not experimental"</span><span class="p">)</span> 90 91 <span class="c1">// expression 92 </span><span class="c1">// --- SKIP: TestName (0.00s) 93 </span><span class="c1">// skip.go:19: apiVersion < version("v1.24") 94 </span><span class="c1"></span><span class="nx">skip</span><span class="p">.</span><span class="nf">If</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="nx">apiVersion</span> <span class="p"><</span> <span class="nf">version</span><span class="p">(</span><span class="s">"v1.24"</span><span class="p">))</span></code></pre></div> 95 <p>There is few elements to note though :</p> 96 97 <ul> 98 <li>This package (as other parts of the <code>gotest.tools</code> packages), will try to look at source 99 files to display the expression used (same goes for <code>assert</code>). This is usually not a 100 problem because you run tests where the source code is. <strong>However</strong>, in the cases you 101 generate a test binary to be executed later (à-la <code>kubernetes</code> or other projects), this 102 can display a weird error message if the sources are not available… You shouldn’t be 103 worried too much about it, but it’s better if you know :)</li> 104 <li>The main reason to use <code>skip.If</code> is mainly for new contributors to get in quickly, 105 <strong>reducing potential friction of them running the tests on their environment</strong>. The more 106 the tests are written in a way they explicitely declare their requirements (and skipped 107 if the environment does not meet those), the easier it makes contributors run your 108 tests. <strong>But</strong>, this also means, you should try to measure the skipped tests on your 109 continuous integration system to make sure you run all of them eventually… otherwise 110 it’s dead code. <em>But more on that in later posts 😉</em>.</li> 111 </ul> 112 113 <p>That’s all for today folks, told you that was going to be quick.</p> 114 115 116 </article> 117 <hr /> 118 <div class="prev-next"> 119 120 <a class="paging-link prev" href="/posts/2018-09-06-gotest-tools-golden/" title="Golang testing — gotest.tools golden">← Previous post</a> 121 122 123 124 <a class="paging-link next" href="/posts/2018-08-16-gotest-tools-assertions/" title="Golang testing — gotest.tools assertions">Next post →</a> 125 126 </div> 127 128 </div> 129 </div> 130 131 <footer> 132 <nav> 133 134 <a href="/">home</a> 135 <span class="text-muted"> | </span> 136 137 <a href="/about">about</a> 138 <span class="text-muted"> | </span> 139 140 <a href="/archive">archive</a> 141 <span class="text-muted"> | </span> 142 143 <a href="/categories">categories</a> 144 <span class="text-muted"> | </span> 145 146 <a href="/tags">tags</a> 147 <span class="text-muted"> | </span> 148 149 <a href="https://twitter.com/vdemeest">twitter</a> 150 <span class="text-muted"> | </span> 151 152 <a href="https://github.com/vdemeester">github</a> 153 <span class="text-muted"> | </span> 154 155 <a href="https://vincent.demeester.fr/index.xml">rss</a> 156 </nav> 157 <br/> 158 <address> 159 <span class="copyright"> 160 Content and design by Vincent Demeester 161 (<a rel="licence" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Some rights reserved</a>) 162 </span><br /> 163 <span class="engine"> 164 Powered by <a href="https://gohugo.io/">Hugo</a> and <a href="https://github.com/kaushalmodi/ox-hugo/">ox-hugo</a> 165 </span> 166 </address> 167 </footer> 168 </body> 169