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

r testthat and covr use in a non-package library

我希望能够在不是 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/

这应该没有问题。

第一个:我有一个文件,其中包含应测试的代码,名为 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)

多个文件应该没问题。