2018-09-01-gotest-tools-skip.html (4586B)
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 skip</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 skip</h1> 20 </header><section id="outline-container-Introduction" class="outline-2"> 21 <h2 id="Introduction">Introduction</h2> 22 <div class="outline-text-2" id="text-Introduction"> 23 <p> 24 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 25 really simple one so this should be quick. 26 </p> 27 28 <blockquote> 29 <p> 30 <code>skip</code> provides functions for skipping a test and printing the source code of the 31 condition used to skip the test. 32 </p> 33 </blockquote> 34 35 <p> 36 The package consists of only one function : <code>If</code>. The idea comes mainly from 37 <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) 38 given different context. By context I mean things like the system we are running on 39 (<code>Windows</code>, <code>Linux</code>, …) or the capabilities of the running kernel or node (is <code>apparmor</code> 40 available or not on the current machine). 41 </p> 42 43 <p> 44 This <code>If</code> method takes a <code>testing.T</code> pointer and either a boolean, a function that 45 returns a boolean, <b>or</b> an expression. 46 </p> 47 48 <div class="org-src-container"> 49 <pre class="src src-go">// boolean 50 // --- SKIP: TestName (0.00s) 51 // skip.go:19: MissingFeature 52 var MissingFeature bool 53 skip.If(t, MissingFeature) 54 55 // function 56 // --- SKIP: TestName (0.00s) 57 // skip.go:19: !IsExperimentalDaemon(dockerClient): daemon is not experimental 58 skip.If(t, IsExperimentalDaemon(dockerClient), "daemon is not experimental") 59 60 // expression 61 // --- SKIP: TestName (0.00s) 62 // skip.go:19: apiVersion < version("v1.24") 63 skip.If(t, apiVersion < version("v1.24")) 64 </pre> 65 </div> 66 67 <p> 68 There is few elements to note though : 69 </p> 70 71 <ul class="org-ul"> 72 <li>This package (as other parts of the <code>gotest.tools</code> packages), will try to look at source 73 files to display the expression used (same goes for <code>assert</code>). This is usually not a 74 problem because you run tests where the source code is. <b>However</b>, in the cases you 75 generate a test binary to be executed later (à-la <code>kubernetes</code> or other projects), this 76 can display a weird error message if the sources are not available… You shouldn’t be 77 worried too much about it, but it’s better if you know :)</li> 78 <li>The main reason to use <code>skip.If</code> is mainly for new contributors to get in quickly, 79 <b>reducing potential friction of them running the tests on their environment</b>. The more 80 the tests are written in a way they explicitely declare their requirements (and skipped 81 if the environment does not meet those), the easier it makes contributors run your 82 tests. <b>But</b>, this also means, you should try to measure the skipped tests on your 83 continuous integration system to make sure you run all of them eventually… otherwise 84 it’s dead code. <i>But more on that in later posts 😉</i>.</li> 85 </ul> 86 87 <p> 88 That’s all for today folks, told you that was going to be quick. 89 </p> 90 </div> 91 </section> 92 </main> 93 <footer id="postamble" class="status"> 94 <footer> 95 <small><a href="/" rel="history">Index</a> • <a href="/sitemap.html">Sitemap</a> • <a href="https://dl.sbr.pm/">Files</a></small><br/> 96 <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/> 97 <small class='copyright'> 98 Content and design by Vincent Demeester 99 (<a rel='licence' href='http://creativecommons.org/licenses/by-nc-sa/3.0/'>Some rights reserved</a>) 100 </small><br /> 101 </footer> 102 </footer> 103 </body> 104 </html>