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

How to handle quit in tested method #1905

Closed
alexander793 opened this issue Nov 28, 2023 · 1 comment
Closed

How to handle quit in tested method #1905

alexander793 opened this issue Nov 28, 2023 · 1 comment

Comments

@alexander793
Copy link

Hi there,
I have some methods that return a certain error code with quit(status = error_code) and I would like to test that these methods return exactly the expected codes.
When running such a method inside a testthat test, the R terminal quits.

Is there an expect-method to test that a method returns a certain exit code?

Example:
To be tested method bar in file foo.R

bar <- function() {
  print("Do something...")
  quit(status = 1)
}

Desired Test:

testthat("bar returns exit code 1", {
  expect_quit_status(bar(), 1)
})
@hadley
Copy link
Member

hadley commented Nov 28, 2023

I'd strongly recommend against calling quit() in a function since this is rather hostile to your user. If you really do want to do this, then you could use local_mocked_binding() to temporarily make quit() do something other than quitting.

@hadley hadley closed this as completed Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants