R测试没有报告失败或错误

时间:2015-03-03 03:10:21

标签: r unit-testing testthat

我使用testthat找到了一些奇怪的结果。运行test_file时,会发现单个test_that调用但除了上下文的名称之外没有输出到控制台,并且返回的data.frame没有预期的结果。我怀疑我正在做一些非常愚蠢的事情,但尝试了很多替代方案并得到了同样的结果。

这是我的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 

1 个答案:

答案 0 :(得分:3)

testthat测试中,您声明了一个匿名函数,如下所示:

function() {
  expect_equal(43,42)
}

运行这段代码。是否有任何实际执行?不,因为你已经发出了一个你没有打电话的功能声明。现在运行

{expect_equal(43,42)}

这是一段代码,而不是一个函数。

将测试文件更改为

context("The context")

test_that('should pass', {
  expect_equal(42,42)
})

test_that('should fail', {
  expect_equal(43,42)
})

test_that('should error', {
  stop()
})

产量

The context : .12


1. Failure(@test.R#8): should fail -----------------------------------------------------------------------------------------------------------------------------------
43 not equal to 42
Mean relative difference: 0.02380952

2. Error: should error -----------------------------------------------------------------------------------------------------------------------------------------------

1: withCallingHandlers(eval(code, new_test_environment), error = capture_calls)
2: eval(code, new_test_environment)
3: eval(expr, envir, enclos)
4: stop() at test.R:12