4. Error Cases

library(autotest)

4.1. Testing Variables

Variable Not Found

test_that('', {
  expect_equal(x, 1)
})
## AutoTestCaseError:
## Testing variable/expression:  x
## object 'x' not found

4.2. Testing Functions

Function Not Found

f <- 1
test_that('',{
  expect_equal(f(1), 1)
})
## AutoTestCaseError:
## Testing variable/expression:  f(1)
## could not find function "f"

Function Return Nothing

f <- function(x) {}
test_that('',{
  expect_equal(f(1), 1)
})
## AutoTestCaseError:
## Testing variable/expression:  f(1)
## We expect your answer returns type "numeric", but it returns "NULL" instead.
## Do you forget to return something in your function definition?

Function Unused Arguments

f <- function() 1
test_that('',{
  for (i in 1:5){
    expect_equal(f(i), i + 1)
  }
})
## AutoTestCaseError:
## Testing variable/expression:  f(i)
## unused argument (i)

4.3. Result Not Equal

  • Default error
f <- function(x) ifelse(x > 5, x + 1, x)
test_that('',{
  for (i in 1:10){
    expect_equal(f(i), i + 1)
  }
})
## AutoTestCaseError:
## Testing variable/expression:  f(i)
## Your answer is 1, which is not equal to the correct answer 2
  • Register error message.
f <- function(x) ifelse(x > 5, x + 1, x)
test_that('',{
  for (i in 1:10){
    registerPreMsg('In testing f(%d)', i) # better
    expect_equal(f(i), i + 1)
  }
})
## AutoTestCaseError:
## Testing variable/expression:  f(i)
## In testing f(1)
## Your answer is 1, which is not equal to the correct answer 2
  • Close tracing after register error message
f <- function(x) ifelse(x > 5, x + 1, x)
test_that('',{
  for (i in 1:10){
    registerPreMsg('Tesing the function `f`, in testing f(%d)', i)
    expect_equal(f(i), i + 1, trace=FALSE) # close tracing
  }
})
## AutoTestCaseError:
## Tesing the function `f`, in testing f(1)
## Your answer is 1, which is not equal to the correct answer 2

Compared with the error messages with previous two blocks, the line Testing variable/expression: f(i) is removed. That is what trace=FALSE is doing.

x = 1; y = 2
test_that('', {
  registerPreMsg('testing x')
  expect_equal(x, 1)
})
test_that('', {
  expect_equal(y, 1)
})
## AutoTestCaseError:
## Testing variable/expression:  y
## Your answer is 2, which is not equal to the correct answer 1

4.4. Self-Defined Error

x = 2
test_that('', {
  registerPostMsg('Your answer is wrong. Please try again.')
  expect_equal(x, 1, suppressErr=T)
})
## AutoTestCaseError:
## Testing variable/expression:  x
## Your answer is wrong. Please try again.