繁体   English   中英

失败时报告来自 test_that 块的额外信息

[英]Report extra information from a test_that block when failing

在测试失败的情况下,我想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()再次检查,但这是一个非常丑陋的重复。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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