[英]`expect_error` inside `test_that` does not work
如果我运行以下,行为是预期的。 什么都没有返回
expect_error(expect_true(FALSE))
当我运行相同的代码时,但包裹在test_that
test_that("expect_error should not fail",{
expect_error(expect_true(FALSE))
})
Error: Test failed: 'expect_error should not fail'
* Not expected: FALSE isn't true.
* Not expected: expect_true(FALSE) code did not generate an error.
输出很奇怪,这意味着在test_that
折叠时expect_true(FALSE)
不会产生错误。
如何将expect_error
放入test_that
?
更新
我使用testthat
进行代码断言。 有包assertthat
。
你误解了expect_error
的意思。 它应该捕获用户代码中的错误,而不是测试中的错误。
expect_that
会产生一个错误,但是这个错误会在test_that
内部处理,然后expect_error
才有机会处理它。
不要在expect_error
内包装expect_*
调用。 直接使用expect_error
有效:
test_that('expect_error catches error conditions', {
expect_error(stop('foo'))
})
行为的不一致来自这样一个事实,即test_that
将捕获由expect_*
生成的错误,而不是让它们正常传播。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.