Lyn*_*nda 21 unit-testing r testthat
我想知道是否可以在R中使用testthat
测试框架来设置相等的容差.
目前,如果example.R
是:
library(testthat)
three_times<-function(x) 3*x
context('Test three_times')
test_that('Three times returns 3 times x',{
expect_equal(three_times(3),9)
expect_equal(three_times(pi),9.4247)
})
Run Code Online (Sandbox Code Playgroud)
并执行test_file('example.R','stop')
,第一次测试通过,但第二次测试失败:
Error: Test failed: 'Three times returns 3 times x'
Not expected: three_times(pi) not equal to 9.4247
Mean relative difference: 8.271963e-06.
Run Code Online (Sandbox Code Playgroud)
是否可以为平均相对差异设置更高的误差阈值?例如1e-3.我有一些只有3位小数精度的预期结果,这意味着现在我的测试总是失败...
And*_*rie 29
你可以传递参数scale
或tolerance
.这些参数传递给all.equal
.
expect_equal(three_times(pi),9.4247, tolerance=1e-8)
Error: three_times(pi) not equal to 9.4247
Mean relative difference: 8.271963e-06
expect_equal(three_times(pi),9.4247, tolerance=1e-3)
Run Code Online (Sandbox Code Playgroud)
请参阅?all.equal
更多帮助
归档时间: |
|
查看次数: |
4079 次 |
最近记录: |