Is there a way to skip a test if another test failed before? I use a standard setup for testing a package with devtools/testthat and one function is dependent on another function. So, if a test for this function failed, I want to skip the test for the function that is dependent on it.
I tried wrapping the first test in a tryCatch()
and skipping the second test if the first test exits with an error, i.e.
context('This is a test file with two tests')
skip_test = FALSE
tryCatch({
test_that('This is a test that fails', {
fail()
})
}, error = function(e) {
print(e)
skip_test <<- TRUE
})
test_that('This test is skipped if the first test failed', {
skip_if(skip_test, 'The first test was unsuccessful')
expect_equal(1+1, 2)
})
skip_test is set to TRUE globally, because otherwise it is only changed in the environment of the first test_that()
. This approach works when directly executing the code in the console, as a failed test returns an error when used in the console (see documentation of test_that()
).
However, when testing the whole package via the devtools::test()
function, the failed test does not seem to return an error. This means that it is not possible to detect failed tests with my approach.
Is there another way to skip tests of dependent functions?
Aucun commentaire:
Enregistrer un commentaire