Skip to content

Commit dd44f64

Browse files
authored
Add new special files vignette (#1769)
Fixes #1638
1 parent 13db5a5 commit dd44f64

File tree

8 files changed

+106
-133
lines changed

8 files changed

+106
-133
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# testthat (development version)
22

3+
* New `vignette("special-files")` describes the various special files
4+
that testthat uses (#1638).
5+
36
* `skip_on_bioc()` now uses the documented environment variable
47
(`IS_BIOC_BUILD_MACHINE`) (#1712).
58

R/test-files.R

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
11
#' Run all tests in a directory
22
#'
3+
#' @description
34
#' This function is the low-level workhorse that powers [test_local()] and
45
#' [test_package()]. Generally, you should not call this function directly.
56
#' In particular, you are responsible for ensuring that the functions to test
67
#' are available in the test `env` (e.g. via `load_package`).
78
#'
8-
#' @section Special files:
9-
#' Certain `.R` files have special significance in testthat:
10-
#'
11-
#' * Test files start with `test` and are executed in alphabetical order.
12-
#'
13-
#' * Setup files start with `setup` and are executed before tests. If
14-
#' clean up is needed after all tests have been run, you can use
15-
#' `withr::defer(clean_up(), teardown_env())`. See `vignette("test-fixtures")`
16-
#' for more details.
17-
#'
18-
#' * Helper files start with `helper` and are executed before tests are
19-
#' run and, unlike setup files, are also loaded by `devtools::load_all()`.
20-
#' Helper files can be necessary for side-effect-y code that you need to run
21-
#' when developing the package interactively. It's certainly possible to
22-
#' define custom test utilities in a helper file, but they can usually be
23-
#' defined below `R/`, just like any other internal function.
24-
#'
25-
#' There is another type of special file that we no longer recommend using:
26-
#'
27-
#' * Teardown files start with `teardown` and are executed after the tests
28-
#' are run. Now we recommend interleaving setup and cleanup code in `setup-`
29-
#' files, making it easier to check that you automatically clean up every
30-
#' mess that you make.
31-
#'
32-
#' All other files are ignored by testthat.
9+
#' See `vignette("special-files")` to learn more about the conventions for test,
10+
#' helper, and setup files that testthat uses, and what you might use each for.
3311
#'
3412
#' @section Environments:
3513
#' Each test is run in a clean environment to keep tests as isolated as
@@ -45,7 +23,6 @@
4523
#' @param env Environment in which to execute the tests. Expert use only.
4624
#' @param ... Additional arguments passed to [grepl()] to control filtering.
4725
#' @param load_helpers Source helper files before running the tests?
48-
#' See [source_test_helpers()] for more details.
4926
#' @param stop_on_failure If `TRUE`, throw an error if any tests fail.
5027
#' @param stop_on_warning If `TRUE`, throw an error if any tests generate
5128
#' warnings.
@@ -126,10 +103,9 @@ test_dir <- function(path,
126103
#' Run all tests in a single file
127104
#'
128105
#' Helper, setup, and teardown files located in the same directory as the
129-
#' test will also be run.
106+
#' test will also be run. See `vignette("special-files")` for details.
130107
#'
131108
#' @inherit test_dir return params
132-
#' @inheritSection test_dir Special files
133109
#' @inheritSection test_dir Environments
134110
#' @param path Path to file.
135111
#' @param ... Additional parameters passed on to `test_dir()`

R/test-package.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
#' * `test_package()` tests an installed package.
66
#' * `test_check()` checks a package during `R CMD check`.
77
#'
8-
#' Tests live in `tests/testthat`.
8+
#' See `vignette("special-files")` to learn about the various files that
9+
#' testthat works with.
910
#'
1011
#' @section `R CMD check`:
1112
#' To run testthat automatically from `R CMD check`, make sure you have
@@ -19,7 +20,6 @@
1920
#' ```
2021
#'
2122
#' @inherit test_dir return params
22-
#' @inheritSection test_dir Special files
2323
#' @inheritSection test_dir Environments
2424
#' @param ... Additional arguments passed to [test_dir()]
2525
#' @export

man/test_dir.Rd

Lines changed: 3 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/test_file.Rd

Lines changed: 1 addition & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/test_package.Rd

Lines changed: 2 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/custom-expectation.Rmd

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ library(testthat)
1212
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
1313
```
1414

15-
This vignette shows you how to create custom expectations that work identically to the built-in `expect_` functions. Since these functions will need to be loaded when your package is loaded for testing, it is recommended that `expect_` functions be defined in `test-helpers.R` in your packages `R/` directory.
15+
This vignette shows you how to create custom expectations that work identically to the built-in `expect_` functions. Since these functions will need to be loaded in order for your tests to work, we recommend putting them in an appropriately named helper file, i.e. `tests/testthat/helper-expectations.R`.
1616

17-
## Creating an expectation
17+
## Expectation basics
1818

1919
There are three main parts to writing an expectation, as illustrated by `expect_length()`:
2020

@@ -35,7 +35,7 @@ expect_length <- function(object, n) {
3535
}
3636
```
3737

38-
## Quasi-labelling
38+
### Quasi-labelling
3939

4040
The first step in any expectation is to capture the actual object, and generate a label for it to use if a failure occur. All testthat expectations support quasiquotation so that you can unquote variables. This makes it easier to generate good labels when the expectation is called from a function or within a for loop.
4141

@@ -86,16 +86,3 @@ expect_length <- function(object, n) {
8686
fail(message)
8787
}
8888
```
89-
90-
## Testing your expectations
91-
92-
Use the expectations `expect_success()` and `expect_failure()` to test your expectation.
93-
94-
```{r}
95-
test_that("length computed correctly", {
96-
expect_success(expect_length(1, 1))
97-
expect_failure(expect_length(1, 2), "has length 1, not length 2.")
98-
expect_success(expect_length(1:10, 10))
99-
expect_success(expect_length(letters[1:5], 5))
100-
})
101-
```

vignettes/special-files.Rmd

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
title: "Special files"
3+
output: rmarkdown::html_vignette
4+
vignette: >
5+
%\VignetteIndexEntry{Special files}
6+
%\VignetteEngine{knitr::rmarkdown}
7+
%\VignetteEncoding{UTF-8}
8+
---
9+
10+
```{r, include = FALSE}
11+
knitr::opts_chunk$set(
12+
collapse = TRUE,
13+
comment = "#>"
14+
)
15+
```
16+
17+
```{r setup}
18+
#| include: false
19+
library(testthat)
20+
```
21+
22+
This vignette describes the various special files that testthat understands: test, helper, setup/teardown, snapshot, and everything else.
23+
24+
## Test files
25+
26+
These are the bread and butter of testthat.
27+
Test files live in `tests/testthat/`, start with either `test-` or `test_`, and end with `.r` or `.R`.
28+
We recommend organising your test files so that there's a one-to-one correspondence between the files in `R/` and the files in `tests/testthat/` so that (e.g.) `R/myfile.R` has a matching `tests/testthat/test-myfile.R`.
29+
This correspondence is maintained by functions like `usethis::use_r()` and `usethis::use_test()` and is taken advantage of by functions like `devtools::test_active_file()` and `devtools::test_coverage_active_file()`.
30+
31+
Test files are executed in alphabetical order, but you should strive to avoid dependencies between test files.
32+
In principle, you should be able to be run your test files in any order or even at the same time.
33+
34+
## Helper files
35+
36+
Helper files live in `tests/testtthat/`, start with `helper`, and end with `.r` or `.R`.
37+
They are sourced by `devtools::load_all()` (so they're available interactively when developing your packages) and by `test_check()` and friends (so that they're available no matter how your tests are executed).
38+
39+
Helper files are a useful place for functions that you've extracted from repeated code in your tests, whether that be test fixtures (`vignette("test-fixtures")`), custom expectations (`vignette("custom-expectations")`), or skip helpers (`vignette("skipping")`).
40+
41+
## Setup files
42+
43+
Setup files live in `tests/testthat/`, start with `setup`, and end with `.r` or `.R`.
44+
Typically there is only one setup file which, by convention, is `tests/testthat/setup.R`.
45+
Setup files are sourced by `test_check()` and friends (so that they're available no matter how your tests are executed), but they are *not* sourced by `devtools::load_all()`.
46+
47+
Setup files are good place to put truly global test setup that would be impractical to build into every single test and that might be tailored for test execution in non-interactive or remote environments.
48+
Examples:
49+
50+
- Turning off behaviour aimed at an interactive user, such as messaging or writing to the clipboard.
51+
- Setting up a cache folder.
52+
53+
If any of your setup should be reversed after test execution (i.e. it needs to be torn down), we recommend maintaining that teardown code alongside the setup code, in `setup.R`, because this makes it easier to ensure they stay in sync.
54+
The artificial environment `teardown_env()` exists as a magical handle to use in `withr::defer()` and `withr::local_*()`.
55+
A legacy approach (which still works, but is no longer recommended) is to put teardown code in `tests/testthat/teardown.R`.
56+
57+
Here's a `setup.R` example from the reprex package, where we turn off clipboard and HTML preview functionality during testing:
58+
59+
```{r eval = FALSE}
60+
op <- options(reprex.clipboard = FALSE, reprex.html_preview = FALSE)
61+
62+
withr::defer(options(op), teardown_env())
63+
```
64+
65+
Since we are just modifying options here, we can be even more concise and use the pre-built function `withr::local_options()` and pass `teardown_env()` as the `.local_envir`:
66+
67+
```{r eval = FALSE}
68+
withr::local_options(
69+
list(reprex.clipboard = FALSE, reprex.html_preview = FALSE),
70+
.local_envir = teardown_env()
71+
)
72+
```
73+
74+
### Teardown files
75+
76+
Teardown files live in `tests/testhat/`, start with `teardown` and end with `.r` or `.R`.
77+
They are executed after the tests are run, but we no longer recommend using them as it's easier to check that you clean up every mess that you make if you interleave setup and tear down code as described above.
78+
79+
## Snapshot files
80+
81+
Snapshot files live in `tests/testthat/_snaps/`.
82+
Snapshot file are named automatically based on the name of the test file so that `tests/testthat/test-one.R` will generated `tests/testthat/_snaps/one.md`.
83+
Learn more about snapshot tests in `vignette("snapshotting")`.
84+
85+
## Other files and folders
86+
87+
Other files and folders in `tests/testthat/` are ignored by testthat, making them a good place to store persistent test data.
88+
Since the precise location of the `test/testthat/` directory varies slightly depending on how you're running the test, we recommend creating paths to these files and directories using `test_path()`.

0 commit comments

Comments
 (0)