testthat ex 6: testing a package

Load the devtools package, and navigate to a location on disk where you want to create the package. Use devtools::create to create the directory structure.

library(devtools)
library(roxygen2)
create("hypo")
## Creating package hypo in .
## No DESCRIPTION found. Creating with values:
## Package: hypo
## Title: What the Package Does (one line, title case)
## Version: 0.0.0.9000
## Authors@R: person("First", "Last", email = "first.last@example.com", role = c("aut", "cre"))
## Description: What the package does (one paragraph)
## Depends: R (>= 3.3.0)
## License: What license is it under?
## LazyData: true
## Adding RStudio project file to hypo

Write the hypotenuse function to a file in the R directory. (Either point and click, or use writeLines.)

Also write this roxygen function documentation above the hypotenuse function.

#' Calculate the hypotenuse
#' 
#' Calculates the length of the hypotenuse of a 
#' right-angled triangle.
#' @param x A numeric vector.
#' @param y A numeric vector.
#' @return A numeric vector of hypotenuses
#' @export
writeLines(
  "#' Calculate the hypotenuse
#' 
#' Calculates the length of the hypotenuse of a 
#' right-angled triangle.
#' @param x A numeric vector.
#' @param y A numeric vector.
#' @return A numeric vector of hypotenuses
#' @export
hypotenuse <- function(x, y)
{
  sqrt(x ^ 2 + y ^ 2)
}",
  "hypo/R/hypotenuse.R"
)

Create a tests dir, and a testthat subdir. (Again, point and click or use dir.create.)

dir.create("hypo/tests")
dir.create("hypo/tests/testthat")

In the tests/testhat dir, create a file named test-hypotenuse.R. Add the unit tests that you wrote earlier for the hypotenuse function to this file.

writeLines(
  "test_that(
  'hypotenuse, with inputs 5 and 12, returns 13',
  {
    expected <- 13
    actual <- hypotenuse(5, 12)
    expect_equal(actual, expected)
  }
)
test_that(
  'hypotenuse, with inputs both 1e300, returns sqrt(2) * 1e300',
  {
    expected <- sqrt(2) * 1e300
    actual <- hypotenuse(1e300, 1e300)
    expect_equal(actual, expected)
  }
)",    # etc.
  "hypo/tests/testthat/test-hypotenuse.R"
)

In the tests dir, add a file named testthat.R that contains the boilerplate code for running your tests.

writeLines(
  "library(testthat)
library(hypo)

test_check('hypo')",
  "hypo/tests/testthat.R"
)

Update the DESCRIPTION file in the root of the package directory to include a description of the package, your details, and a line to say that testthat and devtools are used by the package.

Suggests: testthat, devtools
lines <- readLines("hypo/DESCRIPTION")
lines[2] <- "Title: Calculate Hypotenuses"
lines[5] <- "Description: Mostly for demonstrating testthat."
lines[7] <- "License: Unlimited"
lines <- c(lines[1:6], "Suggests: testthat, devtools", lines[7:8])
writeLines(lines, "hypo/DESCRIPTION")

Roxygenize, build and then check the package, to make sure it works as expected.

The check command should fail, since there are failing tests. Unfortunately, when you run check inside a knitted document, the failure message isn’t terribly clear. Run check directly from R to see how the tests fails.

roxygenize("hypo")
## First time using roxygen2 4.0. Upgrading automatically...
## Writing NAMESPACE
## Writing hypotenuse.Rd
build("hypo")
## "d:/PROGRA~1/R/R-devel/bin/x64/R" --no-site-file --no-environ --no-save  \
##   --no-restore CMD build "C:\Users\rjc2003\Dropbox\useR2015_workshop\for  \
##   distribution\answers\testthat\hypo" --no-resave-data --no-manual
## [1] "C:/Users/rjc2003/Dropbox/useR2015_workshop/for distribution/answers/testthat/hypo_0.0.0.9000.tar.gz"
check("hypo")
## Updating hypo documentation
## Loading hypo
## Writing NAMESPACE
## Writing hypotenuse.Rd
## "d:/PROGRA~1/R/R-devel/bin/x64/R" --no-site-file --no-environ --no-save  \
##   --no-restore CMD build "C:\Users\rjc2003\Dropbox\useR2015_workshop\for  \
##   distribution\answers\testthat\hypo" --no-resave-data --no-manual 
## 
## "d:/PROGRA~1/R/R-devel/bin/x64/R" --no-site-file --no-environ --no-save  \
##   --no-restore CMD check  \
##   "C:\Users\rjc2003\AppData\Local\Temp\RtmpYN65Ox/hypo_0.0.0.9000.tar.gz"  \
##   --timings
## Error: Command failed (1)