[英]R testthat: use external package only in test file - not in DESCRIPTION
I'm trying to run a testthat
script using GitHub Actions.我正在尝试使用 GitHub 操作运行
testthat
脚本。
I would like to test a functionality of my function that allows it to be combined with (many) external packages.我想测试我的函数的一个功能,它允许它与(许多)外部包结合使用。 Now I want to test these external packages for the
R CMD Check
but I don't want to load the external packages generally (ie putting them into the Description) - after all, most people will not use these external packages.现在我想为
R CMD Check
测试这些外部包,但我一般不想加载外部包(即把它们放在描述中)——毕竟,大多数人不会使用这些外部包。
Any ideas how to just include an external package in the testing files but not in the DESCRIPTION?任何想法如何只在测试文件中包含一个外部包而不是在描述中?
Thanks!谢谢!
I think you describe a very standard use of Suggests
.我认为您描述了
Suggests
的非常标准的用法。
I see two related but separable issues:我看到两个相关但可分离的问题:
You want to test something using CI, in this case GHA.您想使用 CI 测试某些内容,在本例中为 GHA。 That is fine.
那没关系。 Because you control the execution of the code, you could move your code from the test runner to, say,
inst/examples
and call it explicitly.因为您控制代码的执行,所以您可以将您的代码从测试运行器移动到
inst/examples
并显式调用它。 That way the standard check of 'is the package using undeclared code' passes as inst/examples
is not checked这样,“是否使用未声明代码的包”的标准检查作为
inst/examples
未检查通过
You want to not force other people to have to load these packages.您不想强迫其他人必须加载这些包。 That is fine too, and we have
Suggests:
for this!这也很好,我们有
Suggests:
为此! Read Section 1.1 of Writing R Extensions about all the detailed semantics.阅读有关所有详细语义的“编写 R 扩展”的第 1.1 节。 If your package invokes other packages via tests, the every
R CMD check
touches this (and the external packages) so they must be declared.如果您的包通过测试调用其他包,则每个
R CMD check
涉及此(以及外部包),因此必须声明它们。 But you already know that only "some" people will want to use this "some of the time": that is precisely what Suggests:
does, and you bracket the use with if (requireNamespace(pkgHere, quietly=TRUE))
.但是您已经知道只有“某些”人会想要在“某些时候”使用这个:这正是
Suggests:
所做的,并且您将使用if (requireNamespace(pkgHere, quietly=TRUE))
起来if (requireNamespace(pkgHere, quietly=TRUE))
。
You can go either way, or even combine both.您可以采用任何一种方式,甚至可以将两者结合起来。 But you cannot call packages from tests and not declare them.
但是你不能从测试中调用包而不声明它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.