如果目录中没有文件,如何在testthat中写入expect_error以抛出错误?
how to write expect_error in testthat to throw an error if there is no file in the directory?
我是 运行 一个 R 代码,如果目录中没有文件,则使用 testthat 抛出错误。我的测试代码如下,(我根据Waldi的回答进行了编辑)
test_that(desc = "Test for 'LoadData' Condition 1",
code = {
filePath = "./UnitTest/Data/Expected_Output2"
expect_error(LoadData(inputPath = filePath),"There's no file at ./UnitTest/Data/Expected_Output2")
}
)
我的职能是,
LoadData = function(inputPath) {
if(!file.exists(inputPath){
stop(paste0("There's no file at ", inputPath))
}
}
我的测试代码失败并显示此消息,
Error: `LoadData(inputPath = filePath)` threw an error with unexpected message.
Expected match: "There's no file at ./UnitTest/Data/Expected_Output_2"
Actual message: "cannot open the connection"
In addition: Warning message:
In open.connection(con, "rb") :
cannot open file './UnitTest/Data/Expected_Output_2': Permission denied
您只需准确测试预期的错误消息即可:
library(testthat)
LoadData = function(inputPath) {
if(length(list.files(inputPath))==0){
stop(paste0("There's no file at ", inputPath))
}
}
test_that(desc = "Test for 'LoadData' Condition 1",
code = {
filePath = "./UnitTest/Data/Expected_Output2"
expect_error(LoadData(inputPath = filePath),"There's no file at ./UnitTest/Data/Expected_Output2")
}
)
由 reprex package (v0.3.0)
于 2020-07-06 创建
以上测试成功,因为结果为:
LoadData("./UnitTest/Data/Expected_Output2")
出现以下错误:
Error in LoadData("./UnitTest/Data/Expected_Output2") :
There's no file at ./UnitTest/Data/Expected_Output2
解决此问题的一种方法是分配一个 json 文件,该文件实际上不存在于目录中,如下所示:
test_that(desc = "Test for 'LoadData' Condition 1",
code = {
filePath = "./UnitTest/Data/Expected_Output2/nofiles.json"
expect_error(LoadData(inputPath = filePath),"There's no file at ./UnitTest/Data/Expected_Output2/nofiles.json")
}
)
这里我们需要注意理解,之前我们只是分配了一个空路径。但是我们需要分配一个实际上不存在的假设文件。这对我有用。我的测试成功了。
我是 运行 一个 R 代码,如果目录中没有文件,则使用 testthat 抛出错误。我的测试代码如下,(我根据Waldi的回答进行了编辑)
test_that(desc = "Test for 'LoadData' Condition 1",
code = {
filePath = "./UnitTest/Data/Expected_Output2"
expect_error(LoadData(inputPath = filePath),"There's no file at ./UnitTest/Data/Expected_Output2")
}
)
我的职能是,
LoadData = function(inputPath) {
if(!file.exists(inputPath){
stop(paste0("There's no file at ", inputPath))
}
}
我的测试代码失败并显示此消息,
Error: `LoadData(inputPath = filePath)` threw an error with unexpected message.
Expected match: "There's no file at ./UnitTest/Data/Expected_Output_2"
Actual message: "cannot open the connection"
In addition: Warning message:
In open.connection(con, "rb") :
cannot open file './UnitTest/Data/Expected_Output_2': Permission denied
您只需准确测试预期的错误消息即可:
library(testthat)
LoadData = function(inputPath) {
if(length(list.files(inputPath))==0){
stop(paste0("There's no file at ", inputPath))
}
}
test_that(desc = "Test for 'LoadData' Condition 1",
code = {
filePath = "./UnitTest/Data/Expected_Output2"
expect_error(LoadData(inputPath = filePath),"There's no file at ./UnitTest/Data/Expected_Output2")
}
)
由 reprex package (v0.3.0)
于 2020-07-06 创建以上测试成功,因为结果为:
LoadData("./UnitTest/Data/Expected_Output2")
出现以下错误:
Error in LoadData("./UnitTest/Data/Expected_Output2") :
There's no file at ./UnitTest/Data/Expected_Output2
解决此问题的一种方法是分配一个 json 文件,该文件实际上不存在于目录中,如下所示:
test_that(desc = "Test for 'LoadData' Condition 1",
code = {
filePath = "./UnitTest/Data/Expected_Output2/nofiles.json"
expect_error(LoadData(inputPath = filePath),"There's no file at ./UnitTest/Data/Expected_Output2/nofiles.json")
}
)
这里我们需要注意理解,之前我们只是分配了一个空路径。但是我们需要分配一个实际上不存在的假设文件。这对我有用。我的测试成功了。