Skip to content
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

Setup and teardown functions #628

Closed
hadley opened this issue Oct 1, 2017 · 3 comments
Closed

Setup and teardown functions #628

hadley opened this issue Oct 1, 2017 · 3 comments
Labels
feature a feature request or enhancement fixtures 🏭

Comments

@hadley
Copy link
Member

hadley commented Oct 1, 2017

Would be run before/after each test.

Do we also need per-file setup and teardown?

@hadley hadley added feature a feature request or enhancement fixtures 🏭 labels Oct 1, 2017
@gaborcsardi
Copy link
Member

I think we could start with something that runs before all tests (which we almost have with helper-, barring load_all() behavior), and something that runs after all tests.

I.e. sg. like creating a test db, and then cleaning it up.

Per-file setup can be useful as well, I can see that. Maybe you can already do this, actually, by simply putting the code at the beginning of the file, and using on.exit(). AFAIR this works when the tests are running via test_file(). But it would be nice to formalize / document this.

I am not sure if per-block setup would be useful, I think you can just manually include that, and I don't see a situation where setup/teardown is needed for every block.

@hadley
Copy link
Member Author

hadley commented Oct 2, 2017

See #589 for code run before/after all tests.

For per-file setup & teardown, maybe we could start with simple definitions:

setup <- function(x) invisible(x)

teardown <- function(x) {
  # withr trick to call on.exit in parent enviroment
}

That would provide a nice set of conventions which we could extend with more sophisticated implementations if needed the future.

@r-lib r-lib deleted a comment from gaborcsardi Oct 2, 2017
@hadley
Copy link
Member Author

hadley commented Oct 3, 2017

I think setup() should probably behave like local (so there's some point in using it)

But that leads to code that looks like this:

tmp <- tempfile()
setup(writeLines(tmp, "some test data"))
teardown(unlink(tmp))

I'm not sure if that's good idea or not.

@hadley hadley closed this as completed in 8c5f4bc Oct 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement fixtures 🏭
Projects
None yet
Development

No branches or pull requests

2 participants