[英]R - testthat using data file outside the testing directory
I am using testthat
to test a package with a file tree similar to the following: 我使用testthat
测试包含类似于以下文件树的包:
.
├── data
│ └── testhaplom.out
├── inst
│ └── test
│ ├── test1.r
│ ├── tmp_S7byVksGRI6Q
│ │ └── testm.desc
│ └── tmp_vBcIkMN1arbn
│ ├──testm.bin
│ └── testm.desc
├── R
│ ├── haplom.r
│ └── winIdx.r
└── tmp_eUG3Qb0PKuiN
└── testhaplom.hap2.desc
In the test1.r
file, I need to use the data/testhaplom.out
file as input data for a certain function, but if I do test_file(test1.r)
, it changes into the inst/test
directory and cannot see the data file, giving the error below: 在test1.r
文件中,我需要使用data/testhaplom.out
文件作为某个函数的输入数据,但是如果我执行test_file(test1.r)
,它将更改为inst/test
目录并且无法查看数据文件,给出以下错误:
...Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'data/testhaplom.out': No such file or directory
There are two solutions for your problem: 您的问题有两种解决方案:
You could use the relative path ( ../data/testhaplom.out
): 您可以使用相对路径( ../data/testhaplom.out
):
expect_true(file.exists(file.path("..", "data", "testhaplom.out")))
Or you could use system.file
to get the location of the data directory: 或者您可以使用system.file
来获取数据目录的位置:
expect_true(file.exists(file.path(system.file("data", package="YOUR_R_PACKAGE"), "testhaplom.out")))
I prefer the second solution. 我更喜欢第二种解决方案。
BTW: file.path
use the correct path separator on each platform. BTW: file.path
在每个平台上使用正确的路径分隔符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.