Developing R Packages
Jasmin Ludolf
Content Developer
In testthat package:
expect_equal(): equality with small tolerance allowedexpect_identical(): exact equality
expect_output(): matching text from object call
expect_error(): error from object callexpect_warning(): warning produced by object call#' @examples
#' # Convert 32F to C
#' temp_converter(32, "Fahrenheit", "Celsius")
[1] 0
In the tests/testthat/test-temp_converter.R file:
library(testthat) expect_equal(object = temp_converter(32, "Fahrenheit", "Celsius"),expected = 0)
expect_identical()expect_identical(sqrt(3) ^ 2, 3)
Error: sqrt(3)^2 (`actual`) not
identical to 3 (`expected`).
`actual`: 2.9999999999999996
`expected`: 3.0000000000000000
expect_equal()expect_equal() when comparing numeric valuesexpect_equal(sqrt(3) ^ 2, 3)
expect_equal()tolerance argument, default usually goodexpect_output(print("Testing R Packages is fun"),"funk")
Error: `print\("Testing R Packages is
fun"\)` does not match "funk".
Actual value: "\[1\] "Testing R
Packages is fun""

expect_warning(
temp_converter(-40,
"Celsius",
"Celsius")
)

expect_error(
temp_converter(300,
"Kelvin",
"Fahrenheit")
)

Developing R Packages