r testthat和covr在非包装库中使用

时间:2018-02-06 06:58:10

标签: r testthat covr

我希望能够在 r包的项目中使用testthatcovr。实际上不使用任何第三方服务。只是一个普通的-r源文件集合

我正在努力找出这是否可能,如果是的话,关于如何设置这个的说明已经完成。

我发现的假设你正在写一个r包。我喜欢避免这种开销。

现有技术:

  1. https://www.rstudio.com/resources/webinars/covr-bringing-test-coverage-to-r/
  2. https://walczak.org/2017/06/how-to-add-code-coverage-codecov-to-your-r-package/

1 个答案:

答案 0 :(得分:2)

这应该没有问题。

首先:我有一个文件包含应该测试的名为code.R的代码:

f1 <- function(n, ...) {
    rnorm(n = n, ...)
}

第二次:然后我有一个名为tests.R的测试文件:

source("code.R")

test_that("Random tests", {
    tmp1 <- f1(10)
    expect_type(tmp1, "double")
    expect_equal(length(tmp1), 10)
})

第三次:然后你可以运行测试以及这样的覆盖:

library(testthat)
library(covr)

test_file("tests.R")

res <- file_coverage("code.R", "tests.R")
res
report(res)

多个文件应该没问题。