Quickcheck: Randomized unit testing for R

[This article was first published on Revolutions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)