1

Now that I find myself spending so much time programming in R, I really want to get back to automated testing (which I learned to do by habit in Perl). Besides being user-friendly, I would also be particularly interested in being able to generate random inputs for tests like Perl's Test::LectroTest or Haskell's QuickCheck. Is there anything similar for R?

Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99

1 Answers1

3

See the R package quickcheck on GitHub.

Like Test::LectroTest, the R package quickcheck is a port of QuickCheck, which Koen Claessen and John Hughes wrote for Haskell.

In addition to QuickCheck features, quickcheck also gives a nod to Hadley Wickam's popular testthat R package, by intentionally incorporating his "expectation" functions (which they call "assertions"). In addition to numerical and string tests are tests for failures and warnings, etc.

Here is a simple example using it:

library(quickcheck)

my_square <- function(x){x^2}        # the function to test

test( function(x = rinteger())  min(my_square(x)) >= 0 )
# Pass  function (x = rinteger())  
#  min(my_square(x)) >= 0 
# [1] TRUE

test( function(x = rdouble())
      all.equal(
                my_square(x),
                x^2
      )
)
# Pass  function (x = rdouble())  
#  all.equal(my_square(x), x^2) 
# [1] TRUE

The first test ensures that anything generated by my_square is positive. The second test actually replicates the functionality of my_square and checks every output to make sure it is correct.

Note that rinteger() produces a vector of any length consisting of integer values. Other randomly generated input data can be produced using functions like rcharacter, rdouble, and rmatrix.


Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
  • 2
    quickcheck dev here. Feel free to reach out for feedback, bugs etc. Assertion is the normal CS terminology for these things. Expectation is a loaded term in statistics and so there was no question in my mind we should stick with established terminology. – piccolbo Mar 20 '15 at 20:22