https://github.com/iwdgo/testingfiles
Using files to test large output
https://github.com/iwdgo/testingfiles
go golang reference-files testing testing-tools
Last synced: 11 months ago
JSON representation
Using files to test large output
- Host: GitHub
- URL: https://github.com/iwdgo/testingfiles
- Owner: iwdgo
- License: gpl-3.0
- Created: 2019-04-06T07:18:47.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2025-01-06T20:56:10.000Z (over 1 year ago)
- Last Synced: 2025-01-06T21:29:36.289Z (over 1 year ago)
- Topics: go, golang, reference-files, testing, testing-tools
- Language: Go
- Homepage: https://pkg.go.dev/github.com/iwdgo/testingfiles
- Size: 95.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://pkg.go.dev/github.com/iwdgo/testingfiles)
[](https://goreportcard.com/report/github.com/iwdgo/testingfiles)
[](https://codecov.io/gh/iwdgo/testingfiles)
[](https://cirrus-ci.com/github/iwdgo/testingfiles)
[](https://ci.appveyor.com/project/iwdgo/testingfiles)
[](https://github.com/iwdgo/testingfiles/actions/workflows/go.yml)
# Using reference files for large output
A `want` reference file is compared to data from a `got` source.
Comparison is provided for `File`, `Buffer` or `ReadCloser` where a file is the least efficient.
When comparison fails, a file is created with `got_` prefix from the byte where the first difference
appeared. No further check on the file is done.
When running tests for the first time, they might fail as no `want` file is usually available.
The produced `got` file can be renamed into a `want` file to have a second successful run.
### Offline test
```
func TestOffline(t *testing.T) {
if err := tearDownOffline(handler(...), t.Name()); err != nil {
t.Error(err)
}
}
func tearDownOffline(b *bytes.Buffer, s string) (err error) {
if b == nil {
return errors.New("bytes.Buffer is nil")
}
testingfiles.OutputDir("output")
if err := testingfiles.BufferCompare(b, s); err != nil {
return err
}
return nil
}
```
### Online test
```
func TestOnline(t *testing.T) {
resp, err := http.Get(getAppUrl("").String())
if err != nil {
t.Fatal(err)
}
tearDown(t, resp)
}
func tearDown(t *testing.T, resp *http.Response) {
if resp == nil {
t.Fatal("response is nil")
}
if resp.StatusCode != 200 {
t.Fatalf("request failed with error %d for %s", resp.StatusCode, s)
}
testingfiles.OutputDir("output")
if err := testingfiles.ReadCloserCompare(resp.Body, t.Bame()); err != nil {
t.Error(err)
}
}
```
## Working directory
Reference files are expected to reside in a working directory which defaults to `./output`.
Using a subdirectory avoids having the data files mixed with source code.
The directory is not created but its existence is checked.
If the working directory is unavailable, tests will panic.
In CI scripts, the working directory is created before running the tests.
## Testing of the module
Testing can be online or offline.
Online is used to read a reference page.
Offline requires to provide the reference file.
# Common files
A first method allows to extract common lines between files selected using a glob pattern.
A second method removes common lines from each file.
```
// Extract all common features from Linux ports
p := filepath.Join(os.Getenv("GOROOT"), "/api/refs")
gf := "z_syscall_linux_*.txt"
baseline := "z_syscall_linux.txt"
if err := ExtractCommon(p, gf, baseline); err != nil {
log.Fatal(err)
}
if err := CreateSupplements(p, gf, baseline); err != nil {
log.Fatal(err)
}
```
# Good to know
Other examples are available in modules of [largeoutput](https://github.com/iwdgo/largeoutput) repository.
Benchmarking between string and bytes.Buffer is inconclusive inline with documented behavior.
```
go version go1.13beta1 windows/amd64
pkg: github.com/iwdgo/testingfiles
BenchmarkGetPageStringToFile-4 1 1029407400 ns/op
BenchmarkGetPageBufferToFile-4 1 1108362700 ns/op
BenchmarkGetPageBufferCompare-4 2 512716000 ns/op
BenchmarkGetPageReadCloserCompare-4 3 545020000 ns/op
```