下面,我介绍Rstudio的测试功能所遇到的问题。它是我在软件包中所做的简化版本。我肯定做错了什么,但我不知道该怎么办。当我开始在CRAN上的软件包中进行更改时,我发现了它。某些测试突然没有通过Rstudio,而是通过R CMD check
进行。
要查看此问题,请使用Rstudio创建一个新程序包,然后调用usethis::use_test()
。然后编辑tests/testthat.R
,使文件为
library(testthat)
library(<name-of-package>)
suppressWarnings(RNGversion("3.5.0"))
test_check("<name-of-package>")
下一步,进入tests/testthat/test-hello.R
并将其更改为
set.seed(1)
rnorm(1)
test_that("seed is the same",
expect_known_value(.Random.seed, "the-seed.RDS"))
test_that("sample is the same",
expect_known_value(sample(100), "sample.RDS"))
生成程序包,然后运行devtools::test()
3次。第一次创建参考值。第二次产生
devtools::test()
#R Loading <name-of-package>
#R Testing <name-of-package>
#R ✔ | OK F W S | Context
#R ✖ | 1 1 | hello
#R ──────────────────────────────────────────────────────────────────────
#R ???: failure: sample is the same
#R sample(100) has changed from known value recorded in 'sample.RDS'.
#R 100/100 mismatches (average diff: 37.1)
#R [1] 39 - 76 == -37
#R [2] 1 - 39 == -38
#R [3] 34 - 24 == 10
#R [4] 87 - 53 == 34
#R [5] 43 - 92 == -49
#R [6] 14 - 86 == -72
#R [7] 82 - 40 == 42
#R [8] 59 - 83 == -24
#R [9] 51 - 90 == -39
#R ...
#R ──────────────────────────────────────────────────────────────────────
#R
#R ══ Results ═══════════════════════════════════════════════════════════
#R OK: 1
#R Failed: 1
#R Warnings: 0
#R Skipped: 0
并覆盖测试结果。一切都第三次过去了。通过Build
> More
> Test Package
通过Rstudio运行测试不会失败。接下来,转到控制台,然后转到父目录并运行
R CMD build <name-of-package>
R CMD check <name-of-package>_0.1.0.tar.gz
后者产生以下testthat.Rout.fail
#R R version 3.6.0 (2019-04-26) -- "Planting of a Tree"
#R Copyright (C) 2019 The R Foundation for Statistical Computing
#R Platform: x86_64-pc-linux-gnu (64-bit)
#R
#R R is free software and comes with ABSOLUTELY NO WARRANTY.
#R You are welcome to redistribute it under certain conditions.
#R Type 'license()' or 'licence()' for distribution details.
#R
#R R is a collaborative project with many contributors.
#R Type 'contributors()' for more information and
#R 'citation()' on how to cite R or R packages in publications.
#R
#R Type 'demo()' for some demos, 'help()' for on-line help, or
#R 'help.start()' for an HTML browser interface to help.
#R Type 'q()' to quit R.
#R
library(testthat)
library(<name-of-package>)
suppressWarnings(RNGversion("3.5.0"))
test_check("<name-of-package>")
#R ── 1. Failure: seed is the same ─────────────────────────────────────
#R `.Random.seed` has changed from known value recorded in 'the-seed.RDS'.
#R 1/626 mismatches
#R [1] 403 - 10403 == -10000
#R
#R ── 2. Failure: sample is the same ────────────────────────────────────
#R sample(100) has changed from known value recorded in 'sample.RDS'.
#R 99/100 mismatches (average diff: 36.6)
#R [1] 58 - 39 == 19
#R [2] 90 - 1 == 89
#R [3] 20 - 34 == -14
#R [4] 88 - 87 == 1
#R [5] 91 - 43 == 48
#R [6] 63 - 14 == 49
#R [7] 60 - 82 == -22
#R [8] 6 - 59 == -53
#R [9] 19 - 51 == -32
#R ...
#R
#R ══ testthat results ═════════════════════════════════════════════════
#R OK: 0 SKIPPED: 0 WARNINGS: 0 FAILED: 2
#R 1. Failure: seed is the same
#R 2. Failure: sample is the same
#R
#R Error: testthat unit tests failed
#R Execution halted
重新启动Rstudio并运行
setwd("tests")
source('path/to/package/<name-of-package>/tests/testthat.R')
与R CMD check
相同。通过使用R CMD check
并通过在原始程序包中获取testthat.R
文件得到的结果与旧的参考值相同。因此,devtools::test()
似乎正在发生某些事情。这是我的sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.2 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
Random number generation:
RNG: Mersenne-Twister
Normal: Inversion
Sample: Rounding
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] <name-of-package>_0.1.0 testthat_2.1.1
loaded via a namespace (and not attached):
[1] compiler_3.6.0 assertthat_0.2.1 magrittr_1.5
[4] R6_2.4.0 cli_1.1.0 tools_3.6.0
[7] withr_2.1.2 rstudioapi_0.10 yaml_2.2.0
[10] crayon_1.3.4 rlang_0.3.4
建议的解决方案here是创建一个文件,该文件的字母顺序比tests/testthat
中的所有测试要优先(因此它将首先运行,请参见?testthat::source_file
)。
因此,可以通过重复问题中的步骤并替换为
来修复错误然后编辑tests / testthat.R,使文件为
library(testthat) library(<name-of-package>) suppressWarnings(RNGversion("3.5.0")) test_check("<name-of-package>")
创建具有此内容的文件tests/testthat/helper.R
suppressWarnings(RNGversion("3.5.0"))
由于某种原因,这在第一次devtools::test()
和第二次devtools::test()
调用之间仍会产生不同的结果,但是现在其他所有结果都相同。第二个devtools::test()
#R Loading wtfpackage
#R Testing wtfpackage
#R ✔ | OK F W S | Context
#R ✖ | 1 1 | hello
#R ────────────────────────────────────────────────────────────────────
#R ???: failure: sample is the same
#R sample(100) has changed from known value recorded in 'sample.RDS'.
#R 99/100 mismatches (average diff: 34.6)
#R [1] 58 - 28 == 30
#R [2] 90 - 99 == -9
#R [3] 20 - 63 == -43
#R [4] 88 - 21 == 67
#R [5] 91 - 13 == 78
#R [6] 63 - 46 == 17
#R [7] 60 - 87 == -27
#R [8] 6 - 56 == -50
#R [9] 19 - 90 == -71
#R ...
#R ────────────────────────────────────────────────────────────────────
#R
#R ══ Results ═════════════════════════════════════════════════════════
#R OK: 1
#R Failed: 1
#R Warnings: 0
#R Skipped: 0
调用的输出是
COPY FROM