gpt4 book ai didi

r - 如何设置testthat以进行R CMD检查?

转载 作者:行者123 更新时间:2023-12-03 11:58:20 29 4
gpt4 key购买 nike

显然,有两种方法可以将testthatR CMD check集成在一起。我都不能上类。

方法1 :(可能已弃用)

根据devtools wiki:

When developing a package, put your tests in inst/tests and then create a file tests/run-all.R (note that it must be a capital R), which contains the following code:


library(testthat) 
library(mypackage)
test_package("mypackage")

This will evaluate your tests in the package namespace (so you can test non-exported functions), and it will throw an error if there are any test failures. This means you'll see the full report of test failures and R CMD check won't pass unless all tests pass.



整个包是 here。其中有两个文件:
## minimalbugexample/inst/tests/run-all.R
library(testthat)
library(minimalbugexample)
test_package('minimalbugexample')


## minimalbugexample/inst/tests/test-use-Matrix-package.R
context("Intentional break")
expect_that( TRUE, equals(FALSE))

我的描述是
Package: minimalbugexample
Title:
Description:
Version: 0.1.1
Author: Nathan VanHoudnos <nathanvan@letterafterFmail.com>
Maintainer: Nathan VanHoudnos <nathanvan@letterafterFmail.com>
Depends:
R (>= 3.0.1),
Matrix (>= 1.0)
Suggests:
testthat
License: GPL
LazyData: true
Collate:
'minimalbugexample-package.r'
'use-Matrix-package.R'

安装软件包后,我可以很好地运行测试(它们按预期失败)。
> test_package('minimalbugexample')
Intentional break : 1


1. Failure: -------------------------------------------------------------------
TRUE not equal to FALSE
1 element mismatch
Error: Test failures
>

但是 R CMD check无法运行测试。
$ R CMD check minimalbugexample_0.1.1.tar.gz 
... snip ...
* checking PDF version of manual ... WARNING
WARNING: There was 1 warning.
See
‘/home/nathanvan/software/minimalbugexample.Rcheck/00check.log’
for details.

我认为PDF警告与此无关,但是如果需要,我可以提供更多详细信息。

方法2 :(出血边缘)

根据 README file of the testthat repository:

Now, recommend practice is to put your tests in tests/testthat, and ensure R CMD check runs then by putting the following code in tests/test-all.R:


library(testthat)
test_check(yourpackage)

因此,我确保已安装了最新版本的test:
> install_github("testthat")

然后换包。您可以获取此版本 here。我将两个文件修改为
## minimalbugexample/inst/tests/test-all.R
library(testthat)
test_check(minimalbugexample)


## minimalbugexample/inst/tests/testthat/test-use-Matrix-package.R
context("Intentional break")
expect_that( TRUE, equals(FALSE))

然后将DESCRIPTION文件中的软件包版本更新为0.1.2,我可以对其进行构建,安装并使用 testthat进行检查并获得与之前相同的输出。因此,就 testthat而言,它似乎可以正常工作。

但是,R CMD检查仍然无法运行测试:
$ R CMD check minimalbugexample_0.1.2.tar.gz 
... snip ...
* checking PDF version of manual ... WARNING
LaTeX errors when creating PDF version.
This typically indicates Rd problems.
WARNING: There was 1 warning.
See
‘/home/nathanvan/software/minimalbugexample.Rcheck/00check.log’
for details.

所以问题是:

我究竟做错了什么?我更喜欢方法2的解决方案,但我要么接受!

最佳答案

您没有tests目录。 test-all.R应该位于minimalbugexample/tests/test-all.R

然后,您的实际测试将使用方法1的minimalbugexample/inst/tests或方法2的minimalbugexample/tests/testthat/

对于方法2,test-all.R文件应使用test_check(yourpackage)而不是test_package(yourpackage),并且不再需要library(yourpackage)调用。

关于r - 如何设置testthat以进行R CMD检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17595796/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com