4

を使用して奇妙な結果を見つけてい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には、失敗したテストとエラーのあるテストが 1 つずつ表示される代わりに、すべてが成功したように見えます。

関数本体をコンソールで直接実行すると、期待どおりの結果が得られます。

> 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 
4

1 に答える 1