r-失败时报告test_that块中的额外信息



我想在测试失败的情况下向控制台cat()一些信息(我确信这不会发生,但我无法证明它不会发生),这样我就可以调查这个问题。

现在我有了大致如下的代码:

testthat::test_that('Maybe fails', {
seed <- as.integer(Sys.time())
set.seed(seed)

testthat::expect_true(maybe_fails(runif(100L)))
testthat::expect_equal(long_vector(runif(100L)), target, tol = 1e-8)
if (failed()) {
cat('seed: ', seed, 'n')
}
})

不幸的是,failed()并不存在。

expect_*()的返回值似乎没有什么用处,它们只是返回实际的参数。

我正在考虑使用all.equal()再次检查,但这是一个非常丑陋的重复。

您可以将testthat管理的info参数及其reporters用于所有expect函数,而不是使用cat(出于兼容性原因保留参数):

library(testthat)
testthat::test_that("Some tests",{

testthat::expect_equal(1,2,info=paste('Test 1 failed at',Sys.time()))
testthat::expect_equal(1,1,info=paste('Test 2 failed at',sys.time()))
})
#> -- Failure (<text>:5:3): Some tests --------------------------------------------
#> 1 not equal to 2.
#> 1/1 mismatches
#> [1] 1 - 2 == -1
#> Test 1 failed at 2021-03-03 17:25:37

最新更新