r - data.table和testthat包 [英] r - data.table and testthat package

查看:161
本文介绍了r - data.table和testthat包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个与data.table工作的包,并且应该使用包testthat测试。
当从命令行调用时,代码工作正常,当从测试用例调用时遇到问题。看来基本包中的[]函数,即运行测试时使用的data.frames函数。

I am building a package which works with data.table and which should be tested using package testthat. While the code works fine when calling from the command line, I run into issues when calling from a test case. It seems that the [] function from the base package, i.e. the function for data.frames is used when running the tests.

我创建了一个最小示例,它可以是在这里找到: https://github.com/utalo/test_datatable_testthat

I have created a minimum example which can be found here: https://github.com/utalo/test_datatable_testthat

该软件包包含一个单独的函数:

The package contains a single function:

test <- function() {
   dt <- data.table(MESSAGE="Test 1234567890",TYPE="ERROR")
   dt[,.(MESSAGE=strwrap(MESSAGE,width = 10)),by=.(TYPE)]
}

当调用 test.datatable.testthat ::: test )从命令行获得预期结果:

When calling test.datatable.testthat:::test() from the command line I get the expected result:

    TYPE    MESSAGE
 1: ERROR       Test
 2: ERROR 1234567890

但是,执行以下单元测试时: p>

However, when executing the following unit test:

test_that("Test package",{
  dt <- test()

  expected_res <- structure(list(TYPE = c("ERROR", "ERROR"),
                             MESSAGE = c("Test","1234567890")),
                        row.names = c(NA, -2L), class = c("data.table","data.frame"),
                        .Names = c("TYPE", "MESSAGE"))

  expect_equal(dt,expected_res)
})

我得到一个错误:

1
1. Error: Test package -------------------------------------------------------------------------------------------------------
could not find function "."
1: withCallingHandlers(eval(code, new_test_environment), error = capture_calls, message = function(c) invokeRestart("muffleMessage"))
2: eval(code, new_test_environment)
3: eval(expr, envir, enclos)
4: test() at test.R:4
5: dt[, .(MESSAGE = strwrap(MESSAGE, width = 10)), by = .(TYPE)] at test.datatable.testthat/R/hello.R:5
6: `[.data.table`(dt, , .(MESSAGE = strwrap(MESSAGE, width = 10)), by = .(TYPE)) at C:\Users\D057806\Documents\R\test.datatable.testthat/R/hello.R:5
7: `[.data.frame`(x, i, j)

正如你所看到的,在测试中data.frame的[]称为。
我的第一个猜测是data.table包的依赖关系没有正确声明。这是我的DESCRIPTION文件:

As you can see, within the test the [] of data.frame is called. My first guess was that the dependency to the data.table package is not declared correctly. This is my DESCRIPTION file:

Package: test.datatable.testthat
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1
Date: 2016-04-07
Authors@R: person("First", "Last", email = "first.last@example.com", role = c("aut", "cre"))
Description: More about what it does (maybe more than one line)
License: What license is it under?
LazyData: TRUE
Depends:
    data.table
Suggests:
     testthat
RoxygenNote: 5.0.1

根据使用data.table包里面我自己的包,应该足够声明data.table作为依赖包。

According to Using data.table package inside my own package it should be sufficient to declare data.table as a dependent package. However, this does not seem to be the case here.

有关为什么我的函数直接调用而不是在testthat的上下文中工作的线索?

Any clues as to why my function is working when being called directly but not in the context of testthat?

推荐答案

为了发表评论,


  1. 不要在包名称中使用下划线,它会破坏标准。

  1. Don't use underscore in package name, it breaks the standard. Underscores will be turned to dots.

无法真正告诉您testthat未能处理您的测试的原因。您可以尝试导出 test 函数。它不导出,因此应仅使用 :::

Can't really tell you why testthat failed to process your test. You can try to export test function. It is not exported so should be used only with ::: explicitly. Maybe testthat depends on that in some way, don't know.

当我将测试从testthat中移出时,测试就会通过。如果你不能解决它,我会寻求在testthat问题的支持。

Test is passing when I move tests out of testthat. If you fail to resolve it I would look for support in testthat issues.

您可以看到我的叉 jangorecki / test_datatable_testthat 您的pkg(url将在几天后不工作,所以提取更改,如果你想稍后访问它们)。

您的测试已移出的测试在 tests / test.R 中,内容如下。

You can see my fork jangorecki/test_datatable_testthat of your pkg (url won't work in few days from now so fetch the changes if you want to access them later).
Your test moved out of testthat is in tests/test.R, content below.

dt <- test.datatable.testthat:::test()
expected_res <- structure(list(TYPE = c("ERROR", "ERROR"),
                               MESSAGE = c("Test","1234567890")),
                          row.names = c(NA, -2L), class = c("data.table","data.frame"),
                          .Names = c("TYPE", "MESSAGE"))
stopifnot(all.equal(dt,expected_res))

Testthat测试通过将其改为dummy来抑制, TRUE == TRUE
现在你的测试定义在testthat之外,通过确定。

相关部分来自 00check.log

Testthat test is suppressed by changing it to dummy, kind of TRUE==TRUE. Now your test defined outside of testthat pass OK.
Related part from 00check.log:

* checking tests ...
  Running ‘test.R’
  Running ‘testthat.R’
 OK
* DONE

这篇关于r - data.table和testthat包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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