首页 > 解决方案 > Declare function before each unit test with testthat package

问题描述

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

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

标签: rtestthat

解决方案


推荐阅读