I have a package with a suggested dependency enabling optional behaviour.
Following the best practice, I added this dependency in my "Suggests" and check if it's installed with requireNamespace
before using it.
In this particular case, the suggested package is data.tree
and the optional behaviour is making a pretty print instead of a more boring one.
myfun <- function(...) {
if (requireNamespace("data.tree", quietly = TRUE)) {
# do pretty print
} else {
# do boring print
}
}
My question is: how do I test this behaviour with testthat
? I want to test both branches of the if/else
, but since I do have data.tree installed, I only ever run the first branch.
test_that("myfun does a pretty print with data.tree", {
myfun()
})
test_that("myfun does a boring print if data.tree is not installed", {
# Ideally I am looking for a bit of code like this:
with(simulate_missing("data.tree",
myfun())
})
Is there a way to simulate a missing dependency with testthat
, or more generally a way to test myfun
?
Aucun commentaire:
Enregistrer un commentaire