簡體   English   中英

r - data.table和testthat包

[英]r - data.table and testthat package

我正在構建一個使用data.table的包,應該使用package testthat進行測試。 雖然代碼在從命令行調用時工作正常,但在從測試用例調用時會遇到問題。 似乎在運行測試時使用了基本包中的[]函數,即data.frames的函數。

我創建了一個最小的例子,可以在這里找到: https//github.com/utalo/test_datatable_testthat

該軟件包包含一個函數:

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

從命令行調用test.datatable.testthat:::test() ,我得到了預期的結果:

    TYPE    MESSAGE
 1: ERROR       Test
 2: ERROR 1234567890

但是,在執行以下單元測試時:

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包的依賴。 這是我的描述文件:

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

根據我自己的包中的Using data.table包將data.table聲明為依賴包就足夠了。 但是,這似乎不是這種情況。

關於為什么我的函數在被直接調用時工作但在testthat的上下文中沒有的任何線索?

要發表評論,以便發表評論。

  1. 不要在包名中使用下划線,它會破壞標准。 下划線將變為點。

  2. 無法真正告訴你為什么testthat無法處理你的測試。 您可以嘗試導出test功能。 它不會導出,因此只能與:::顯式使用。 也許testthat在某種程度上取決於那個,不知道。

  3. 當我將測試移出測試時,測試正在通過。 如果你沒有解決它,我會尋求測試問題的支持。

你可以看到你的pkg的fork jangorecki / test_datatable_test (url將在幾天后無法工作,所以如果你想稍后訪問它們,請獲取更改)。
您的測試移出tests/test.R中的tests/test.R ,內容如下。

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

測試通過將其更改為虛擬來抑制,其類型為TRUE==TRUE 現在你的測試在測試之外定義,通過OK。
相關部分來自00check.log

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM