简体   繁体   English

如何使用testthat测试已安装的软件包?

[英]How to test an installed package with testthat?

I would like to test an installed package, but this returns an error. 我想测试一个已安装的软件包,但这会返回错误。

library(testthat)
test_package("testthat")
# Error: No tests found for testthat

test_package ( source here ) returns this error because system.file("tests", package = package) is empty. 由于system.file("tests", package = package)为空system.file("tests", package = package)因此system.file("tests", package = package) 此处的源 )返回此错误。 In fact, the tests directory is missing from the package installed. 实际上,安装的软件包中没有tests目录。

list.dirs(system.file("", package = "testthat"))
# [1] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat/"     
# [2] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat//help"
# [3] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat//html"
# [4] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat//libs"
# [5] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat//Meta"
# [6] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat//R"  

How to install a package so that its tests directory remains present? 如何安装软件包,使其测试目录仍然存在?

If the author chooses not to put the tests in the inst/ directory, then they will not be installed with the package and you cannot run the tests via the installed package. 如果作者选择不将测试放在inst/目录中,那么这些测试包将不会与软件包一起安装,并且您无法通过已安装的软件包运行测试。

So there's nothing you can do, short of modifying the source package and re-installing. 因此,除了修改源程序包并重新安装之外,您无能为力。 But at that point, you might as well just run the tests on the source package. 但是到那时,您最好只对源包运行测试。

You can test packages with 您可以使用

tools::testInstalledPackage("package")

But I think it just works only if the tests are in inst/ 但是我认为只有在inst /

There is also 也有

install.packages("testthat", INSTALL_opts = "--install-tests")

to install also the tests with the package. 还将测试与软件包一起安装。 But also just works if test are in inst/ 但是如果test在inst /

So probably best you download the source package and run: 因此,最好下载源代码包并运行:

devtools::test()

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

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