在devtools :: check中失败的测试,但在devtools :: test中工作 [英] testthat fails within devtools::check but works in devtools::test
问题描述
有什么方法可以重现devtools::check
使用的环境吗?
Is there any way to reproduce the environment which is used by devtools::check
?
我的测试可以在devtools::test()
上使用,但在devtools::check()
上失败.我的问题是现在,如何找到问题. check
的报告仅显示错误日志的最后几行,而我找不到用于测试的完整报告.
I have the problem that my tests work with devtools::test()
but fail within devtools::check()
. My problem is now, how to find the problem. The report of check
just prints the last few lines of the error log and I can't find the complete report for the testing.
checking tests ... ERROR
Running the tests in ‘tests/testthat.R’ failed.
Last 13 lines of output:
...
我知道check
使用的环境不同于test
,但是我不知道如何调试这些问题,因为这些问题根本无法再现.特别是这些测试是在几个月前在哪里运行的,因此不确定在哪里查找问题.
I know that check
uses a different environment compared to test
but I don't know how I should debug these problems since they are not reproducible at all. Specially these test where running a few month ago, so not sure where to look for the problem.
编辑
实际上,我试图找到问题所在,并且找到了解决方案.但是要发布我的解决方案,我必须添加更多详细信息.
actually I tried to locate my problem and I found a solution. But to post my solution to it, I have to add more details.
因此,我的测试始终失败,因为我正在测试Markdown脚本(如果运行时没有错误),之后我要检查某些环境变量是否设置正确.这些是我使用脚本计算的结果以及我设置的标准设置.因此,如果我在开发后忘记更改某些设置,我想得到一个警告.
So my test always failed since I was testing a markdown script if it is running without errors and afterwards I was checking if some of the environmental variables are set correctly. These where results which I calculate with the script as well as standard settings which I set. So I wanted to get a warning if I forgot to change some of my settings after developing...
无论如何,由于它是一个降价脚本,因此我不得不提取代码,并且使用了这篇文章中的注释编织器:在其中运行所有块Rmarkdown文档,使用knitr::purl
来获取代码,并使用sys.source
来执行代码.
Anyway, since it is a markdown script, I had to extract the code and I was using comments from this post knitr: run all chunks in an Rmarkdown document using knitr::purl
to get the code and sys.source
to execute it.
runAllChunks <- function(rmd, envir=globalenv()){
# as found here https://stackoverflow.com/questions/24753969
tempR <- tempfile(tmpdir = '.', fileext = ".R")
on.exit(unlink(tempR))
knitr::purl(rmd, output=tempR, quiet=TRUE)
sys.source(tempR, envir=envir)
}
由于某种原因,这可能会在几周后产生一个错误(不确定我最近安装了哪些新软件包... ).但是由于有一个新注释,我可以只使用knitr::knit
来执行代码,因此可以按预期工作,现在我的测试不再抱怨了.
For some reason, this produces an error since maybe a few weeks (not sure which new packages I installed lately...). But since there is a new comment, that I can just use knitr::knit
which also executes the code, this worked as expected and now my test no longer complains.
最后,我不知道问题出在哪里,但是现在可以解决了.
So in the end, I don't know where the problem exactly was, but this is now working.
推荐答案
我最近遇到了一个类似的问题,我的测试失败了(成功使用devtools::test()
失败了,但是使用devtools::check()
失败了).我不知道这种解决方案是否一定能解决上述问题,但它应该有助于找出类似的问题.
I recently had a similar issue with my tests breaking (succeeding with devtools::test()
but failing with devtools::check()
). I don't know if this solution necessarily fixes the problem above, but it should help to track down similar problems.
就我而言,问题最终归结为使用的功能需要的是Suggests
中列出的软件包,而不是Imports
/Depends
中的软件包.特别是,我的函数名为httr::content()
,当我尝试将其传递给as = "parsed"
参数时出现中断.事实证明,as = "parsed"
使用建议的程序包readr
来读取csv,并且我需要将其添加到我的依赖项中,以使devtools::check()
正常工作.
In my case, the problem ultimately came down to using a function that needed a package listed in Suggests
rather than in Imports
/Depends
. In particular, my function called httr::content()
, which broke when I tried to pass it the as = "parsed"
argument. It turns out that as = "parsed"
uses a suggested package, readr
to read a csv, and I needed to add it to my dependencies for devtools::check()
to work.
这篇关于在devtools :: check中失败的测试,但在devtools :: test中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!