简体   繁体   English

devtools::test() 有效,但 devtools::check() 在测试 `expect_known_value()` 时失败

[英]devtools::test() works, but devtools::check() fails when testing `expect_known_value()`

I am building unit tests for an R-package that I am developing.我正在为我正在开发的 R 包构建单元测试。 I am using testthat , and for some of the functions, I am using testthat::expect_known_value()我正在使用testthat ,对于某些功能,我正在使用testthat::expect_known_value()

I am saving the reference files created by expect_known_value() in a folder inside the testthat folder "package/tests/testthat/reference_files"我将由expect_known_value()创建的参考文件保存在 testthat 文件夹“package/tests/testthat/reference_files”内的文件夹中

Two of my unit test looks like this:我的两个单元测试如下所示:

test_that("project_single_joint_to_MP works when .method is 'first_last'", {
  df <- dplyr::filter(mocapr::mocapr_data, movement_nr == 11)

  expect_known_value(project_single_joint_to_MP(df, LSX, LSY, LSZ, "LS", .method = "first_last"), here::here("tests", "testthat", "reference_files", "first_last"))
})

test_that("project_single_joint_to_MP works when .method is 'first_dist'", {
  df <- dplyr::filter(mocapr::mocapr_data, movement_nr == 11)
  expect_known_value(project_single_joint_to_MP(df, LSX, LSY, LSZ, "LS", .method = "first_dist"), here::here("tests", "testthat", "reference_files", "first_dist"))

})

I get no erros when running devtools::test() or devtools::test_coverage() , but when I run devtools::check() the two unit tests fails with this message:运行devtools::test()devtools::test_coverage()时没有错误,但是当我运行devtools::check()时,两个单元测试失败并显示以下消息:

