【问题标题】:test_file() works but test() fails with data.tabletest_file() 有效,但 test() 因 data.table 而失败
【发布时间】:2014-04-05 06:20:06
【问题描述】:

我的问题类似于r devtools test() errors but testthat test_file() works,但我不确定@hadley 的评论是否也适用于此。

我创建了一个 R package 的最小工作示例,其中使用 testthat::test_file 测试单个文件有效,但使用 devtools::test 测试包无效。

这是我包中唯一的 R 文件R/a.R

#' @export
generate_data_table <- function() {
  data.table(a = 1:10, b = 11:20)
}

这是我的测试文件inst/tests/test-a.R

test_that("everything is OK", {
  x <- generate_data_table()
  expect_equal(x[b == 11]$a, 1)
})

当我运行 test_file 时该测试通过,但当我运行 devtools::test 时收到以下错误:

> test()
Testing ttdt
Loading ttdt
1

1. Error: everything is OK -----------------------------------------------------
object 'b' not found
1: expect_equal(x[b == 11]$a, 1) at test-a.R:3
2: expect_that(object, equals(expected, label = expected.label, ...), info = info, label = label)
3: condition(object)
4: compare(expected, actual, ...)
5: compare.default(expected, actual, ...)
6: all.equal(x, y, ...)
7: all.equal.numeric(x, y, ...)
8: attr.all.equal(target, current, tolerance = tolerance, scale = scale, ...)
9: mode(current)
10: x[b == 11]
11: `[.data.table`(x, b == 11)
12: `[.data.frame`(x, i)

这是正确的行为还是应该被视为data.tabledevtools 中的错误?

这是我的环境:

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.9.2 testthat_0.8.1   devtools_1.4.1  

loaded via a namespace (and not attached):
 [1] digest_0.6.4   evaluate_0.5.1 httr_0.2       memoise_0.1    parallel_3.0.2
 [6] plyr_1.8.1     Rcpp_0.11.0    RCurl_1.95-4.1 reshape2_1.2.2 stringr_0.6.2 
[11] tools_3.0.2    whisker_0.3-2 

更新。我已经更新了包的依赖以正确依赖data.tablehere,但是问题依然存在。

【问题讨论】:

标签: r data.table devtools testthat


【解决方案1】:

如果你

  1. DESCRIPTION文件中正确设置依赖关系
  2. 运行test(fresh = TRUE)

一切都应该正常。

使用 fresh = TRUE 将确保测试在新的 R 会话中运行。

【讨论】:

    猜你喜欢
    • 2014-03-07
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 2020-05-27
    相关资源
    最近更新 更多