我有一个使用testthat的小测试程序
library(testthat)
source("src/MyFile.r")
results <- test_dir("tests", reporter="summary")所以,我是通过Rscript运行这个的。问题是,即使有测试失败,退出代码也始终为0。因此,如果有任何故障,我想打电话给stop。但我似乎找不到合适的代码来做到这一点。在results中是否有我应该查看的方法或字段来确定是否有任何错误?
发布于 2017-08-25 17:10:39
目前,我的解决方案正在迭代这样的结果:
for (i in 1:length(results)) {
if (!is(results[[i]]$result[[1]], "expectation_success")) {
stop("There were test failures")
}
}发布于 2020-04-03 13:39:19
您还可以调整jamesatha的答案以检索测试失败的次数。
failed.tests <- sapply(results, function(r) {
!is(r$result[[1]], "expectation_success")
})然后,您允许您失败,就像以前一样:
if (any(failed.tests)) {
stop("There were test failures")
}或者做一些更有针对性的事情
if (any(failed.tests)) {
failed.test.count <- length(which(failed.tests))
stop(paste(failed.test.count,"failed tests is",failed.test.count,"too many!")
}发布于 2019-05-13 10:13:15
您可以简单地将stop_on_failure=TRUE作为参数传递给test_dir。如果您有任何测试失败,它将引发一个错误并退出非零。
例如:
results <- test_dir("mypath", stop_on_failure=TRUE)这是记录在案的这里
https://stackoverflow.com/questions/45870687
复制相似问题