https://github.com/larhun/golden
Package for testing commands using smart validation strings and gold master files.
https://github.com/larhun/golden
command testing
Last synced: 22 days ago
JSON representation
Package for testing commands using smart validation strings and gold master files.
- Host: GitHub
- URL: https://github.com/larhun/golden
- Owner: larhun
- License: bsd-3-clause
- Created: 2018-03-03T10:24:14.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-08-01T09:31:16.000Z (almost 7 years ago)
- Last Synced: 2025-12-17T10:13:53.662Z (7 months ago)
- Topics: command, testing
- Language: Go
- Homepage:
- Size: 15.6 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# golden
[](https://opensource.org/licenses/BSD-3-Clause)
[](http://codecov.io/github/larhun/golden?branch=master)
[](https://travis-ci.org/larhun/golden)
[](https://goreportcard.com/report/larhun/golden)
[](https://godoc.org/github.com/larhun/golden)
----
Package golden provides functions for testing commands using smart validation
strings and gold master files.
A command must implement the `Runner` interface. It represents a black box
system with inputs (the argument list) and outputs (the standard and error
outputs, the panic and error messages, the exit code, and an optional file
output). A test case is a value of `Case` type. It represents a set of
input and output values. A test is performed using the `Test` function.
The following example tests a command named `"hello"` that should print `"Hello
World!"` to its standard output with an argument list equal to `[]string{"hello",
"World"}` (the first argument must be the command name).
```Go
var command Runner = ... // the "hello" command
func TestXxx(t *testing.T) {
Test(t, command, []Case{{
Name: "output", // test case name
Args: []string{"hello", "World"}, // argument list
WantStdout: "Hello World!", // expected standard output
})
}
```
The command under test may be implemented using inner functions or may be
generated from an external `hello` program using the `Program` function:
```Go
var command = Program("hello", nil)
```
The `Test` function supports smart validation strings and gold master files that
define very flexible matches with a very simple syntax (see the package
documentation for details). The `"..."` ellipsis is used to encode a partial
match:
```Go
"Hello..." // match "Hello" prefix
"...World!" // match "World!" suffix
"...World..." // match "World" substring
```
The `"^"` and `"$"` delimiters are used to encode a pattern matching:
```Go
"^.*(Hello|World).*$" // match "Hello" or "World" substring
```
The `"golden"` term is used to encode a value stored by a gold master file:
```Go
"golden.json" // match file testdata/golden/TestXxx-output.json
```
The gold master pattern is commonly used when testing complex output: the
expected string is saved to a file, the gold master, rather than to a validation
string. All the gold masters used by `TestXxx` are updated by running the test
with the `update` flag:
```Shell
go test -run TestXxx -update
```
All the gold masters are updates by running:
```Shell
go test -update
```
See the testing files for usage examples.