我正在撰寫單元測驗testthat來檢查一些操作,這些操作需要具有srcref屬性的物件。srcref如果在包安裝/構建選項keep.source設定為. 時添加屬性TRUE。
devtools::check()如果這些測驗期望物件的屬性,我看到我所有的測驗都失敗srcref了。當以互動方式執行測驗時,即使用devtools:test(). 我能做些什么來保持這些測驗,運行它并讓它們通過使用devtools::check()?我試過devtools::check(args = "--with-keep.source")了,但這個論點不被認可。
我rlang::pkg_env("my-package")用來獲取具有srcref屬性的物件,因此測驗如下所示:
testthat("my example works", {
expect_true(!is.null(get_srcref_for_some_object(names(rlang::pkg_env("my-package")))))
})
uj5u.com熱心網友回復:
選項--with-keep.source是用于R CMD INSTALL,而不是R CMD check。為了確保check在安裝包時保留源代碼,您需要運行
R CMD check /path/to/tarball --install-args=--with-keep.source
在殼中或
devtools::check("path/to/package", args = "--install-args=--with-keep.source")
在 R。
最小的可重現示例
pkgname <- "foo"
usethis::create_package(pkgname, rstudio = FALSE, open = FALSE)
setwd(pkgname)
usethis::use_testthat()
text <- "
#' @title A title
#' @description A description.
#' @param a,b Arguments.
#' @examples
#' x <- add(1, 1)
#' @export
add <- function(a, b) a b
"
cat(text, file = file.path("R", "add.R"))
devtools::document(".")
text <- "
test_that(\"sources kept\", {
expect_false(is.null(attr(add, \"srcref\")))
})
"
cat(text, file = file.path("tests", "testthat", "test-add.R"))
devtools::check(".")
─ checking tests ...
─ Running ‘testthat.R’ (357ms)
E Some test files failed
Running the tests in ‘tests/testthat.R’ failed.
Last 13 lines of output:
> library(foo)
>
> test_check("foo")
[ FAIL 1 | WARN 0 | SKIP 0 | PASS 0 ]
══ Failed tests ════════════════════════════════════════════════════════════════
── Failure (test-add.R:3:3): sources kept ──────────────────────────────────────
is.null(attr(add, "srcref")) is not FALSE
`actual`: TRUE
`expected`: FALSE
[ FAIL 1 | WARN 0 | SKIP 0 | PASS 0 ]
Error: Test failures
Execution halted
devtools::check(".", args = "--install-args=--with-keep.source")
─ checking tests ...
? Running ‘testthat.R’
干凈的
setwd("..")
unlink(pkgname, recursive = TRUE)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/420156.html
標籤:
