h2non/baloo

Improvement: (optionally) print response body if assertion fails

Opened this issue · 0 comments

zak905 commented

An issue that has been bothering me for a while is that when a status assertion (it can be other assertion as well) fails, the response is not printed. Most of the time (at the least in the case of Rest APIs), the response contains a hint about why the status assertion fails, and it makes it easy to debug when the response is printed. Currently, I need to add parse and print response code everytime I need it, and it gets more annoying when tests are running in the CI. I am aware of the plugin https://github.com/izumin5210/gentleman-logger, but with the plugin the response is printed in all cases (and also it seems very old and not maintained). Example:

func TestXxx(t *testing.T) {
	baloo.New("https://httpbin.org").Post("/anything").BodyString(`{"error":"db connection failed"}`).
		Expect(t).Status(500).Done()
}

The assertion error in this case is Unexpected status code for: POST https://httpbin.org/anything => 200 != 500

To be able to see the error, one has to add some boilerplate code:

func TestXxx(t *testing.T) {
	resp, _ := baloo.New("https://httpbin.org").Post("/anything").BodyString(`{"error":"db connection failed"}`).
		Expect(t).Status(500).Send()
	fmt.Println(resp.String())
}

It would be nice if this is provided out of the box, and only when the assertion fails.