我找到一些奇怪的结果使用testthat
。在运行test_file
时,会发现个人test_that
调用,但除了上下文的名称外没有输出到控制台,并且返回的data.frame
没有预期的结果。我怀疑我正在做一些非常愚蠢的事情,但已经尝试了很多替代方案并得到相同的结果。R testthat不报告故障或错误
这里是我的tests.r
文件:
context("The context")
test_that('should pass',function(){
expect_equal(42,42)
})
test_that('should fail',function(){
expect_equal(43,42)
})
test_that('should error',function(){
stop()
})
我在其上运行test_file
:
> require(testthat)
Loading required package: testthat
> test_file('tests.r')
The context :
> results <- test_file('tests.r')
The context :
> results
file context test nb failed error user system real
1 tests.r The context should pass 0 0 FALSE 0 0 0.002
2 tests.r The context should fail 0 0 FALSE 0 0 0.001
3 tests.r The context should error 0 0 FALSE 0 0 0.001
正如你所看到的,没有控制台输出,并在结果data.frame
而不是有一个失败的测试和一个有错误,看起来好像一切都已经过去了。
当我直接在控制台中运行的函数体,我得到的期望是什么:
> expect_equal(42,42)
> expect_equal(43,42)
Error: 43 not equal to 42
Mean relative difference: 0.02380952
> stop()
Error:
这是我运行:
> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_NZ.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_NZ.UTF-8 LC_COLLATE=en_NZ.UTF-8
[5] LC_MONETARY=en_NZ.UTF-8 LC_MESSAGES=en_NZ.UTF-8
[7] LC_PAPER=en_NZ.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_NZ.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] testthat_0.9.1
怀疑,我可能有一个破碎的安装或我在Ubuntu Trusty/R 3.12和Windows 7/R 3.11上尝试过相同的测试,并得到相同的结果。我真的必须做些愚蠢的事情!
当我写使用Runit
类似的测试:
test.should_pass <- function(){
checkEquals(42,42)
}
test.should_fail <- function(){
checkEquals(43,42)
}
test.should_error <- function(){
stop()
}
我得到预期的产出和结果:
> results <- runTestFile('tests.r')
Executing test function test.should_error ... Timing stopped at: 0 0 0
Error in func() :
done successfully.
Executing test function test.should_fail ... Timing stopped at: 0 0 0.001
Error in checkEquals(43, 42) : Mean relative difference: 0.02325581
done successfully.
Executing test function test.should_pass ... done successfully.
> results
Number of test functions: 3
Number of errors: 1
Number of failures: 1
欢迎SO!您可以定义函数,但不会调用它们。要调用函数,请使用'what_is_the_meaning()'。在你的第三个例子中,你有一个匿名函数声明,但是再次,你不运行它。顺便说一句,'testthat'具有像'expect_error'这样的函数,请检查这些函数。 – tonytonov 2015-03-03 08:00:46
谢谢@tonytonov。我编辑了我的问题,所以它没有暗示道格拉斯亚当斯,但至少在句法上是正确的。不过,我得到了同样的结果。它非常奇怪。 – nokome 2015-03-04 02:53:36
别忘了带上毛巾。 – tonytonov 2015-03-04 08:09:53