Hello,
I am fairly new to writing tests and would love suggestions for how best to test a function that loads an image with the magick package.
I wrote a simple helper function for my R package to load an image with magick::image_read() with the input argument is the filepath for the image. I saved a test image
in my package as tests > testthat > fixtures > w0030_s03_pWOZ_r01.png.
Here's a minimal working example:
load_image <- function(image_path){
if (file.exists(image_path)){
return(magick::image_read(image_path))
} else {
stop("image_path does not exist.")
}
}
Here are my attempts at writing tests for this function:
test_that("Load image works", {
image_path <- testthat::test_path("fixtures", "w0030_s03_pWOZ_r01.png")
actual <- load_image(image_path)
expected <- magick::image_read(image_path)
expect_identical(actual, expected)
expect_s3_class(actual, "magick-image")
})
test_that("Load image fails if file does not exist", {
image_path <- "fake.png"
expect_error(load_image(image_path), "image_path does not exist.")
})
The tests "expect_s3_class(actual, "magick-image")" and "expect_error(load_image(image_path), "image_path does not exist.")" both pass but expect_identical(actual, expected) failed and gave the following message:
actual
(actual
) not identical to expected
(expected
).
actual
is # A tibble: 1 × 7
expected
is # A tibble: 1 × 7
actual
is format width height colorspace matte filesize density
expected
is format width height colorspace matte filesize density
actual
is
expected
is
actual
is 1 PNG 548 125 Gray FALSE 23892 28x28
expected
is 1 PNG 548 125 Gray FALSE 23892 28x28
Here are my questions:
- Any ideas why this test failed? What am I missing?
- Are there different tests I should be running on this function?
Thank you for your help!
Stephanie