2

I am running tests in R using the test_dir() function from the testthat package. In some of the test scripts there are functions that call readline(), which - in interactive mode - causes the testing to pause and wait for user input. The functions calling readline() are not my own and I don't have any influence on them. The user input is irrelevant for the output of those functions.

Is there a way to avoid these pauses during testing?

Approaches that come to mind, but I wouldn't know how to implement them:

  • disable interactive mode while R is running
  • use another function from the testthat package that runs scripts in non-interactive mode
  • somehow divert stdin to something else than the terminal(??)
  • wrap functions calling readline() in another script that is called in non-interactive mode from my testing script and makes the results available

Testing only from the command line using Rscript is an option, but I'd rather stay in the RStudio workflow.

======

Example Code

with_pause <- function () {  
  readline() 
  2
}
without_pause <- function () {
  2
}
expect_equal(with_pause(), without_pause())
Niels
  • 176
  • 7
  • I now stick to testing from the command line. It's not too much hassle, but still not the _ideal_ solution in terms of workflow. – Niels Jul 27 '16 at 10:08

1 Answers1

1

I have a similar problem. I solved it with a global option setting.

original_test_mode <- getOption('my_package.test_mode')
options('my_package.test_mode' = TRUE)
# ... some tests ...
options('my_package.test_mode' = original_test_mode)

In my scripts I have a if statement

if(getOption('my_package.test_mode', FALSE)) {
  # This happens in test mode
  my_value <- 5
} else {
  # normal processing
  my_value <- readline('please write value: ')
}

Also not the nicest way but it works for me.

Maybe one more hint. It happened to that my test script failed. The problem here is, that the global option stays TRUE and in the next round and also for executing the script in the same session, it will never prompt you to write a value. I guess I should put some stuff in a tryCatch function or so. But if you have this problem in mind, just "sometimes" options('my_package.test_mode', NULL) helps :-)

drmariod
  • 11,106
  • 16
  • 64
  • 110