繁体   English   中英

`test_that` 中的 `expect_error` 不起作用

[英]`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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM