Can anyone kindly clarify how the tolerance
argument works in the expect_equal
function in testthat
? I have some examples that appear as if they should be failing tests but actually pass.
library("testthat")
test_that('Two is equal to two', {expect_equal(2, 4.000001/2, tolerance = 1e-4)})
As expected, the output here is "Test passed "
test_that('Two is equal to two', {expect_equal(2, 4.8/2, tolerance = 1)})
As expected, the output here is also "Test passed "
test_that('Two is equal to two', {expect_equal(2, 4.8/2, tolerance = 0.4)})
Since 2.4 - 2 = 0.4, this should be the lowest tolerance level for which the test passes. Indeed, the output: "Test passed "
test_that('Two is equal to two', {expect_equal(2, 4.8/2, tolerance = 0.2)})
Well that's confusing... the output is still "Test passed "
test_that('Two is equal to two', {expect_equal(2, 4.8/2, tolerance = 0.1)})
Finally, the expected error:
ββ Failure: Two is equal to two ββββββββββββββββββββββββββββββββββββββββββββββββ
2 not equal to 4.8/2.
1/1 mismatches
[1] 2 - 2.4 == -0.4
Error:
! Test failed
Backtrace:
β
1. ββtestthat::test_that(...)
2. β ββwithr (local) `<fn>`()
3. ββreporter$stop_if_needed()
4. ββrlang::abort("Test failed", call = NULL)
Can someone please explain what's going on here? Why is the test still passing when the difference is greater than the tolerance threshold?