Suppose I want a custom testthat
expectation. For example, I'm testing lots of objects to see if they have no missing values. The testhat
way of writing things should be something like this:
expect_no_nas <- function(object, info = NULL, label = NULL)
{
lab <- testthat:::make_label(object, label)
expect(has_no_nas(object), sprintf("%s has nulls.", lab),
info = info)
invisible(object)
}
has_no_nas <- function()
{
!any(is.na(x))
}
How do I test that that is right?
I can write tests that pass, no problem.
test_that(
"expect_no_nas passes when there are no NAs",
{
expect_no_nas(1:5)
}
)
I thought I could wrap the custom expectation in expect_error
, but this doesn't work:
test_that(
"expect_no_nas fails when there are NAs",
{
expect_error(expect_no_nas(c(1, NA)))
}
)
## Error: Test failed: 'expect_no_nas fails when there are NAs'
## * Not expected: c(1, NA) has NAs.
## * Not expected: expect_no_nas(c(1, NA)) code raised an error.
Wrapping it in try
doesn't work either.
test_that(
"expect_no_nas fails when there are NAs",
{
res <- try(expect_no_nas(c(1, NA)))
expect_false(res$passed)
}
)
## Error: Test failed: 'expect_no_nas fails when there are NAs'
## Not expected: c(1, NA) has NAs.
How do I test for the failing cases? (The important thing to remember is that we are testing whether expect_no_nas
works, not just writing tests that use expect_no_nas
.)
Nico's query help clarify things: you need a test inside a test.
test_that(
"expect_no_nas fails when there are NAs",
{
expect_error(
test_that(
"failing test",
{
expect_no_nas(c(1, NA))
}
)
)
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With