gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:09:56 25 4
gpt4 key购买 nike

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

多个文件应该没问题。

关于r testthat 和 covr 在非包库中的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48637143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com