Join GitHub today
Unit tests should use local client instead of package-global client #762
In #732 it became clear that having a package-global
client, teardown := setup() defer teardown()
and the global
This would make for a very large PR, but could possibly be done with the assistance of
This would also be a great project for a new contributor to the repo. Let it be known that all help is greatly appreciated! Thank you in advance.
referenced this issue
Oct 27, 2017
@gmlewis I don't think it'll be sufficient to have
Edit: Actually, I think only
But we can't reuse the package scoped
Makes sense, thanks for input @shurcooL. From a quick check, there does actually seem to be a couple of tests using the global
So in order to not change any of the tests, we would have setup() always return the four things (I like explicitly returning teardown rather than just having every test call server.Close):
client, mux, server, teardown := setup()
Or we could do something like (naming tbc):
client, mux, globalConfig, teardown := setup()
where global config contains configuration details that might be needed in tests - i.e. the server base URL value.
How about this:
server, mux, client := setup() defer server.Close()
Since everything that
Edit: I missed this thing you said:
Is the server base URL value the only thing used by tests? If so, maybe just return that url instead of globalConfig?
That's true. Although I quite liked that it is explicit, and that to "forget" to teardown I have to purposefully ignore the variable and use
That said, I imagine almost all tests written will just copy a previous one, which will have the code you mentioned in it. So in practice I'm not sure how much of a problem that is.
Ah, sorry, I replied before I saw your updated comment!
Yes, that sound like a good approach. So we're now at:
serverBaseURL, mux, client, teardown := setup() defer teardown()
...giving the tests what they need and not over complicating when we only need that one value at the moment.