Uma das minhas funções está gerando vários avisos que desejo testar testthat::expect_warning()
. Eu poderia revisar o design, mas a documentação parece dizer que isso é possível, com o argumento deprecated all
:
all
: DEPRECATED Se você precisar testar vários avisos/mensagens, agora você precisa usar várias chamadas para expect_message()/expect_warning()
(Agradecimentos aos comentários após esta resposta )
No reprex a seguir (inspirado nos exemplos do documento), devtools::check()
is passing. devtools::test()
também está passando, mas também emite avisos (quando um aviso específico é testado, o outro é lançado).
Qual é a maneira correta de testar vários avisos, já que todos são esperados?
f1.R
f1 <- function(x) {
if (x < 0) {
warning("*x* is already negative")
warning("this is a second warning")
return(x)
}
-x
}
teste-f1.R
test_that("testing", {
# Testing the function
expect_equal(f1(2), -2)
# Testing a generic warning
expect_warning(f1(-1))
# Testing the first warning
expect_warning(f1(-1), "already negative")
# Testing the second warning
expect_warning(f1(-1), "second")
# Testing no warning
expect_warning(f1(1), NA)
})
Produção relevante detest()
Correspondente aexpect_warning(f1(-1))
Aviso (teste-f1.R:5:3): testar este é um segundo aviso
Correspondente aexpect_warning(f1(-1), "already negative")
Aviso (teste-f1.R:6:3): testar este é um segundo aviso
Correspondente aexpect_warning(f1(-1), "second")
Aviso (test-f1.R:7:3): o teste x já é negativo