1. Learn
  2. /
  3. Courses
  4. /
  5. Creating R Packages

Exercise

Writing an individual test

You create individual tests within your test files using functions named with the pattern expect_*. To make your code easier to read, you may want to create the object to be tested (and/or the expected value, if there is one) before you call the expect_* function.

Here is one of the tests from the simutils package.

air_expected <- c(Ozone = 37, Solar.R = 7, 
                  Wind = 0, Temp = 0, Month = 0, Day = 0)

expect_equal(na_counter(airquality), air_expected)

The expect_* functions differ in the number of parameters, but the first parameter is always the object being tested.

When you run your tests, you might notice that there is no output. You will only see an output message if the test has failed.

Instructions

100 XP
  • Call data_summary() on iris and assign the result to iris_summary.
  • Assign the number of rows in iris_summary to summary_rows.
  • Use the function expect_equal() to test whether the result of calling data_summary() on the iris dataset returns 4 rows.