2018-09-01-gotest-tools-skip.org (2728B)
1 #+title: Golang testing — gotest.tools skip 2 #+date: <2018-09-01 Sat> 3 #+filetags: go testing skip 4 #+setupfile: ../templates/post.org 5 6 * Introduction 7 8 Let's continue the [[https://gotest.tools][=gotest.tools=]] serie, this time with the =skip= package. This is a 9 really simple one so this should be quick. 10 11 #+BEGIN_QUOTE 12 =skip= provides functions for skipping a test and printing the source code of the 13 condition used to skip the test. 14 #+END_QUOTE 15 16 The package consists of only one function : =If=. The idea comes mainly from 17 [[https://github.com/docker/docker][=docker/docker=]] integration test suite, where we wanted to skip some test (or test suites) 18 given different context. By context I mean things like the system we are running on 19 (=Windows=, =Linux=, …) or the capabilities of the running kernel or node (is =apparmor= 20 available or not on the current machine). 21 22 This =If= method takes a =testing.T= pointer and either a boolean, a function that 23 returns a boolean, *or* an expression. 24 25 #+BEGIN_SRC go 26 // boolean 27 // --- SKIP: TestName (0.00s) 28 // skip.go:19: MissingFeature 29 var MissingFeature bool 30 skip.If(t, MissingFeature) 31 32 // function 33 // --- SKIP: TestName (0.00s) 34 // skip.go:19: !IsExperimentalDaemon(dockerClient): daemon is not experimental 35 skip.If(t, IsExperimentalDaemon(dockerClient), "daemon is not experimental") 36 37 // expression 38 // --- SKIP: TestName (0.00s) 39 // skip.go:19: apiVersion < version("v1.24") 40 skip.If(t, apiVersion < version("v1.24")) 41 #+END_SRC 42 43 There is few elements to note though : 44 45 - This package (as other parts of the =gotest.tools= packages), will try to look at source 46 files to display the expression used (same goes for =assert=). This is usually not a 47 problem because you run tests where the source code is. *However*, in the cases you 48 generate a test binary to be executed later (à-la =kubernetes= or other projects), this 49 can display a weird error message if the sources are not available… You shouldn't be 50 worried too much about it, but it's better if you know :) 51 - The main reason to use =skip.If= is mainly for new contributors to get in quickly, 52 *reducing potential friction of them running the tests on their environment*. The more 53 the tests are written in a way they explicitely declare their requirements (and skipped 54 if the environment does not meet those), the easier it makes contributors run your 55 tests. *But*, this also means, you should try to measure the skipped tests on your 56 continuous integration system to make sure you run all of them eventually… otherwise 57 it's dead code. /But more on that in later posts 😉/. 58 59 That's all for today folks, told you that was going to be quick.