Skip to content

Commit

Permalink
Deprecate inst/tests. Closes #231
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Sep 29, 2015
1 parent d5769bb commit 0a7d27b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# testthat 0.10.0.9000

* Use tests in `inst/tests` is formally deprecated. Please move them into
`tests/testthat` instead (#231).

* `expect_match()` now encodes the match, as well as the output, in the
expectation message (#232).

Expand Down
26 changes: 16 additions & 10 deletions R/test-package.r
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ with_top_env <- function(env, code) {
code
}

#' Run all tests in an installed package
#' Run all tests in an installed package.
#'
#' Test are run in an environment that inherits from the package's namespace
#' environment, so that tests can access non-exported functions and variables.
#' Tests should be placed in either \code{inst/tests}, or (better)
#' \code{tests/testthat}.
#' Tests should be placed in \code{tests/testthat}. Use \code{test_check} with
#' \code{R CMD check} and \code{test_pacakge} interactively at the console.
#'
#' @section R CMD check:
#' Use \code{test_package} to test an installed package, or in
#' \code{tests/test-all.R} if you're using the older \code{inst/tests}
#' convention.
#' Create \code{tests/testthat.R} that contains:
#'
#' If your tests live in \code{tests/testthat} (preferred) use \code{test_check}
#' in \code{tests/testthat.R}. You still use \code{test_package} when testing
#' the installed package.
#' \preformatted{
#' library(testthat)
#' library(yourpackage)
#'
#' test_check("yourpackage")
#' }
#'
#' @param package package name
#' @inheritParams test_dir
Expand All @@ -47,7 +48,12 @@ test_package <- function(package, filter = NULL, reporter = "summary", ...) {

# If testthat subdir exists, use that
test_path2 <- file.path(test_path, "testthat")
if (file.exists(test_path2)) test_path <- test_path2
if (file.exists(test_path2)) {
test_path <- test_path2
} else {
warning("Placing tests in `inst/tests/` is deprecated. ",
"Please use `tests/testthat/` instead", call. = FALSE)
}

run_tests(package, test_path, filter, reporter, ...)
}
Expand Down
14 changes: 2 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,7 @@ Instructions for using this package can be found in the [Testing](http://r-pkgs.

## Integration with R CMD check

If you're using testthat in a package, you need to adopt a specific structure to work with `R CMD check`. This structure has changed recently to comply with new demands from CRAN, so please read closely if you submit your packages to CRAN. Previously, best practice was to put all test files in `inst/tests` and ensure that `R CMD check` ran them by putting the following code in `testthat.R`:

```R
library(testthat)
library(yourpackage)
test_package("yourpackage")
```

Now, recommended practice is to put your tests in `tests/testthat` in files starting with `test`, and ensure `R CMD check` runs them by putting the following code in `tests/testthat.R`:
If you're using testthat in a package, you should put your tests in `tests/testthat`. Each test file should start with `test` and end in `.R` or `.r`. To ensure `R CMD check` runs your tests, place the following code in `tests/testthat.R`:

```R
library(testthat)
Expand All @@ -42,6 +34,4 @@ library(yourpackage)
test_check("yourpackage")
```

The advantage of this new structure is that the user has control over whether or not tests are installed using the `--install-tests` parameter to `R CMD install`, or `INSTALL_opts = "--install-tests"` argument to `install.packages()`. I'm not sure why you wouldn't want to install the tests, but now you have the option.

You also need to add `Suggests: testthat` to `DESCRIPTION` as stated in the [Writing R Extensions](http://cran.r-project.org/doc/manuals/R-exts.html#Package-Dependencies) document. This avoids a `R CMD check` warning about unspecified dependencies.
Also make sure to add `Suggests: testthat` to your `DESCRIPTION`.
19 changes: 10 additions & 9 deletions man/test_package.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
\name{test_package}
\alias{test_check}
\alias{test_package}
\title{Run all tests in an installed package}
\title{Run all tests in an installed package.}
\usage{
test_package(package, filter = NULL, reporter = "summary", ...)

Expand All @@ -26,18 +26,19 @@ the results as a "testthat_results" (list)
\description{
Test are run in an environment that inherits from the package's namespace
environment, so that tests can access non-exported functions and variables.
Tests should be placed in either \code{inst/tests}, or (better)
\code{tests/testthat}.
Tests should be placed in \code{tests/testthat}. Use \code{test_check} with
\code{R CMD check} and \code{test_pacakge} interactively at the console.
}
\section{R CMD check}{
Use \code{test_package} to test an installed package, or in
\code{tests/test-all.R} if you're using the older \code{inst/tests}
convention.
Create \code{tests/testthat.R} that contains:
If your tests live in \code{tests/testthat} (preferred) use \code{test_check}
in \code{tests/testthat.R}. You still use \code{test_package} when testing
the installed package.
\preformatted{
library(testthat)
library(yourpackage)
test_check("yourpackage")
}
}
\examples{
\dontrun{test_package("testthat")}
Expand Down

0 comments on commit 0a7d27b

Please sign in to comment.