[英]How to make a test fail with the testthat package?
I have a test for which if the prerequisites are not met (eg, missing file or something) I would like to make it fail. 我有一个测试,如果不满足先决条件(例如,丢失文件或某物),我想使其失败。
Just for clarification, here's an example I'd like to do: 只是为了澄清,这是我想做的一个例子:
test_that("...", {
if ( ... precondition to execute the test is not met... ) {
expect_true(FALSE) # Make it fail without going further
}
expect_that( ... real test here ...)
})
Now my question is: Is there any fail()
-like expectation in the testthat package or I have to write expect_true(FALSE)
all the time? 现在我的问题是:在testthat包中是否有任何fail()
类的期望,还是我必须一直写expect_true(FALSE)
?
There isn't a fail
function in testthat
at the moment. 没有一个fail
的功能testthat
的时刻。 I think you want something like 我想你想要类似的东西
fail <- function(message = "Failure has been forced.", info = NULL, label = NULL)
{
expect_that(
NULL,
function(message)
{
expectation(FALSE, message)
},
info,
label
)
}
Usage is, for example, 用法是,例如
test_that("!!!", fail())
Failure is not an option... 失败不是一种选择...
Try using stop
: 尝试使用stop
:
test_that("testingsomething", {
if(file.exists("foo.txt")){
stop("foo.txt already exists")
}
foo = writeFreshVersion("foo.txt")
expect_true(file.exists("foo.txt"))
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.