-- 1. Error: project_single_joint_to_MP works when .method is 'first_last' (@test-project_to_MP.R#12)  -------------------------------------------------------------------
cannot open the connection
1: expect_known_value(project_single_joint_to_MP(df, LSX, LSY, LSZ, "LS", .method = "first_last"), here::here("tests", "testthat", "reference_files", "first_last")) at testthat/test-project_to_MP.R:12
2: saveRDS(object, file, version = version)
3: gzfile(file, mode)

-- 2. Error: project_single_joint_to_MP works when .method is 'first_dist' (@test-project_to_MP.R#17)  -------------------------------------------------------------------
cannot open the connection
1: expect_known_value(project_single_joint_to_MP(df, LSX, LSY, LSZ, "LS", .method = "first_dist"), here::here("tests", "testthat", "reference_files", "first_dist")) at testthat/test-project_to_MP.R:17
2: saveRDS(object, file, version = version)
3: gzfile(file, mode)

I have found some answers to similar questions and issues that suggest adding Sys.setenv(R_TESTS="") or Sys.setenv("R_TESTS" = "") to the first line in testthat.R, but this does not change anything.我找到了一些类似问题的答案,建议将Sys.setenv Sys.setenv(R_TESTS="")Sys.setenv("R_TESTS" = "")添加到 testthat.R 的第一行,但这不会改变任何事情。

Where should I place the reference files so that devtools::check() can open the files?我应该将参考文件放在哪里,以便devtools::check()可以打开文件?

I am running Rstudio in administrator mode, in case it is relevant.我在管理员模式下运行 Rstudio,以防万一。

Session info: Session 信息:

R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

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

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

other attached packages:
 [1] mocapr_0.0.1.9004 forcats_0.4.0     stringr_1.4.0     dplyr_0.8.3       purrr_0.3.3       readr_1.3.1       tidyr_1.0.0       tibble_2.1.3      ggplot2_3.2.1    
[10] tidyverse_1.2.1   testthat_2.2.1    usethis_1.5.1    

loaded via a namespace (and not attached):
 [1] nlme_3.1-141         fs_1.3.1             xopen_1.0.0          lubridate_1.7.4      devtools_2.2.1       progress_1.2.2       httr_1.4.1          
 [8] rprojroot_1.3-2      tools_3.6.1          backports_1.1.5      utf8_1.1.4           R6_2.4.0             lazyeval_0.2.2       colorspace_1.4-1    
[15] withr_2.1.2          tidyselect_0.2.5     prettyunits_1.0.2    processx_3.4.1       curl_4.2             compiler_3.6.1       cli_1.1.0           
[22] rvest_0.3.4          xml2_1.2.2           desc_1.2.0           scales_1.0.0         callr_3.3.2          commonmark_1.7       digest_0.6.22       
[29] rmarkdown_1.16       pkgconfig_2.0.3      htmltools_0.4.0      sessioninfo_1.1.1    rlang_0.4.0          readxl_1.3.1         rstudioapi_0.10     
[36] generics_0.0.2       farver_1.1.0         jsonlite_1.6         magrittr_1.5         Rcpp_1.0.2           munsell_0.5.0        fansi_0.4.0         
[43] lifecycle_0.1.0      stringi_1.4.3        yaml_2.2.0           MASS_7.3-51.4        pkgbuild_1.0.6       grid_3.6.1           crayon_1.3.4        
[50] lattice_0.20-38      haven_2.1.1          hms_0.5.1            zeallot_0.1.0        knitr_1.25           ps_1.3.0             pillar_1.4.2        
[57] pkgload_1.0.2        glue_1.3.1           evaluate_0.14        gganimate_1.0.3.9000 remotes_2.1.0        modelr_0.1.5         vctrs_0.2.0         
[64] tweenr_1.0.1         cellranger_1.1.0     gtable_0.3.0         rcmdcheck_1.3.3      polyclip_1.10-0      assertthat_0.2.1     xfun_0.10           
[71] ggforce_0.3.1        broom_0.5.2          roxygen2_6.1.1       memoise_1.1.0        ellipsis_0.3.0       here_0.1    

Had the same problem, my reference files were in package/test/reference_files.有同样的问题,我的参考文件在 package/test/reference_files 中。 But the tests are located in package/test/testthat.但是测试位于 package/test/testthat 中。 Exiting the testthat folder and back into the reference_folder did it for me.退出 testthat 文件夹并返回到 reference_folder 为我做了这件事。 That is:那是:

expect_known_value(test_depth, '../reference_files/ref_data.rda',
                     update = F, print = TRUE)

In your case I would try:在你的情况下,我会尝试:

expect_known_value(project_single_joint_to_MP(df, LSX, LSY, LSZ, "LS", .method = "first_dist"), "reference_files/first_dist")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 lintr::expect_lint_free() 进行测试失败并使用 devtools::check() 但可以使用 devtools::test() - testthat with lintr::expect_lint_free() fails with devtools::check() but works with devtools::test() "testthat 在 devtools::check 中失败,但在 devtools::test 中有效" - testthat fails within devtools::check but works in devtools::test R CMD检查失败,devtools :: test()工作正常 - R CMD check fails, devtools::test() works fine lintr测试使用devtools :: check()传递,并使用devtools :: test()失败 - lintr test passes with devtools::check() and fails with devtools::test() devtools :: test()有效,但devtools :: check()没有。 为什么? - devtools::test() works but devtools::check() doesn't. Why? `devtools::test()` 有效,但 `devtools::check()` 不能让 `system.file` 找到扩展名为 .yml 的文件 - `devtools::test()` works, but `devtools::check()` does not for `system.file` to locate file with `.yml` extension devtools::check 由于小插图构建而失败 - devtools::check fails because of vignette building devtools :: test()失败:依赖包X不可用 - devtools::test() fails: Dependency package X not available r devtools::test 失败但所有上下文都正常 - r devtools::test fails but all contexts are OK devtools::check 因 cpp_object_initializer 而失败 - devtools::check fails with cpp_object_initializer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM