I'm trying to find a way to create a test table from golden and input files.
I have the following dir structure:
pkg
|
| mypackage/
| testdata/
| file1.input
| file1.golden
| file2.input
| file2.golden
| mypackage_test.go
| ...
I'm trying to find an efficient way to create a test table from a pair of input and golden files:
type test struct {
input,
expected string
}
func getFiles() test {
input, _ := ioutil.ReadFile(filepath.Join("testdata", fmt.Sprintf("%s.%s", filename, "input")))
expected, := ioutil.ReadFile(filepath.Join("testdata", fmt.Sprintf("%s.%s", filename, "golden")))
return {string(input), string(expected)}
}
func getTable() []test {
return []test{getFiles("file1"), getFiles("file2"), getFiles("file3")}
}
func TestInputs(t *testing.T) {
table := getTable()
for _, tt := range table {
// perform the test
}
}
I'm not really the best at writting tests, and the code above seems a bit hacky. Is there a better way to do what I'm trying?