簡體   English   中英

如何在R中使用testthat測試文件夾是否存在

[英]How to test for folder existence using testthat in R

我有一個功能可以為我的其余工作流程設置一些文件夾

library(testthat)

analysisFolderCreation<-function(projectTitle=NULL,Dated=FALSE,destPath=getwd(),SETWD=FALSE){
  stopifnot(length(projectTitle)>0,is.character(projectTitle),is.logical(Dated),is.logical(SETWD))

  # scrub any characters that might cause trouble
  projectTitle<-gsub("[[:space:]]|[[:punct:]]","",projectTitle)

  rootFolder<-file.path(destPath,projectTitle)
  executionFolder<-file.path(rootFolder,if (Dated) format(Sys.Date(),"%Y%m%d"))

  subfolders<-c("rawdata","intermediates","reuse","log","scripts","results")

  dir.create(path=executionFolder, recursive=TRUE)
  sapply(file.path(executionFolder,subfolders),dir.create)
  if(Setwd) setwd(executionFolder)

}

我正在嘗試對其進行單元測試,並且我的錯誤測試工作正常:

test_that("analysisFolderCreation: Given incorrect inputs, error is thrown",
{
  # scenario: No arguments provided
  expect_that(analysisFolderCreation(),throws_error())
})

但是我對成功的考驗不是...

 test_that("analysisFolderCreation: Given correct inputs the function performs correctly",
 {
   # scenario: One argument provided - new project name
   analysisFolderCreation(projectTitle="unittest")
   expect_that(file.exists(file.path(getwd(),"unittest","log")),
               is_true())
 }

錯誤

  1. 錯誤:analysisFolderCreation:給定正確的輸入,函數將正確執行--------------------------------------- ------------------------找不到函數“ analysisFolderCreation”

當我檢查文件夾的存在時,我不確定如何去以期望的格式對此文件夾進行測試,該格式包括實際包含在其中的功能analysisFolderCreation。

我正在dev_mode()運行,並使用test_file()顯式執行測試文件

有誰能夠提供重寫測試以使其正常工作的方法,或者提供存在性檢查的期望?

問題似乎出在使用test_file()。 與test_file()不同,在整個單元測試套件中使用test()不需要在工作區中已經創建函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM