-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit tests should use local client instead of package-global client #762
Comments
I decided to have a go at this issue, sorry I forgot to comment here first. This was quite a nice one to do as a new contributor :) |
@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. Any thoughts? |
How about this: server, mux, client := setup()
defer server.Close() Since everything that Edit: I missed this thing you said:
Hmm... 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. |
This refactor removes the global test variables used by all tests, replacing them with local variables that are independent in each test. This is better style, easier to read and be confident about correctness, and allows tests to be parallelized in the future. Resolves google#762.
In #732 it became clear that having a package-global
client
for unit testing will prevent the unit tests from being run in parallel in the future. Ideally, each test will have its own client... something like this:and the global
client
ingithub_test.go
will be removed.This would make for a very large PR, but could possibly be done with the assistance of
gofmt -r
or your favorite text editor. 😄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.
The text was updated successfully, but these errors were encountered: