Documentation
¶
Overview ¶
Package golden provides standard way to write tests with golden files.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EqualString ¶
EqualString asserts that the golden file content is equal to the data in string format.
func File ¶
File returns the golden file content for the test. If OVERRIDE_TEST_DATA env is set to true, the golden file will be created with the content of the data. OVERRIDE_TEST_DATA is read only once at the start of the test and it's value is not updated. Depending of the test structure the golden file and it's directories arew created in ./testdata/{testFuncName}/{subTestName}.golden or ./testdata/{testFuncName}/{testFuncName}.golden.
func FileString ¶
FileString returns the output of golden.File as a string.
func Request ¶ added in v2.0.3
Request sends the request and asserts that the response status code is equal to the expectedStatusCode. It also asserts that the response body is equal to the golden file content using EqualString. Example test function:
func TestAPI(t *testing.T) { tests := []struct { name string method string path string body io.Reader expectedCode int }{ { name: "create user", method: "POST", path: "/api/v1/user", body: strings.NewReader(`{"name": "someone"}`), expectedCode: 200, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { req := must.NewRequest(t, tt.method, "http://127.0.0.1:8080"+tt.path, tt.body) golden.Request(t, http.DefaultClient, req, tt.expectedCode) }) } }