apitest icon indicating copy to clipboard operation
apitest copied to clipboard

The name set in apitest.New("some name") doesn't seem to show up in logging upon failure

Open archsword opened this issue 1 year ago • 6 comments

Describe the bug It seems that it is possible to set a name for a http request by calling apitest.New("some name") yet the name doesn't show up in any of the logging when there is a failure. Am I misunderstanding the use of this variable? Is there some other way to make it visible?

Steps to reproduce Use apitest.New("some name")

Expected behavior Seeing "some name" show up somewhere in the logs when there is a failure in the test.

Additional context n/a

archsword avatar Aug 31 '22 12:08 archsword

Hey @archsword. The name is currently only used in reports, e.g. sequence diagrams. I think currently you'd identify failed tests using the standard go test name. If you aren't using the sequence diagrams it's probably easier to not use the New() method, tbh it's not the nicest API and I added the Handler method later to simplify creation, e.g

apitest.Handler(handler).
		Post("/hello").
		Body(`{"a": 12345}`).
		Header("Content-Type", "application/json").
		Expect(t).
		Status(http.StatusOK).
		End()

steinfletcher avatar Sep 01 '22 09:09 steinfletcher

So, the problem is that I have is that I have many tests that do a GET to check the state, POST to change the state, and GET to confirm the state changed. And if one of the GET fails, I don't know which one it is because there is no label and they look identical. So, it means always having to lookup the line number for failing tests, which is particularly time-consuming because this is used in continuous integration tests and so I have to first find the commit it is in so that the line number is correct. If you have any recommendations, I would greatly appreciate it. This tool has been wonderful to use as supports everything I could even think of asking.

archsword avatar Sep 01 '22 14:09 archsword

@archsword thanks for more info. Understood. I'll look into it and get back to you.

If you aren't already aware, Debug() is very useful for getting some visibility into failing tests. It prints the http headers and payload of all interactions.

func TestApiTest_ResponseBody(t *testing.T) {
	apitest.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		_, _ = w.Write([]byte(`{"id": "1234", "name": "Andy"}`))
		w.WriteHeader(http.StatusOK)
	}).
		Debug().
		Get("/user/1234").
		Expect(t).
		Body(`{"id": "1234", "name": "Andy"}`).
		Status(http.StatusOK).
		End()
}

steinfletcher avatar Sep 16 '22 08:09 steinfletcher

How about something like this? It'll require a little bit of a refactor but should be possible.

Screenshot 2022-09-16 at 09 41 50

steinfletcher avatar Sep 16 '22 08:09 steinfletcher

That looks great!

archsword avatar Sep 16 '22 10:09 archsword

I have a branch with the changes if you want to take it for a spin, otherwise I think I'll look to merge it over the weekend some time.

https://github.com/steinfletcher/apitest/tree/feature/test-case-name

steinfletcher avatar Sep 16 '22 12:09 steinfletcher