Code Monkey home page Code Monkey logo

testthat's Introduction

testthat

Testing your code is normally painful and boring. testthat tries to make testing as fun as possible, so that you get a visceral satisfaction from writing tests. Testing should be fun, not a drag, so you do it all the time. To make that happen, testthat:

  • Provides functions that make it easy to describe what you expect a function to do, including catching errors, warnings and messages.

  • Easily integrates in your existing workflow, whether it's informal testing on the command line, building test suites or using R CMD check.

  • Can re-run tests automatically as you change your code or tests.

  • Displays test progress visually, showing a pass, fail or error for every expectation. If you're using the terminal, it'll even colour the output.

testthat draws inspiration from the xUnit family of testing packages, as well from many of the innovative ruby testing libraries, like rspec, testy, bacon and cucumber. I have used what I think works for R, and abandoned what doesn't, creating a testing environment that is philosophically centred in R.

Instructions for using this package can be found in the Testing chapter of my forthcoming book Advanced R development: making reusable code".

Integration with R CMD check

If you're using testthat in a package, you need to adopt a specific structure to work with R CMD check. This structure has changed recently to comply with new demands from CRAN, so please read closely if you submit your packages to CRAN. Previously, best practice was to put all test files in inst/tests and ensure that R CMD check ran them by putting the following code in tests/test-all.R:

library(testthat)
library(yourpackage)
test_package("yourpackage")

Now, recommended practice is to put your tests in tests/testthat, and ensure R CMD check runs them by putting the following code in tests/testthat.R:

library(testthat)
test_check("yourpackage")

The advantage of this new structure is that the user has control over whether or not tests are installed using the --install-tests parameter to R CMD install, or INSTALL_opts = "--install-tests" argument to install.packages(). I'm not sure why you wouldn't want to install the tests, but now you have the option.

You also need to add Suggests: testthat to DESCRIPTION as stated in the Writing R Extensions document. This avoids a R CMD check warning about unspecified dependencies.

testthat's People

Contributors

hadley avatar karl-forner-quartz-bio avatar dirkschumacher avatar craigcitro avatar crowding avatar windelinckx avatar dkesh avatar trestletech avatar jmcphers avatar wibeasley avatar azvoleff avatar berndbischl avatar cboettig avatar sharpie avatar dlebauer avatar jlehtoma avatar kbroman avatar kenahoo avatar krlmlr avatar sje30 avatar lianos avatar wch avatar juancentro avatar tbates avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.