datacamp / testwhat

Write Submission Correctness Tests for R exercises
https://datacamp.github.io/testwhat
GNU Affero General Public License v3.0
33 stars 24 forks source link

Document examples of checking for warnings or errors being thrown #211

Open richierocks opened 5 years ago

richierocks commented 5 years ago

It just took me a while to figure out the best way to test that a particular warning or error was thrown, so that means we need an example in the docs. (Same for other *what docs.)

I think that the best way is to use check_output(), including a Warning message: or Error: prefix.

Solution

warning("this is a warning")
stop("this is an error")

SCTs

ex() %>% check_output("Warning message: this is a warning", fixed = TRUE)
ex() %>% check_output("Error: this is an error", fixed = TRUE)

If you can distinguish output that went to stderr() rather than stdout(), you could maybe add an argument to check_output() to filter for just warnings and errors, but that may be overkill.