【发布时间】:2021-06-07 09:41:11
【问题描述】:
为了开发一个包,我使用testthat。我想测试我的函数是否发送正确的消息和警告。我想同时测试函数的结果。这就是我正在尝试的方式:
## R/foo.r
foo <- function() {
message("This is a message")
warning("This is a warning")
"This is a result"
}
我编写了这些测试:
## tests/testthat/test-foo.R
test_that("foo works", {
expect_equal(foo(), "This is a result")
expect_message(foo(), "This is a message")
expect_warning(foo(), "This is a warning")
})
但是现在当我运行测试时,我有这个:
Loading testWarnMess
Testing testWarnMess
v | OK F W S | Context
/ | 0 | foo This is a message
v | 3 2 | foo [0.3 s]
----------------------------------------------------------------------------------------------
Warning (test-foo.R:2:3): foo works
This is a warning
Backtrace:
1. testthat::expect_equal(foo(), "This is a result") test-foo.R:2:2
4. testWarnMess::foo()
Warning (test-foo.R:3:3): foo works
This is a warning
Backtrace:
1. testthat::expect_message(foo(), "This is a message") test-foo.R:3:2
7. testWarnMess::foo()
----------------------------------------------------------------------------------------------
== Results ===================================================================================
Duration: 0.3 s
[ FAIL 0 | WARN 2 | SKIP 0 | PASS 3 ]
This is a message
当我预期它们会发生时,如何防止在测试结果中看到消息和警告?
【问题讨论】: