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
Set reproducibility options in test_that() #1054
Conversation
devtools::test() was already doing this so it seems unlikely to affect many existing tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My main meta thought is: what if you have additional test config that is unique to your package? But applies broadly across your tests.
Is there any way to register your own options or env vars to set and restore?
In usethis, for example, that would potentially apply to options(usethis.quiet = TRUE)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
One thing to think about is whether we want to handle this from the other end. I.e. should cli notice that it is running in testthat, and then modify its output accordingly?
Alternatively, should we support a hook, so packages can add their own options or callbacks that are set or called by test_that()
? We would do this in addition to the currently set options and env vars. We don't need to do this in this PR, but in the long run it could be a more sustainable solution I think.
EDIT: I can see now that @jennybc brought up the same thing. :)
testthat cannot provide an API for this, because you would not want to import testthat. So yeah, one solution would be to set options with a specific name, and a setup and a restore function. E.g. usethis would do something like this in options(
testthat_context_usethis = list(
setup = function(.env) {
options(usethis.quiet = TRUE)
},
restore = function(.env, old) {
options(old)
}
)
) testthat would get all the |
Packages can already uses the TESTTHAT env var to condition behaviour based on whether or not testthat is running. Do we need something more? |
I guess it's a question of where to put the instructions to do THIS not THAT when testing. If there were a way to register package-specific test state instructions, then they are all centralized in, e.g., If we're supposed to consult the env var I feel like centralizing them makes them much more visible and maintainable. |
I don't see how we can make a registry work when you are running the tests interactively. That would force you to also run |
Maybe. For the other package, I think just setting an option is easier. But we don't have to rush this now I think. |
Yes, when you run parts of a
I agree that using this well needs some discipline from the 3rd party packages. But if we don't have a hook, then we need to collect all such information in testthat, which also does not seem right. (Although the |
Yeah, I agree. The alternative is to introduce a helper function into your package (usethis), that queries E.g cli already has this for console with, so I'll only need to update the |
Hmmm I just noticed that |
Yeah, I think just having a helper that checks for |
One more point in favour of doing this in one place — it's nice to know that you can load at |
Yeah, we want to have it in one place as well, I think. That's actually the motivation for the API in #1054 (comment) With that, for a given If packages will change their behavior based on So in this respect the API is better I think. But I am not entirely convinced if it is needed, or it is too much. |
Fixes #1044. Fixes #971.