# Example of unit testing R code with testthat

# Example of unit testing R code with testthat

Join the DZone community and get the full member experience.

Join For Free**Is the concept of adopting a continuous everything model a daunting task for your fast moving business? Read this whitepaper to break down and understand one of the key pillars of this model in Continuous Governance: The Guardrails for Continuous Everything.**

Here’s a little example of using Hadley Wickham’s `testthat`

package for unit testing R code. You can read more about `testthat`

here.

The function below computes the real roots of a quadratic. All that really matters for our purposes is that the function can return 0, 1, or 2 numbers and it could raise an error.

real.roots <- function(a, b, c) { if (a == 0.) stop("Leading term cannot be zero") d = b*b - 4*a*c # discriminant if (d < 0) rr = c() else if (d == 0) rr = c( -b/(2*a) ) else rr = c( (-b - sqrt(d))/(2*a), (-b + sqrt(d))/(2*a) ) return(rr) }

To test this code with `testthat`

we create another file for tests. The name of the file should begin with `test`

so that `testthat`

can recognize it as a file of test code. So let name the file containing the code above `real_roots.R`

and the file containing its tests`test_real_roots.R`

.

The test file needs to read in the file being tested.

source("real_roots.R")

Now let’s write some tests for the case of a quadratic with two real roots.

test_that("Distinct roots", { roots <- real.roots(1, 7, 12) expect_that( roots, is_a("numeric") ) expect_that( length(roots), equals(2) ) expect_that( roots[1] < roots[2], is_true() ) })

This tests that we get back two numbers and that they are sorted in increasing order.

Next we find the roots of (*x* + 3000)^{2} = *x*^{2} + 6000*x* + 9000000. We’ll test whether we get back -3000 as the only root. In general you can’t expect to get an exact answer, though in this case we do since the root is an integer. But we’ll show in the next example how to test for equality with a given tolerance.

test_that("Repeated root", { roots <- real.roots(1, 6000, 9000000) expect_that( length(roots), equals(1) ) expect_that( roots, equals(-3000) ) # Test whether ABSOLUTE error is within 0.1 expect_that( roots, equals(-3000.01, tolerance = 0.1) ) # Test whether RELATIVE error is within 0.1 # To test relative error, set 'scale' equal to expected value. # See base R function all.equal for optional argument documentation. expect_equal( roots, -3001, tolerance = 0.1, scale=-3001) })

To show how to test code that should raise an error, we’ll find the roots of 2*x* + 3, which isn’t a quadratic. Notice that you can test whether any error is raised or you can test whether the error message matches a given regular expression.

test_that("Polynomial must be quadratic", { # Test for ANY error expect_that( real.roots(0, 2, 3), throws_error() ) # Test specifically for an error string containing "zero" expect_that( real.roots(0, 2, 3), throws_error("zero") ) # Test specifically for an error string containing "zero" or "Zero" using regular expression expect_that( real.roots(0, 2, 3), throws_error("[zZ]ero") ) })

Finally, here are a couple tests that shouldn’t pass.

test_that("Bogus tests", { x <- c(1, 2, 3) expect_that( length(x), equals(2.7) ) expect_that( x, is_a("data.frame") ) })

To run the tests, you can run `test_dir`

or `test_file`

. If you are at the R command line and your working directory is the directory containing the two files above, you could run the tests with `test_dir(".")`

. In this case we have only one file of test code, but if we had more test files `test_dir`

would find them, provided the file names begin with `test`

.

**Are you looking for greater insight into your software development value stream? Check out this whitepaper: DevOps Performance: The Importance of Measuring Throughput and Stability to see how CloudBees DevOptics can give you the visibility to improve your continuous delivery process.**

Published at DZone with permission of John Cook , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

## {{ parent.title || parent.header.title}}

## {{ parent.tldr }}

## {{ parent.linkDescription }}

{{ parent.urlSource.name }}