Chapter 8 Tests

Tests are really important to a healthy package.

Some people even start off with tests! 😮 see TDD, or “test driven development”

8.1 test infrastructure

8.1.1 Exercise: set up test infrastructure

Create manually, or run

which:

  • creates tests/testthat.R
  • creates tests/testthat/
  • adds testthat package as a Suggested package

8.2 write some tests

8.2.1 Exercise: write tests

Run

Or create a file in tests/testthat/ of the form test-foobar.R

Within the test file, if you ran use_test() you’ll have something like:

Add a test like that above if you don’t have it already.

8.3 run tests

8.3.1 Exercise: run tests

from the command line

from R

Or buttons in IDE

8.4 structure of a test file

  • context states the overall goal of the file, e.g., tests for function helloworld
    • context can be repeated whenever you like
    • context are the things printed when you run tests
  • test_that blocks, or function calls, can have any code run and at least one test expectation, e.g., expect_equal

8.5 further reading

the tests chapter