Declare function before each unit test with testthat package

Viewed 105

I want to create global functions which be used in each unit test of my package:

  • beforeTest() which creates database table to test
  • afterTest() which drops the test database table.

Files:

/tests/testthat.R:

library(mypackage)  
library(testthat)

beforeTest <- function() {
  # implementation
}

afterTest <- function() {
  # implementation
}

test_check("mypackage")

/tests/testthat/unit_test_1.R:

test_that("mytest", {
  beforeTest()
  on.exit(afterTest()) 

  # implemetation of the unit test...
  # ...
  # ...
})

The problem is that file unit_test_1.R doesn't see the functions beforeTest() and afterTest()

0 Answers
Related