簡體   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