How to use testthat expect_error() correctly?
Asked Answered
C

2

25

What is the correct usage of expect_error() in testthat package? I've tried to adapt example from help, but this fails when I use brackets in error message.

library(testthat)

# Works
tmp1 <- function() stop("Input is not correct")
    expect_error(tmp1(), "Input is not correct")

# Does not work
tmp2 <- function() stop("Input (x) is not correct")
    expect_error(tmp2(), "Input (x) is not correct")

# Does not work
tmp3 <- function() stop("Input(")
    expect_error(tmp3(), "Input(")

This results in:

> library(testthat)
> 
> # Works
> tmp1 <- function() stop("Input is not correct")
> expect_error(tmp1(), "Input is not correct")
> # Does not work
> tmp2 <- function() stop("Input (x) is not correct")
> expect_error(tmp2(), "Input (x) is not correct")
Error: tmp2() does not match 'Input (x) is not correct'. Actual value: 
Error in tmp2() : Input (x) is not correct
> # Does not work
> tmp3 <- function() stop("Input(")
> expect_error(tmp3(), "Input(")
Error in grepl("Input(", "Error in tmp3() : Input(\n", fixed = FALSE,  : 
  invalid regular expression 'Input(', reason 'Missing ')''

R version 3.0.1 (2013-05-16)

Cornall answered 11/7, 2013 at 9:57 Comment(1)
Possible duplicate of function expect_that from testthat runs into errorAmyloid
H
14

The second argument is a a regular expression. So you should give a valid regular expression, For example , this will work for 3 functions :

## this works for 3 , error message containing Input
lapply(list('tmp1','tmp2','tmp3'),function(x){
   expect_error(do.call(x,list()),"Input.*")
})

## this works for 3 also, but more complicated regular expression
lapply(list('tmp1','tmp2','tmp3'),function(x){
  expect_error(do.call(x,list()),"Input.?\\(?")
})
Hourglass answered 11/7, 2013 at 10:19 Comment(1)
I see, thank you. Is there some function to autoescape the error string?Cornall
S
29

Since version 0.8 (released 2014-02-20) you could pass arguments to grep. That allow to use fixed = TRUE in call so string is not treated as regular expression but literally.

So you could use:

# Works
tmp1 <- function() stop("Input is not correct")
expect_error(tmp1(), "Input is not correct", fixed=TRUE)

# Works
tmp2 <- function() stop("Input (x) is not correct")
expect_error(tmp2(), "Input (x) is not correct", fixed=TRUE)

# Works
tmp3 <- function() stop("Input(")
expect_error(tmp3(), "Input(", fixed=TRUE)
Shadoof answered 5/5, 2015 at 21:2 Comment(0)
H
14

The second argument is a a regular expression. So you should give a valid regular expression, For example , this will work for 3 functions :

## this works for 3 , error message containing Input
lapply(list('tmp1','tmp2','tmp3'),function(x){
   expect_error(do.call(x,list()),"Input.*")
})

## this works for 3 also, but more complicated regular expression
lapply(list('tmp1','tmp2','tmp3'),function(x){
  expect_error(do.call(x,list()),"Input.?\\(?")
})
Hourglass answered 11/7, 2013 at 10:19 Comment(1)
I see, thank you. Is there some function to autoescape the error string?Cornall

© 2022 - 2024 — McMap. All rights reserved.