Search code examples
rtestthatpackage-development

testthat error on check() but not on test() because of ~/.Rprofile?


EDIT:

Is it possible that ~/.Rprofile is not loaded on within check(). It looks like my whole process fails since the ~/.Rprofile is not loaded.

DONE EDIT

I have a strange problem on automated testing with testthat. Actually, when I test my package with test() everything works fine. But when I test with check() I get an error message.

The error message says:

1. Failure (at test_DML_create_folder_start_MQ_script.R#43): DML create folder start MQ Script works with "../DML_IC_MQ_DATA/dummy_data" data 
  capture.output(messages <- source(basename(script_file))) threw an error

  Error in sprintf("%s folder got created for each raw file.", subfolder_prefix) : 
    object 'subfolder_prefix' not found

Before this error I source a script which defines the subfolder_prefix variable and I guess this is why it works in the test() case. But I expected to get this running in the check() function as well.

I will post the complete test script here, hope it is not to complicated:

library(testthat)
context("testing DML create folder and start MQ script")
test_dir <- 'dml_ic_mq_test'
start_dir <- getwd()

# list of test file folders
data_folders <- list.dirs('../DML_IC_MQ_DATA', recursive=FALSE)

for(folder in data_folders) { # for each folder with test files
  dir.create(test_dir)
  setwd(test_dir)

  script_file <- a.DML_prepare_IC.script(dbg_level=Inf) # returns filename I will source 

  test_that(sprintf('we could copy all files from "%s".', 
                    folder), {
                      expect_that(
                        all(file.copy(list.files(file.path('..',folder), full.names=TRUE), 
                                      '.', 
                                      recursive=TRUE)), 
                        is_true())
                    })
  test_that(sprintf('DML create folder start MQ Script works with "%s" data', folder), {
    expect_that(capture.output(messages <- source(basename(script_file))), 
                not(throws_error()))
  }) 
  count_rawfiles <- length(list.files(pattern='.raw$'))
  created_folders <- list.dirs(recursive=FALSE)
  test_that(sprintf('%s folder got created for each raw file.',
                    subfolder_prefix), {
                      expect_equal(length(grep(subfolder_prefix, created_folders)),
                                   count_rawfiles)
                    })
  setwd(start_dir)  
  unlink(test_dir, recursive=TRUE)
}

In my script I define the variable subfolder_prefix <- 'IC_' and within the test I check if the same number of folders are created for each raw file... This is what my script should do...

So as I said, I am not sure how to debug this problem here since test() works but check() fails during the testthat run.


Solution

  • Now that I know to look in devtools we can find the answer. Per the docs check "automatically builds and checks a source package, using all known best practices". That includes ignoring .Rprofile. It looks like check calls build and that all of that work is done is a separate (clean) R session. In contrast test appears to use your currently running session (in a new environment).