簡體   English   中英

抑制 test_that output“測試通過:)”

[英]Suppress test_that output "Test passed :)"

R 中的 test_that package(版本 3.0.2)的更新導致默認 output 用於通過測試:

考試通過了:)

這最初很可愛,但在 model 中很煩人,它每次迭代都會執行數千次測試。

這個消息怎么能被屏蔽?

一種選擇是將消息提取為字符串並將其轉移到 object。 我們可以使用capture.output

out <- capture.output(test_that("trigonometric functions match identities", {
 expect_equal(sin(pi / 4), 1 / sqrt(2))
expect_equal(cos(pi / 4), 1 / sqrt(2))
expect_equal(tan(pi / 4), 1)
}))

但是抑制消息的功能仍會打印消息

在此處輸入圖像描述


我們可以在使用grep捕獲后設置條件打印與否

f1 <- function(x) if(any(!grepl('passed', x))) cat(x, sep='\n')

out1 <- capture.output(test_that("trigonometric functions match identities", {

  expect_equal(sin(pi / 4), 1 / sqrt(2))
  expect_equal(cos(pi / 4), 1 / sqrt(2))
  expect_equal(tan(pi / 4), 1)
  expect_equal(sin(pi / 4), 1)

   }))

f1(out) # nothing prints
f1(out1)
#── Failure (???): trigonometric functions match identities ─────────────────────
#sin(pi/4) not equal to 1.
#1/1 mismatches
#[1] 0.707 - 1 == -0.293

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM