【问题标题】:devtools::test started failing on my package (with X warning) since devtools 1.13.4 and testhat 2.0自 devtools 1.13.4 和 testhat 2.0 以来,devtools::test 在我的包上开始失败(带有 X 警告)
【发布时间】:2018-06-27 06:46:10
【问题描述】:

由于我设法缩小了问题范围,因此我进行了重大修改:

我遇到了一个关于 devtools::test 的新问题。我的包过去通过单元测试没有任何问题。但是在更新我的包(包括 devtools 和 testthat)之后,它现在失败了。

可重现的例子:

我已经设法构建了一个可重现的示例:在一个新的包架构中。

foo.R:/R/中的R文件

foo <- function(obj){
  return(as.numeric(obj))
}

test_foo.R:/tests/testthat 中的一个单元测试文件

test_that("Test my foo function: ",
          {
            expect_true(is.na(foo("1,5")))
            })

testhat.R:在 /tests/ 中运行我的单元测试

library(testthat)
library(foo)
test_check("foo")

两种场景

运行:devtools::test()

> devtools::test()
Loading foo
Loading required package: testthat
Testing foo
√ | OK F W S | Context
Error in x[[method]](...) : attempt to apply non-function

== Results =====================================================================
Duration: 0.1 s

OK:       0
Failed:   4
Warnings: 1
Skipped:  0

运行 testtjat::test_dir("tests/")

> testthat::test_dir("tests/")
√ | OK F W S | Context
== testthat results  ===========================================================
OK: 2 SKIPPED: 0 FAILED: 0

== Results =====================================================================
Duration: 0.1 s

OK:       0
Failed:   0
Warnings: 0
Skipped:  0

环境

> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252    LC_MONETARY=French_France.1252 LC_NUMERIC=C                  
[5] LC_TIME=French_France.1252    

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

other attached packages:
[1] foo_0.0.0.9000 testthat_2.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.15     roxygen2_6.0.1   rprojroot_1.3-2  crayon_1.3.4     assertthat_0.2.0 digest_0.6.14    withr_2.1.1      commonmark_1.4  
 [9] R6_2.2.2         backports_1.1.2  magrittr_1.5     cli_1.0.0        rlang_0.1.6      stringi_1.1.6    rstudioapi_0.7   xml2_1.1.1      
[17] desc_1.1.1       devtools_1.13.4  tools_3.4.3      stringr_1.2.0    yaml_2.1.16      compiler_3.4.3   memoise_1.1.0   

注意:

Github 上有一个关于这个问题的问题:https://github.com/hadley/devtools/issues/1675

【问题讨论】:

  • 这个问题400% 太长了。请在标题和第一句话中清晰简洁地描述问题所在。
  • 感谢您的反馈,但我不太确定如何改进这个需要描述我如何构建 muy 包的问题。你能提供一些建议吗?
  • 好吧,'我遇到了一个关于 devtools::test 的新问题"' 几乎什么都没告诉我们。你的意思是 "devtools::test 在我的包(带有 X 警告),因为更改 Y”?或者它总是失败?或者它曾经在 assertthat 0.2.0/ devtools 1.13.4 或其他包升级之前通过?(如果是这样,回滚并告诉我们包版本是什么known-good。)你真的需要定义“我遇到问题”的含义。在第一句话中。还有标题。如果这些不清楚,你会在第一行失去 90% 的观众。
  • 将代码移至对问题的清晰简明描述下方。使用代码格式化对其进行格式化。为了便于阅读,使用水平分隔线将代码与其他部分分隔开来。

标签: r devtools testthat


【解决方案1】:

当您在包中使用data.table 时,仅在描述中导入data.table 是不够的。 (注意应该是imports,而不是import

DESCRIPTION
Imports:
    data.table

这只要求您的软件包用户安装 data.table 软件包。

你还需要添加这个

NAMESPACE
import(data.table)

这通常用于写function 而不是pkg::function,不建议使用。但是,您必须为data.table 执行此操作。

以下是我的解释,但可能并不完全准确。我认为这是因为 data.table 需要为隐式 [] 和其他用法分派一些 data.table 特定函数,这不是正常的函数调用,所以你没有使用像 data.table::rbindlist 这样的东西,因为对象也是data.frame,基础版本被调用并导致问题。

This 是一个相关的问答。

【讨论】:

  • 谢谢你,imports 是一个错字。我设法缩小了我的问题,所以我编辑了我的问题。它实际上与 data.table 无关。对不起。
【解决方案2】:

这是一个测试问题,也在 github https://github.com/r-lib/testthat/issues/700 上提出。

解决方案是在每个测试文件的开头放置一个context("some_string")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-10
    • 2020-02-18
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 2019-08-31
    • 2020-10-30
    • 2021-12-23
    相关资源
    最近更新 更多