7

I am developing an R package and one of the function implements interaction with users through standard input via readline. I now wonder how to test the behavior of this function, preferably with testthat library.

It seems test_that function assumes the answer is "" for user-input. I wish I could test the behavior conditional of various answers users may type in.

Below is a small example code. In the actual development, the marryme function is defined in a separate file and exported to the namespace. devtools::test() gets me an error on the last line because the answer never becomes yes. I would like to test if the function correctly returns true when user types "y".

library(testthat)

test_that("input", {
  marryme <- function() {
    ans <- readline("will you marry me? (y/n) > ")
    return(ans == "y")
  }

  expect_false(marryme())  # this is good
  expect_true(marryme())   # this is no good
})
Kota Mori
  • 6,510
  • 1
  • 21
  • 25
  • Split `marryme` into two functions. Put everything except `readline` in a function you can test and call that function with a wrapper function that contains `readline`. Btw., I'm not a fan of using `readline` for user input. – Roland Dec 29 '16 at 07:06
  • 2
    Thanks, @Roland. What would you suggest as an alternative for `readline`? – Kota Mori Dec 29 '16 at 09:39

1 Answers1

8

Use readLines() with a custom connection

By using readLines() instead of readline(), you can define the connection, which allows you to customize it using global options.

There are two steps that you need to do:

  1. set a default option in your package in zzz.R that points to stdin:

    .onAttach <- function(libname, pkgname){
      options(mypkg.connection = stdin())
    }
    
  2. In your function, change readline to readLines(n = 1) and set the connection in readLines() to getOption("mypkg.connection")

Example

Based on your MWE:


    library(testthat)

    options(mypkg.connection = stdin())

    marryme <- function() {
      cat("will you marry me? (y/n) > ")
      ans <- readLines(con = getOption("mypkg.connection"), n = 1)
      cat("\n")
      return(ans == "y")
    }

    test_that("input", {

      f <- file()
      options(mypkg.connection = f)
      ans <- paste(c("n", "y"), collapse = "\n") # set this to the number of tests you want to run
      write(ans, f)

      expect_false(marryme())  # this is good
      expect_true(marryme())   # this is no good
      # reset connection
      options(mypkg.connection = stdin())
      # close the file
      close(f)
    })
#> will you marry me? (y/n) > 
#> will you marry me? (y/n) >
SeGa
  • 9,454
  • 3
  • 31
  • 70
ZNK
  • 2,196
  • 1
  • 19
  • 25
  • This looks good for a single prompt, but I don't immediately see how to extend it to several prompts without having to set a litany of `option`s for each input line... – MichaelChirico Apr 29 '18 at 03:58
  • @MichaelChirico, I only use two `options()` calls per test suite, no matter how many input lines. The challenge is keeping track of the answer stack (in the `ans` variable). – ZNK Apr 29 '18 at 16:14
  • I am getting this error like that. `Error in nsenv[[f_name]](dirname(ns_path), package) : unbenutzte Argumente (dirname(ns_path), package) Ruft auf: suppressPackageStartupMessages ... -> load_code -> -> run_pkg_hook` – SeGa Jul 19 '19 at 16:39
  • I added `libname, pkgname` to the `.onAttach` method. Otherwise it throws the error mentioned in the comments. – SeGa Jul 19 '19 at 16:48