一次测试它和 covr

Testthat and covr in one go

我想 运行 使用 testthat 测试包并使用 covr 计算代码覆盖率。此外,testthat 的结果应以 JUnit XML 格式保存,covr 的结果应以 Cobertura 格式保存。

下面的代码可以解决问题(当 getwd() 是包的根目录时):

options("testthat.output_file" = "test-results.xml")
devtools::test(reporter = testthat::JunitReporter$new())

cov <- covr::package_coverage()
covr::to_cobertura(cov, "coverage.xml")

但是,测试执行了两次。一次 devtools::test,一次 covr::package_coverage

我的理解是 covr::package_coverage 执行测试,但不产生 test-results.xml

如标​​题所示,我想通过一次测试套件的执行同时获得 test-results.xmlcoverage.xml

来自 covr 参考手册 (https://cran.r-project.org/web/packages/covr/covr.pdf)

This function uses tools::testInstalledPackage() to run the code, if you would like to test your package in another way you can set type = "none" and pass the code to run as a character vector to the code parameter

covr::package_coverage(
    type = "none",
    code = "testthat::test_package(
'myPackage',
reporter = testthat::JunitReporter$new(file = 'test-results.xml')
)")