
Testing your code can be painful and tedious, but it greatlyincreases the quality of your code.testthat tries tomake testing as fun as possible, so that you get a visceral satisfactionfrom writing tests. Testing should be addictive, so you do it all thetime. To make that happen, testthat:
Provides functions that make it easy to describe what you expecta function to do, including catching errors, warnings, andmessages.
Easily integrates in your existing workflow, whether it’sinformal testing on the command line, building test suites, or using RCMD check.
Displays test progress visually, showing a pass, fail, or errorfor every expectation. If you’re using the terminal or a recent versionof RStudio, it’ll even colour the output.
testthat draws inspiration from the xUnit family of testing packages,as well as from many of the innovative ruby testing libraries, likerspec,testy,bacon andcucumber.
testthat is the most popular unit testing package for R and is usedby thousands of CRAN packages.
If you’re not familiar with testthat, thetesting chapter inR packages gives a good overview, alongwith workflow advice and concrete examples.
# Install the released version from CRANinstall.packages("testthat")# Or the development version from GitHub:# install.packages("pak")pak::pak("r-lib/testthat")The easiest way to get started is withusethis. Assuming you’re ina package directory, just runusethis::use_test("name") tocreate a test file, and set up all the other infrastructure you need. Ifyou’re using RStudio, press Cmd/Ctrl + Shift + T (or rundevtools::test() if not) to run all the tests in apackage.