| 1 | # goldie - Golden test utility for Go |
| 2 | |
| 3 | [](https://godoc.org/github.com/sebdah/goldie) |
| 4 |  |
| 5 | [](https://goreportcard.com/report/github.com/sebdah/goldie) |
| 6 | |
| 7 | `goldie` is a golden file test utility for Go projects. It's typically used for |
| 8 | testing responses with larger data bodies. |
| 9 | |
| 10 | The concept is straight forward. Valid response data is stored in a "golden |
| 11 | file". The actual response data will be byte compared with the golden file and |
| 12 | the test will fail if there is a difference. |
| 13 | |
| 14 | Updating the golden file can be done by running `go test -update ./...`. |
| 15 | |
| 16 | See the [GoDoc](https://godoc.org/github.com/sebdah/goldie) for API reference |
| 17 | and configuration options. |
| 18 | |
| 19 | # Installation |
| 20 | |
| 21 | Install the latest version, v2, with: |
| 22 | |
| 23 | ```shell |
| 24 | go get -u github.com/sebdah/goldie/v2 |
| 25 | ``` |
| 26 | |
| 27 | For the older v1 release, please use: |
| 28 | |
| 29 | ```shell |
| 30 | go get -u github.com/sebdah/goldie |
| 31 | ``` |
| 32 | |
| 33 | # Example usage |
| 34 | |
| 35 | ## Basic assertions |
| 36 | |
| 37 | The below example fetches data from a REST API. The last line in the test is the |
| 38 | actual usage of `goldie`. It takes the HTTP response body and asserts that it's |
| 39 | what is present in the golden test file. |
| 40 | |
| 41 | ``` |
| 42 | func TestExample(t *testing.T) { |
| 43 | recorder := httptest.NewRecorder() |
| 44 | |
| 45 | req, err := http.NewRequest("GET", "/example", nil) |
| 46 | assert.Nil(t, err) |
| 47 | |
| 48 | handler := http.HandlerFunc(ExampleHandler) |
| 49 | handler.ServeHTTP() |
| 50 | |
| 51 | g := goldie.New(t) |
| 52 | g.Assert(t, "example", recorder.Body.Bytes()) |
| 53 | } |
| 54 | ``` |
| 55 | |
| 56 | ## Assertions using templates |
| 57 | |
| 58 | If some values in the golden file can change depending on the test, you can use |
| 59 | golang template in the golden file and pass the data to `AssertWithTemplate`. |
| 60 | |
| 61 | ### example.golden |
| 62 | ``` |
| 63 | This is a {{ .Type }} file. |
| 64 | ``` |
| 65 | |
| 66 | ### Test |
| 67 | ``` |
| 68 | func TestTemplateExample(t *testing.T) { |
| 69 | recorder := httptest.NewRecorder() |
| 70 | |
| 71 | req, err := http.NewRequest("POST", "/example/Golden", nil) |
| 72 | assert.Nil(t, err) |
| 73 | |
| 74 | handler := http.HandlerFunc(ExampleHandler) |
| 75 | handler.ServeHTTP() |
| 76 | |
| 77 | data := struct { |
| 78 | Type string |
| 79 | }{ |
| 80 | Type: "Golden", |
| 81 | } |
| 82 | |
| 83 | g := goldie.New(t) |
| 84 | g.AssertWithTemplate(t, "example", data, recorder.Body.Bytes()) |
| 85 | } |
| 86 | ``` |
| 87 | |
| 88 | Then run your test with the `-update` flag the first time to store the result. |
| 89 | |
| 90 | `go test -update ./...` |
| 91 | |
| 92 | For any consecutive runs where you actually want to compare the data, simply |
| 93 | drop the `-update` flag. |
| 94 | |
| 95 | `go test ./...` |
| 96 | |
| 97 | ## Validating JSON and XML output |
| 98 | |
| 99 | If you are asserting JSON and XML data, you can use the handy `AssertJson` and |
| 100 | `AssertXml` functions that will nicely indent the golden validation files for |
| 101 | better readability. |
| 102 | |
| 103 | # Flags |
| 104 | |
| 105 | ## Clean output directory |
| 106 | |
| 107 | Using `-update` along with `-clean` flag will clear the fixture directory before updating golden files. |
| 108 | |
| 109 | `go test -update -clean ./...` |
| 110 | |
| 111 | |
| 112 | # Options |
| 113 | |
| 114 | `goldie` supports a number of configuration options that will alter the behavior |
| 115 | of the library. These options should be passed into the `goldie.New()` method. |
| 116 | |
| 117 | ``` |
| 118 | func TestNewExample(t *testing.T) { |
| 119 | g := goldie.New( |
| 120 | t, |
| 121 | goldie.WithFixtureDir("test-fixtures"), |
| 122 | goldie.WithNameSuffix(".golden.json"), |
| 123 | goldie.WithDiffEngine(goldie.ColoredDiff), |
| 124 | goldie.WithTestNameForDir(true), |
| 125 | ) |
| 126 | |
| 127 | g.Assert(t, "example", []byte("my example data")) |
| 128 | } |
| 129 | ``` |
| 130 | |
| 131 | ## Available options |
| 132 | |
| 133 | | Option | Comment | Default |
| 134 | |----------------------------|----------------------------------------------------------|------------- |
| 135 | | `WithFixtureDir` | Set fixture dir name | `testdata` |
| 136 | | `WithNameSuffix` | Suffix for fixture files. | `.golden` |
| 137 | | `WithDirPerms` | Directory permissions for fixtures | `0755` |
| 138 | | `WithFilePerms` | File permissions for fixtures | `0644` |
| 139 | | `WithDiffEngine` | Diff engine to use for diff output | `ClassicDiff` |
| 140 | | `WithDiffFn` | Custom diff logic to be used | None |
| 141 | | `WithIgnoreTemplateErrors` | Ignore errors from templates | `false` |
| 142 | | `WithTestNameForDir` | Create a folder with the tests name for the fixtures | `false` |
| 143 | | `WithSubTestNameForDir` | Create a folder with the sub tests name for the fixtures | `false` |
| 144 | |
| 145 | ## Diff output |
| 146 | |
| 147 | Goldie has three output modes; classic diff (default), colored diffs and simple |
| 148 | mode. |
| 149 | |
| 150 | You can select your preferred output using the `WithDiffEngine` option: |
| 151 | |
| 152 | ``` |
| 153 | g.New( |
| 154 | t, |
| 155 | goldie.WithDiffEngine(goldie.ColoredDiff), // Simple, ColoredDiff, ClassicDiff |
| 156 | ) |
| 157 | ``` |
| 158 | |
| 159 | # Goldie v2 |
| 160 | |
| 161 | With the release of Goldie v2.0.0 we are introducing features that will break |
| 162 | backwards compatibility with older versions of the test helper. A few things |
| 163 | have changed: |
| 164 | |
| 165 | ## New default fixture directory |
| 166 | |
| 167 | There is a new default directory for fixtures, `testdata`. This directory is a |
| 168 | better default as it is more widely used in the Go community (including the |
| 169 | standard library). See issue [#10](https://github.com/sebdah/goldie/issues/10) |
| 170 | for details. |
| 171 | |
| 172 | ## New way to initialize Goldie |
| 173 | |
| 174 | With the introduction of the functional options we also introduced `goldie.New`, |
| 175 | which is initializing Goldie. `Assert*` and other methods are now accessed like: |
| 176 | |
| 177 | ``` |
| 178 | g := goldie.New(t) |
| 179 | g.Assert(t, ...) |
| 180 | ``` |
| 181 | |
| 182 | # FAQ |
| 183 | |
| 184 | ## Do you need any help in the project? |
| 185 | |
| 186 | Yes, please! Pull requests are most welcome. On the wish list: |
| 187 | |
| 188 | - Unit tests. |
| 189 | |
| 190 | ## Why the name `goldie`? |
| 191 | |
| 192 | The name comes from the fact that it's for Go and handles golden file testing. |
| 193 | But yes, it may not be the best name in the world. |
| 194 | |
| 195 | ### How did you come up with the idea? |
| 196 | |
| 197 | This is based on the great [Advanced Go |
| 198 | testing](https://www.youtube.com/watch?v=yszygk1cpEc) talk by |
| 199 | [@mitchellh](https://twitter.com/mitchellh). |
| 200 | |
| 201 | # License |
| 202 | |
| 203 | MIT |
| 204 | |
| 205 | Copyright 2016 Sebastian Dahlgren <[email protected]> |
| 206 | |
| 207 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 208 | this software and associated documentation files (the "Software"), to deal in |
| 209 | the Software without restriction, including without limitation the rights to |
| 210 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 211 | the Software, and to permit persons to whom the Software is furnished to do so, |
| 212 | subject to the following conditions: |
| 213 | |
| 214 | The above copyright notice and this permission notice shall be included in all |
| 215 | copies or substantial portions of the Software. |
| 216 | |
| 217 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 218 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 219 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 220 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 221 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 222 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |