我想使用 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.xml
和 coverage.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 settype = "none"
and pass the code to run as a character vector to thecode
parameter
covr::package_coverage(
type = "none",
code = "testthat::test_package(
'myPackage',
reporter = testthat::JunitReporter$new(file = 'test-results.xml')
)")
关于r - 一次测试它和 covr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50021412/