如何设置testthat以进行R CMD检查? [英] How to setup testthat for R CMD check?

查看:118
本文介绍了如何设置testthat以进行R CMD检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There are apparently two ways to integrate testthat with R CMD check. I can't get either to work.

根据 devtools Wiki :

开发软件包时,将您的测试放在inst/tests中,然后 创建一个文件tests/run-all.R(注意,它必须是大写的R), 其中包含以下代码:

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") 

这将评估包装中的测试 命名空间(因此您可以测试未导出的函数),它将抛出 如果有任何测试失败,则为错误.这意味着您将看到 除非全部通过,否则无法通过测试失败和R CMD检查的完整报告 测试通过.

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.

整个软件包为这里.其中有两个文件:

The whole package is here. In it are the two files:

## 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'

安装软件包后,我可以很好地运行测试(它们按预期失败).

After installing the package, I can run the tests just fine (they fail, as expected).

> 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警告与此无关,但是如果需要,我可以提供更多详细信息.

I don't think that the PDF warning has anything to do with this, but I can provide more details if requested.

根据testthat存储库的自述文件:

现在,建议的做法是将您的测试放入test/testthat中,然后 确保运行R CMD检查,然后将以下代码放入 测试/test-all.R:

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:

So I made sure I had the most recent version of testthat installed:

> install_github("testthat")

然后更改包.您可以在此处获得此版本.我将两个文件修改为

And then changed the package. You can get this version here. I modified the two files to be

## 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而言,它似乎可以正常工作.

Then updating the package version to 0.1.2 in the DESCRIPTION file, I can build it, install it, and use testthat to check it and get the same output as before. So it seems that as far as testthat is concerned, its working.

但是,R CMD检查仍然无法运行测试:

However, R CMD check still doesn't run the tests:

$ 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提供解决方案,但我会选择其中一种!

So the question:

What am I doing wrong? My preference is for a solution for Approach 2, but I'll take either!

推荐答案

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

You don't have a tests directory. test-all.R should be located at minimalbugexample/tests/test-all.R.

然后,您的实际测试进入方法#1的minimalbugexample/inst/tests或进入方法#2的minimalbugexample/tests/testthat/.

Then your actual tests go in minimalbugexample/inst/tests for approach #1 or minimalbugexample/tests/testthat/ for approach #2.

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

For approach #2, the test-all.R file should use test_check(yourpackage) instead of test_package(yourpackage) and the library(yourpackage) call is no longer required.

这篇关于如何设置testthat以进行R CMD检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