2018-09-06-gotest-tools-golden.org (3426B)
1 #+TITLE: Golang testing — gotest.tools golden 2 #+date: <2018-09-06 Thu> 3 #+filetags: go testing golden 4 #+setupfile: ../templates/post.org 5 6 #+TOC: headlines 2 7 8 * Introduction 9 Let's continue the [[https://gotest.tools][=gotest.tools=]] serie, this time with the =golden= package. This is a 10 [[/posts/2017-04-22-golang-testing-golden-file/][/quick follow-up/ on a previous =golden= post]], but focused on the =gotest.tools= 11 implementation. I'm gonna be quicker, please read that one if =golden= files is a new 12 concept for you. 13 14 #+BEGIN_QUOTE 15 Package =golden= provides tools for comparing large mutli-line strings. 16 17 Golden files are files in the =./testdata/= sub-directory of the package under test. 18 #+END_QUOTE 19 20 In the previous article, we described the problem, and how to fix it by writing a small 21 helper. Well, that small helper is in =gotest.tools/golden= now, and it has a tiny bit 22 more features. 23 24 One of the difference between the =gotest.tools= implementation and the previous post is 25 the flag name. In =gotest.tools/golden=, the flag is =-test.update-golden= (was just 26 =-test.update= before). Just as before, if the =-test.update-golden= flag is set then the 27 actual content is written to the golden file, before reading it and comparing. 28 29 There is two ways to use the =golden= package: 30 - on it's own, using =golden.Assert= or =golden.AssertBytes= 31 - as a =cmp.Comparison=, with =golden.String= or =golden.Bytes= 32 33 * =Assert= and =AssertBytes= 34 35 Using =Assert= functions should be straightforward. Both =Assert= function compares the 36 actual content to the expected content in the golden file and returns whether the 37 assertion was successful (true) or not (false). 38 39 - =Assert= uses string. Note that this one *removes carriage return* before comparing to 40 depend as less as possible of the system (=\n= vs =\r\n= 😅) 41 - =AssertBytes= uses raw data (in the form of =[]byte=) 42 43 #+BEGIN_SRC go 44 golden.Assert(t, "foo", "foo-content.golden") 45 // Could also be used to check some binary format 46 golden.AssertBytes(t, []byte("foo"), "foo-content.golden") 47 #+END_SRC 48 49 * =Bytes= and =String= 50 51 As written in a [[/posts/2018-08-16-gotest-tools-assertions/][previous post (about the =assert= package)]], I prefer to use =cmp.Comparison=. 52 53 #+BEGIN_QUOTE 54 All those helper functions have a equivalent function in the =cmp= package that returns a 55 =Comparison=. I, personally, prefer to use =assert.Check= or =assert.Assert= in 56 combination with =cmp.Comparison= as it allows me to write all my assertions the same way, 57 with built-ins comparison or with my own — i.e. =assert.Assert(t, is.Equal(…), "message"= 58 or =assert.Assert(t, stackIsUp(c, time…), "another message")=. 59 #+END_QUOTE 60 61 The =golden= package gives us that too, in the form of =Bytes= and =String=. Using the 62 =assert.Check= or =assert.Assert= functions with those is equivalent to their /helper/ 63 counter-part =golden.Assert= and =golden.AssertBytes=. 64 65 #+BEGIN_SRC go 66 assert.Assert(t, golden.String("foo", "foo-content.golden")) 67 // Could also be used to check some binary format 68 assert.Assert(t, golden.Bytes([]byte("foo"), "foo-content.golden")) 69 #+END_SRC 70 71 * Conclusion… 72 73 … that's a wrap. As for [[/posts/2018-09-01-gotest-tools-skip/][=skip=]], this is a small package, so the post was going to be 74 quick. =golden= package just solve a specific problem (read [[/posts/2017--04-22-golang-testing-golden-file/][Golang testing — golden file]]) 75 in a simple way.