sebdah / goldie Goto Github PK
View Code? Open in Web Editor NEWGolden file testing for Go
License: MIT License
Golden file testing for Go
License: MIT License
Love this.
One minor annoyance - when I add a completely new test, I have to create a directory within my fixtures directory by hand ex:
In this example, FixtureDir
is testdata/fixtures
, and in this case I just added a table test called TestThumbnailJPG
with subtests TestThumbnail-jpg-0
etc. So, then when i go test -update
it still fails:
--- FAIL: TestThumbnailJPG (2.88s)
--- FAIL: TestThumbnailJPG/TestThumbnail-jpg-0- (0.10s)
goldie.go:56: open testdata/fixtures/TestThumbnailJPG/TestThumbnail-jpg-0-.golden: no such file or directory
--- FAIL: TestThumbnailJPG/TestThumbnail-jpg-1- (0.08s)
goldie.go:56: open testdata/fixtures/TestThumbnailJPG/TestThumbnail-jpg-1-.golden: no such file or directory
--- FAIL: TestThumbnailJPG/TestThumbnail-jpg-2- (0.09s)
goldie.go:56: open testdata/fixtures/TestThumbnailJPG/TestThumbnail-jpg-2-.golden: no such file or directory
The -update
flag fails because the directory for that test does not exist. So, I mkdir
then it goes well.
It'd be nice to have a -mkdir
flag that assumes -update
, or maybe -update mkdir
as an option command to the -update
flag or something - so when you run that, it automatically creates the required folders. I'd only see a use for that flag when you've changed FixtureDir
or when you've written a new test - with a new test name - so I think I'd rather it not happen every time you -update
.
I have a test file like:
// +build e2e
package e2e
import (
"testing"
"github.com/sebdah/goldie/v2"
)
func TestFoo(t *testing.T) {
g := goldie.New(t)
out := []byte("some-text")
g.Assert(t, "example", out)
}
And I am running:
go test -tags=e2e -update -clean ./...
And the output is:
? github.com/myrepo/myproj [no test files]
I would expect it to create the golden file but it does nothing. If I remove the -update
flag the the test runs but fails because of no golden file.
Manually creating the golden file does work but I cannot get the -update
flag to create the file.
I am using v2.
Any ideas what I am doing wrong?
It would be nice if the next version used a syntax similar to stretchr/testify. Since t
is passed in to New()
we shouldn't need to pass it in again for each assertion.
See this example from stretchr/testify:
package yours
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSomething(t *testing.T) {
assert := assert.New(t)
// assert equality
assert.Equal(123, 123, "they should be equal")
// assert inequality
assert.NotEqual(123, 456, "they should not be equal")
// assert for nil (good for errors)
assert.Nil(object)
// assert for not nil (good when you expect something)
if assert.NotNil(object) {
// now we know that object isn't nil, we are safe to make
// further assertions without causing any errors
assert.Equal("Something", object.Value)
}
}
For goldie it would look like this:
func TestExample(t *testing.T) {
recorder := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/example", nil)
assert.Nil(t, err)
handler := http.HandlerFunc(ExampleHandler)
handler.ServeHTTP()
g := goldie.New(t)
g.Assert("example", recorder.Body.Bytes()) // notice lack of `t` arg here.
}
If there is interest, I would be happy to make a PR.
Given encoding.json and encoding.xml are both standard packages it would be nice to have the same support for XML as for JSON.
It is a simple addition to provide the helper method, but does break any other Tester API implementations.
First, thanks for building this package!
I'm trying to leverage the goldie.AssertWithTemplate
feature along with the pattern of using the -update
flag when the file doesn't exist.
In regards to the template golden file, I would expect the process to be something like:
go test
and see it fails with the use -update flag
messagego test -update
and see the example.golden
file gets createdGolden
in the generated file with {{ .Type }}
go test
and see everything passesAt that point, everything seems good. Then we
go test -update
go test
to validateexample.golden
file is now overwritten with a single value and doesn't retain the initial {{ .Type }}
that we put in there.I can imagine that it would be relatively simple to go back and change the one overwritten .Type
variable in this case. It is a bit more complex, though, if you had a bunch of variables throughout a large .golden
file.
Do you have any thoughts on how to preserve the original template variables while doing an -update
? One idea is that on -update
, the template code could be smart enough to replace all scalar values in the data passed in with {{ .Dot.Path.To.Key }}
. That way, when .Type
writes itself out to the golden file, it'll end up writing itself out as {{ .Type }}
once again, thus preserving the template variables while updating all the other content of the golden file.
Thanks!
i have two branchs, both branch have same .golden
file.
when i switch branch, and test my project. my test FAIL,
thanks,
goldie.New(ts.T()).AssertJson(ts.T(), tt.golden, response) when rerun and diff in wsl2 show error
test.go:6747: Result did not match the golden fixture. Diff is below:
but the output is same as. When I generate or save file again it is not happen.
testdata/foo.golden
{
"foo": 1,
"bar": 3
}
foo_test.go
package main
import (
"testing"
"github.com/sebdah/goldie/v2"
)
func TestFoo(t *testing.T) {
g := goldie.New(t)
g.AssertJson(t, "something", `{"foo": 1, "bar": 2}`)
}
test failed
=== RUN TestFoo
foo_test.go:12: Result did not match the golden fixture. Diff is below:
--- Expected
+++ Actual
@@ -1,5 +1 @@
-{
- "foo": 1,
- "bar": 3
-}
-
+"{\"foo\": 1, \"bar\": 2}"
--- FAIL: TestFoo (0.00s)
Should we clean the output directory before running go test -update
? Since every time I update the test cases that changes the test cases' names, the old goldie files stay untouch, and I need to delete the files manually to keep the dir clean and clear.
Would it be possible to have a .Contains()
method that would check just part of the golden file? I have a golden file where some parts will change (like generic help text from a CLI) and all I really want to check for is "success" or similar.
I know that means its not really using the golden file but it would make the test less fragile.
Thoughts?
Something like this:
g.AssertJson(t, "test_case_23.json", jsonData)
Will result in:
Result did not match the golden fixture. Diff is below:
--- Expected
+++ Actual
@@ -1 +1,12 @@
-[]
+[
+ {
+ "Min": {
+ "X": 7,
+ "Y": 3
+ },
+ "Max": {
+ "X": 28,
+ "Y": 50
+ }
+ }
+]
For table driven tests, you can't tell which one failed. I propose adding the name
field to the assert failure logging.
Hello, I would like to ignore an id from a json struct. Is there a method to do that ? Thx !
I'm regularly using this tool for regression tests.
While using I found a typical use case that's not yet perfectly covered.
I'm often pretty printing structs to json, e.g. to regression test the output of an adapter.
It regularly happens that I have to split the output into two files and use an external tool to diff view the expected value vs. the actual value to quickly identify the change.
I'd like to add to your api another method/option to output as diff view.
Do you like to add this feature or are you open to pull requests?
It appears that in commit f0e441682ff73cf164ffd4f0cb53978f0f29a7ff
when template support was added, everything now uses template support. Also, template support breaks if you use an image, or other non-text file for your fixture. text/template.Parse()
cannot parse non text and thus fails that function before getting to bytes.Equal()
.
Minimal changes I think are to move https://github.com/sebdah/goldie/blob/master/goldie.go#L123 up above the Template code, return nil if match, and go onto the template work if not exact match? But then probably also add a PNG image template + test for a regression test?
Hi, I'm trying to install goldie to project with modules. And error about incompatible version appears. Could you help me get around this problem?
> go get github.com/sebdah/[email protected]
go: finding github.com/sebdah/goldie v2.0.0
go get github.com/sebdah/[email protected]: github.com/sebdah/[email protected]: invalid version: module contains a go.mod file, so major version must be compatible: should be v0 or v1, not v2
go version go1.13.4 darwin/amd64
Great package! Maybe it should be mentioned somewhere that the original inspiration for this idea comes from the Ruby ecosystem (which is where Mitchell took it from).
By default, this package uses the directory FixtureDir = "fixtures"
, but in Go common practice of using the name testdata
I would like to see that you have in the package by default used the dir name testdata
Examples in standard library
https://github.com/golang/go/tree/master/src/cmd/gofmt/testdata
https://github.com/golang/go/tree/master/src/encoding/json/testdata
P. S. For backward compatibility, you need to share version tags for v1.0.0 and v2.0.0 to current users of the library have not received problems.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.