简体   繁体   English

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

[英]r testthat and covr use in a non-package library

I'd like to be able to use testthat and covr in a project that is not an r package. 我希望能够在一个不是 r包的项目中使用testthatcovr In fact does not use any third party services. 实际上不使用任何第三方服务。 Just a collection of plain-old-r source files 只是一个普通的r源文件的集合

I am struggling to find out if this is possible, and if so, the instructions on how to set this is up. 我正在努力找出这是否可能,如果是这样,关于如何设置它的说明已经完成。

What I have found assumes you are writing an r package. 我发现的假设你正在写一个r包。 I like to avoid this overhead. 我喜欢避免这种开销。

Prior Art: 现有技术:

  1. https://www.rstudio.com/resources/webinars/covr-bringing-test-coverage-to-r/ 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/ https://walczak.org/2017/06/how-to-add-code-coverage-codecov-to-your-r-package/

That should be possible without problems. 这应该没有问题。

First : I have one file with code that should be tested named code.R : 第一 :我有一个代码的文件,应该测试名为code.R

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

Second : Then I have a file with tests named tests.R : 第二 :然后我有一个名为tests.R测试文件:

source("code.R")

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

Third : And then you can run tests as well as coverage like this: 第三 :然后你可以像这样运行测试和覆盖:

library(testthat)
library(covr)

test_file("tests.R")

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

Multiple files should be no problem. 多个文件应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM