我正在办公室写一个R包(delftfews
)。我们正在使用svUnit
进行单元测试。
我们描述新功能的过程:我们定义新的单元测试,最初标记为DEACTIVATED
;我们一次激活它们并实现测试所描述的功能的一个测试块。几乎所有时间我们都有少量的DEACTIVATED测试,相对于可能被删除或将要实现的功能。
我的问题/问题是:我是否可以更改doSvUnit.R以便R CMD check pkg
发出一个NOTE(即自定义消息“NOTE”而不是“OK”),以防止进行DEACTIVATED测试?
截至目前,我们只看到活动测试不会出错:
.
.
* checking for unstated dependencies in tests ... OK
* checking tests ...
Running ‘doSvUnit.R’
OK
* checking PDF version of manual ... OK
如果所有测试都成功,那就没关系,但是如果有跳过的测试则没那么好,如果测试失败则肯定是错误的。在这种情况下,我实际上希望看到如下的注释或警告:
.
.
* checking for unstated dependencies in tests ... OK
* checking tests ...
Running ‘doSvUnit.R’
NOTE
6 test(s) were skipped.
WARNING
1 test(s) are failing.
* checking PDF version of manual ... OK
截至目前,我们必须打开doSvUnit.Rout
来检查真实的测试结果。
我联系了r-forge和CRAN的两位维护人员,他们向我指了sources of R,特别是testing.R
脚本。
如果我理解正确,要回答这个问题,我们需要修补tools
包:
system
调用所以我在R上打开了一个change request,提出了类似于返回状态的位编码,ERROR的bit-0(现在是),WARNING的bit-1,NOTE的bit-2。
通过我的修改,很容易产生这个输出:
.
.
* checking for unstated dependencies in tests ... OK
* checking tests ...
Running ‘doSvUnit.R’
NOTE - please check doSvUnit.Rout.
WARNING - please check doSvUnit.Rout.
* checking PDF version of manual ... OK
Brian Ripley回答说“然而,有几个软件包有正确编写的单元测试 根据需要发出信号。请在其他地方进行讨论:R-bugs不是要问的地方 问题。“并关闭了变更请求。
任何人都有提示吗?
答案 0 :(得分:1)
您应该能够更改doSvUnit.R脚本,以便至少在您描述时发出警告和错误。你想要做的是运行测试,然后检查测试运行器的返回值,并使用R代码调用,warning()或stop()。
有关如何使用RUnit完成此操作的示例,请查看Bioconductor存储库中的codetoolsBioC包。相关代码在inst / templates中,并在下面复制:
.test <- function(dir, pattern = ".*_test\\.R$")
{
.failure_details <- function(result) {
res <- result[[1L]]
if (res$nFail > 0 || res$nErr > 0) {
Filter(function(x) length(x) > 0,
lapply(res$sourceFileResults,
function(fileRes) {
names(Filter(function(x) x$kind != "success",
fileRes))
}))
} else list()
}
if (missing(dir)) {
dir <- system.file("unitTests", package="@PKG@")
if (!nzchar(dir)) {
dir <- system.file("UnitTests", package="@PKG@")
if (!nzchar(dir))
stop("unable to find unit tests, no 'unitTests' dir")
}
}
## Run unit tests from the directory containing the test files.
## This allows tests to refer to data files with relative paths
cwd <- getwd()
on.exit(setwd(cwd))
setwd(dir)
require("RUnit", quietly=TRUE) || stop("RUnit package not found")
RUnit_opts <- getOption("RUnit", list())
RUnit_opts$verbose <- 0L
RUnit_opts$silent <- TRUE
RUnit_opts$verbose_fail_msg <- TRUE
options(RUnit = RUnit_opts)
suite <- defineTestSuite(name="@PKG@ RUnit Tests", dirs=getwd(),
testFileRegexp=pattern,
rngKind="default",
rngNormalKind="default")
result <- runTestSuite(suite)
cat("\n\n")
printTextProtocol(result, showDetails=FALSE)
if (length(details <- .failure_details(result)) >0) {
cat("\nTest files with failing tests\n")
for (i in seq_along(details)) {
cat("\n ", basename(names(details)[[i]]), "\n")
for (j in seq_along(details[[i]])) {
cat(" ", details[[i]][[j]], "\n")
}
}
cat("\n\n")
stop("unit tests failed for package @PKG@")
}
result
}
答案 1 :(得分:0)
我联系了r-forge和CRAN的两位维护人员,他们向我指了sources of R,特别是check.R
脚本。
如果我理解正确的话:
system
调用我在R上打开了一个change request,我的第一个猜测就像是对返回状态进行位编码,对于ERROR为bit-0(现在为),对于WARNING为bit-1,对于NOTE为bit-2 。
来自doSvUnit.R,如果失败,我会quit
status
2,跳过测试时会{4}。
补丁看起来像这样:
Index: src/library/tools/R/testing.R
===================================================================
--- src/library/tools/R/testing.R (revision 57214)
+++ src/library/tools/R/testing.R (working copy)
@@ -352,10 +352,16 @@
} else
cmd <- paste("LANGUAGE=C", "R_TESTS=startup.Rs", cmd)
res <- system(cmd)
- if (res) {
+ if (res%/%4 %% 2) {
+ message("NOTE")
+ }
+ if (res%/%2 %% 2) {
+ message("WARNING")
+ }
+ if (res %% 2) {
file.rename(outfile, paste(outfile, "fail", sep="."))
return(1L)
}
savefile <- paste(outfile, "save", sep = "." )
if (file.exists(savefile)) {
message(" Comparing ", sQuote(outfile), " to ",
未修补的R看到任何与0不同的内容为ERROR。