【问题标题】:How to tell if any tests failed from result of testthat::test_dir如何从 testthat::test_dir 的结果中判断是否有任何测试失败
【发布时间】:2018-02-02 20:55:12
【问题描述】:

我有一个使用testthat的小测试程序

library(testthat)
source("src/MyFile.r")
results <- test_dir("tests", reporter="summary")

所以,我通过Rscript 运行它。问题是即使测试失败,退出代码也始终为 0。所以,如果有任何故障,我想打电话给stop。但我似乎无法获得正确的代码来做到这一点。 results 中是否有我应该查看的方法或字段以确定是否有任何错误?

【问题讨论】:

    标签: r testing testthat


    【解决方案1】:

    目前,我的解决方案正在迭代这样的结果:

    for (i in 1:length(results)) {
      if (!is(results[[i]]$result[[1]], "expectation_success")) {
        stop("There were test failures")
      }
    }
    

    【讨论】:

      【解决方案2】:

      您可以简单地将stop_on_failure=TRUE 作为参数传递给test_dir。如果您遇到任何测试失败,它将引发错误并退出非零。

      例如:

      results <- test_dir("mypath", stop_on_failure=TRUE)
      

      这是记录在here

      【讨论】:

        【解决方案3】:

        您还可以调整 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!")
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-02-15
          • 2012-09-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-31
          • 2015-01-11
          • 2019-05-29
          相关资源
          最近更新 更多