Quickcheck: Randomized unit testing for R

February 4, 2015

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

Hadley Wickham's testthat package has been a boon for R package authors, making it easy to write tests to verify that your code is working directly, and alerting you when you make changes to your code that inadvertently breaks things. 

For the RHadoop project, though, developer Antonio Piccolboni needed a different testing framework, that included the possibility of writing tests that included random input values for functions. The Haskell language has a "quickcheck" package that does this, so Antonio wrote a similar package for R, also called quickcheck. The key function is called (naturally) test, and here's an example of it in action, testing a user-defined function called "identity":

test(function(x = rinteger()) identical(identity(x), x), sample.size = 100)

The rany function generates random integer inputs to pass to the identity function, and in this case the test is run 100 times with random values. Quickcheck supports generating inputs of various R data types (double, character, etc) and can even generate a mixture of R object types to test functions that support inputs of multiple types. And when errors are detected, the function repro will tell you exactly what inputs generated the error so you can track it down.

The quickcheck package is available for download now from GitHub, and a great place to start is the quickcheck tutorial, linked below.

Github (quickcheck): Assertion-based testing with Quickcheck

To leave a comment for the author, please follow the link and comment on their blog: Revolutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)