Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you test a custom expectation?

Tags:

r

testthat

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.)

like image 341
Richie Cotton Avatar asked Dec 04 '15 10:12

Richie Cotton


Video Answer


1 Answers

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))
        }
      ) 
    )
  }
) 
like image 157
Richie Cotton Avatar answered Nov 04 '22 22:11

Richie Cotton