我正在研究一个R包,我需要一些帮助来编写R测试函数,这些函数用于检查是否在C端代码上抛出正确的警告然后在R端捕获.让我给你一些关于我正在做的工作的背景:
到目前为止,我写了类似的东西:
counter <- 0
tryCatch({
function_im_testing()
}, warning = function(war) {
# Check if warning is as expected and if so increment counter
if(toString(war)=="The warning I'm expecting/testing for"){
print(toString(war))
counter <- counter + 1
}
}, error = function(err) {
print(toString(err))
}, finally = {
print("Leaving tryCatch")
})
# Stop if the 3 warnings we expected aren't present
stopifnot(counter == 3)
Run Code Online (Sandbox Code Playgroud)这是我正在使用的方法,到目前为止,我甚至无法通过尝试获取toString(war)和"警告我期望/测试"来获得if语句.事情.除此之外,这种方法非常草率和不可靠,这使我相信有更好的方法.那么,有没有更好的方法来做到这一点?
我正在为一个R函数编写一个测试用例,该函数测试是否在函数中的某个点抛出并正确捕获错误,并且在执行withCallingHandlers期间抛出错误时,我遇到一些麻烦(......).我正在使用这种方法:
counter <- 0
withCallingHandlers({
testingFunction(df0, df1)
testingFunction(df2, df3)
testingFunction(df4, df5)
}, warning=function(war){
print(paste(war$message))
}, error=function(err){
print(paste(err$message))
if(err$message == paste("The function should throw this error message",
"at the right time.")){
counter <<- counter + 1
}
})
stopifnot(counter == 2)
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是脚本在第一个错误(成功)被捕获后退出,我不知道如何处理错误,以便在捕获之后,withCallingHandlers继续执行下一部分.我知道它与重启对象有关,但我不确定如何正确使用它们.有没有人知道如何操作上面的代码,以便即使捕获错误,执行withCallingHandlers(...)仍然继续?