我正在使用 Hadley’s testthat-based approach自动测试我的包. 在这种方法中,最适合放置测试数据文件的地方是什么?我的意思是仅在测试/测试中测试脚本使用的文件),而不是R /中的任何其他
在这种方法中,最适合放置测试数据文件的地方是什么?我的意思是仅在测试/测试中测试脚本使用的文件),而不是R /中的任何其他功能.
我目前的方法是把它们放在tests / testdata中,然后用相对路径read.table来读取.而不是用system.file(为了避免安装包来运行测试).
到目前为止,有什么最佳做法吗?
>测试保存在一个前缀为’test_’的文件中>数据保存在以’helper_’为前缀的文件中
软件包目录和文件结构:
└──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包含测试myfunc1函数的数据
a1 <- 2 a2 <- 2 b1 <- 2*3 b2 <- 6
helper_myfunc2.R包含测试myfunc2函数的数据
c1 <- 50/2 c2 <- 25 d1 <- c(2,3) d2 <- c(2,3)
test_pkg_name.R包含包中的函数和其他对象的测试
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)
})
进行单元测试
library("devtools")
devtools::load_all()
# Loading pkg_name
devtools::test()
# Loading pkg_name
# Testing pkg_name
# pkg_name_functions: ....
# DONE ================================================================
