Snapshots
Snapshot allows to compare the full body of a response with the content of a file.
They are declared by test.ExpectedBody.Snapshot() within the expectations of an HTTP response.
Create a snapshot
Save the expected content within tests/snapshots/:
tests/
├── index.gox
└── snapshots/
└── index.html The file must contain exactly the expected body, including spaces and line leaps.
Snapshot
expected := test.NewResponseExpected{
Status: 200,
Body: test.ExpectedBody.Snapshot("index.html"),
} The relative routes are resolved within the tests/snapshots/. They are also accepted snapshots/index.html and tests/snapshots/index.html without duplicating those folders.
Comparison
The matcher makes an exact comparison between the file and the body received. If they do not match, the result retains the first difference and test.Explain() It shows its location.
If the file does not exist, the expectation fails and identifies the route GOX tried to load.
Save responses
test.Save(result) generates artifacts with captured responses. Results to know its location and format.
These artifacts can be used as a reference when reviewing a change, but GOX does not automatically replace existing snapshots.
Snapshots and benchmarks
The comparisons with snapshots can be part of the pipeline executed by a Benchmark. However, test.Save() does not generate response files in Benchmark mode to avoid mixing functional artifacts with metrics.
Best practices
- It uses snapshots only for complete and stable bodies.
- Keep names related to the route or case tested.
- Check every change in the snapshot before you accept it.
- It uses partial matchers for dynamic content.
- Do not depend on the automatic generation to replace existing expectations.