Reputation: 2250
Tests in my R
package started to fail on attributes. Let's have a minimal example:
library(testthat)
x <- structure(c("2", "0", "1", "2"), dim = c(2L, 2L), dimnames = list(
c("1", "5"), c("m1", "m4")))
y <- structure(c("2", "0", "1", "2"), dim = c(2L, 2L))
expect_equal(x, y, ignore_attr = TRUE)
# Error: `x` not equal to `y`.
# Attributes: < Length mismatch: comparison on first 1 components >
I only want to test the values at this stage, not the dimnames. This used to be done with expect_equivalent
, but according to the expect_equivalent
help (in version 3.2.1.1), the function has been deprecated and replaced with expect_equal(..., ignore_attr = TRUE)
. This has been the solution my code used in the past with success. Not any more. ignore_attr
is an argument for waldo::compare()
, which the help states is used.
Do I now need to rewrite my tests to always have matching attributes? If not, what is the current stable, recommended way to ignore attributes in tests?
Upvotes: 0
Views: 106
Reputation: 2250
Found it!
The issue is with the local edition on the new computer as is described here. expect_equal(..., ignore_attr = TRUE)
is the current, stable way how to ignore attributes in tests. It is only implemented in the third edition, which means testthat >= 3.0.0
.
The package DESCRIPTION
thus needs to specify Config/testthat/edition: 3
to apply the current rules.
My package does that and that's why I did not encounter the issue previously. It seems that today, I loaded testthat
first and doing so from outside the package set the second edition (that used the check.attributes
argument as @Waldi writes). Forcing the third edition with local_edition(3)
solved the problems.
local_edition(2)
expect_equal(x, y, check.attributes = FALSE)
local_edition(3)
expect_equal(x, y, ignore_attr = TRUE)
Upvotes: 1
Reputation: 41220
You could use the check_attributes
parameter as expect_equal
relies on attr.all.equal
which you can turn off with this parameter :
expect_equal(x, y, check.attributes = FALSE)
Upvotes: 0