- tests are kept inside a file which is prefixed with 'test_'
- data are kept inside files prefixed with 'helper_'
Package Directory and File Structure:
└──pkg_name/
├── DESCRIPTION
├── NAMESPACE
├──.Rbuildignore
├── data/
├── man/
├── R/
├── vignettes/
└── tests/
├── testthat.R
└── testthat/
└── helper_myfunc1.R
└── helper_myfunc2.R
└── test_pkg_name.R
testthat.R
library(testthat)
library(pkg_name)
test_check("pkg_name")
helper_myfunc1.R contains data for testing myfunc1 function
a1 <- 2
a2 <- 2
b1 <- 2*3
b2 <- 6
helper_myfunc2.R contains data for testing myfunc2 function
c1 <- 50/2
c2 <- 25
d1 <- c(2,3)
d2 <- c(2,3)
test_pkg_name.R contains tests for functions and other objects in the package
context('pkg_name_functions')
test_that('myfunc1',
{
expect_identical(a1, a2)
expect_identical(b1, b2)
})
test_that('myfunc2',
{
expect_identical(c1, c2)
expect_identical(d1, d2)
})
Conduct unit testing
library("devtools")
devtools::load_all()
# Loading pkg_name
devtools::test()
# Loading pkg_name
# Testing pkg_name
# pkg_name_functions: ....
# DONE ================================================================
/tests/testdata/
and then loading via, e.g.,read.csv("../testdata/test1.csv")
is better. I have checked that the extra files in the foldertestdata
also get copied into the library directory after you have built & installed the package with flag--install-tests
. The latter is important because the tests should be distributed with the package IMHO. – Melonymelosinst/testdata
and thensystem.file("testdata",...,package="my_package")
– Mikkeldevtools::check()
? – PincersR CMD check
, not so sure aboutdevtools::check()
. – Mikkelinst/testdata/
) which I want to use in my tests. How do I access them using thissystem.file("testdata", package="my_package")
approach? I want to read one of the objects back into a variable usingreadRDS()
for instance. – Leilaa.rds
, thena <- readRDS(system.file("testdata", "a.rds", package="my_package"))
should work viaR CMD check
ordevtools::check()
) – Mikkel