Skip to content

Commit 94d42b6

Browse files
committed
Improve reporter documentation
Fixes #657
1 parent d51a8b6 commit 94d42b6

18 files changed

+258
-95
lines changed

DESCRIPTION

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Collate:
5050
'context.R'
5151
'describe.R'
5252
'evaluate-promise.R'
53+
'example.R'
5354
'expect-comparison.R'
5455
'expect-equality.R'
5556
'expect-inheritance.R'

NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ export(test_path)
154154
export(test_rd)
155155
export(test_that)
156156
export(testing_package)
157+
export(testthat_example)
158+
export(testthat_examples)
157159
export(throws_error)
158160
export(try_again)
159161
export(use_catch)

NEWS.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# testthat (development version)
22

3+
* New `testthat_examples()` and `testthat_example()` make it easy to access
4+
new test files bundled with the package. These are used in various examples
5+
to make it easier to understand how to use the package in novel ways.
6+
7+
* Documentation of reporters has been considerably improved (#657).
8+
39
* Expectation object always contains the failure message, even when succesful
410
(#836)
511

R/example.R

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#' Retrieve paths to built-in example test files
2+
#'
3+
#' `testthat_examples()` retrieves path to directory of test files,
4+
#' `testthat_example()` retrieves path to a single test file.
5+
#'
6+
#' @keywords internal
7+
#' @param filename Name of test file
8+
#' @export
9+
#' @examples
10+
#' dir(testthat_examples())
11+
#' testthat_example("success")
12+
testthat_examples <- function() {
13+
system.file("examples", package = "testthat")
14+
}
15+
16+
#' @export
17+
#' @rdname testthat_examples
18+
testthat_example <- function(filename) {
19+
system.file(
20+
"examples", paste0("test-", filename, ".R"),
21+
package = "testthat",
22+
mustWork = TRUE
23+
)
24+
}

R/reporter-zzz.R

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
#' @include reporter-stop.R
22
NULL
33

4-
#' Get/set reporter; execute code in specified reporter.
4+
#' Get and set active reporter.
55
#'
6-
#' Changes global reporter to that specified, runs code and the returns
7-
#' global reporter back to previous value.
6+
#' `get_reporter()` and `set_reporter()` access and modify the current "active"
7+
#' reporter. Generally, these functions should not be called directly; instead
8+
#' use `with_reporter()` to temporarily change, then reset, the active reporter.
89
#'
9-
#' The `with_reporter()` function returns the reporter that has been used
10-
#' for running the code.
1110
#'
11+
#' @inheritParams test_file
12+
#' @param reporter Reporter to use to summarise output. Can be supplied
13+
#' as a string (e.g. "summary") or as an R6 object
14+
#' (e.g. `SummaryReporter$new()`).
15+
#'
16+
#' See [Reporter] for more details and a list of built-in reporters.
17+
#' @param code Code to execute.
18+
#' @return `with_reporter()` invisible returns the reporter active when `code`
19+
#' was evaluated.
20+
#' @param start_end_reporter Should the reporters `start_reporter()` and
21+
#' `end_reporter()` methods be called? For expert use only.
1222
#' @keywords internal
13-
#' @param reporter test reporter to use
14-
#' @param code code block to execute
1523
#' @name reporter-accessors
1624
NULL
1725

@@ -36,7 +44,6 @@ get_reporter <- function() {
3644
}
3745

3846
#' @rdname reporter-accessors
39-
#' @param start_end_reporter whether to start and end the reporter
4047
#' @export
4148
with_reporter <- function(reporter, code, start_end_reporter = TRUE) {
4249
reporter <- find_reporter(reporter)

R/reporter.R

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
1-
#' Managing test reporting
1+
#' Manage test reporting
22
#'
33
#' The job of a reporter is to aggregate the results from files, tests, and
4-
#' expectations and display in some informative way.
4+
#' expectations and display them in an informative way. Every testtthat function
5+
#' that runs multiple tests provides a `reporter` argument which you can
6+
#' use to override the default (which is selected by [default_reporter()]).
7+
#' Typically this will
58
#'
6-
#' Do not clone directly from this object - children should implement all
7-
#' methods.
9+
#' You only need to use this `Reporter` object directly if you are creating
10+
#' a new reporter. Currently, creating new Reporters is undocumented,
11+
#' so if you want to create your own, you'll need to make sure that you're
12+
#' familiar with [R6](https://adv-r.hadley.nz/R6.html) and then need read the
13+
#' source code for a few.
814
#'
915
#' @keywords internal
1016
#' @export
1117
#' @export Reporter
1218
#' @aliases Reporter
1319
#' @importFrom R6 R6Class
1420
#' @family reporters
21+
#' @examples
22+
#' path <- testthat_example("success")
23+
#'
24+
#' # The default reporter - doesn't display well in examples because
25+
#' # it's designed to work in an interactive console.
26+
#' test_file(path)
27+
#'
28+
#' # Override the default by supplying the name of a reporter
29+
#' test_file(path, reporter = "minimal")
1530
Reporter <- R6::R6Class("Reporter",
1631
public = list(
1732
start_reporter = function() {},
@@ -72,22 +87,10 @@ Reporter <- R6::R6Class("Reporter",
7287
)
7388
)
7489

75-
fancy_line <- function(x) {
76-
if (!l10n_info()$`UTF-8`) {
77-
return(x)
78-
}
79-
80-
switch(x,
81-
"-" = "\u2500",
82-
"=" = "\u2550",
83-
x
84-
)
85-
}
86-
87-
#' Retrieve the default reporter.
90+
#' Retrieve the default reporter
8891
#'
8992
#' The defaults are:
90-
#' * [SummaryReporter] for interactive; override with `testthat.default_reporter`
93+
#' * [ProgressReporter] for interactive; override with `testthat.default_reporter`
9194
#' * [CheckReporter] for R CMD check; override with `testthat.default_check_reporter`
9295
#'
9396
#' @export

R/source.R

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
#' Source a file, directory, or all helpers.
1+
#' Source a file, directory of files, or various important subsets
22
#'
3-
#' The expectation is that the files can be sourced in alphabetical order.
4-
#' Helper scripts are R scripts accompanying test scripts but prefixed by
5-
#' `helper`. These scripts are run once before the tests are run.
3+
#' These are used by [test_dir()] and friends
64
#'
7-
#' @param path Path to tests
8-
#' @param pattern Regular expression used to filter files
5+
#' @inheritSection test_dir Test files
6+
#' @param path Path to files.
7+
#' @param pattern Regular expression used to filter files.
98
#' @param env Environment in which to evaluate code.
109
#' @param chdir Change working directory to `dirname(path)`?
11-
#' @param encoding Deprecated
10+
#' @param encoding Deprecated.
1211
#' @param wrap Automatically wrap all code within [test_that()]? This ensures
1312
#' that all expectations are reported, even if outside a test block.
1413
#' @export

R/test-directory.R

+8-7
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,26 @@
4141
#' test_check("yourpackage")
4242
#' ```
4343
#'
44-
#' @param path path to tests
45-
#' @param package package name
44+
#' @param path Path to directory containing tests.
45+
#' @param package Name of installed package.
4646
#' @param filter If not `NULL`, only tests with file names matching this
47-
#' regular expression will be executed. Matching will take on the file
47+
#' regular expression will be executed. Matching be performed on the file
4848
#' name after it has been stripped of `"test-"` and `".R"`.
4949
#' @param ... Additional arguments passed to [grepl()] to control filtering.
5050
#' @param stop_on_failure If `TRUE`, throw an error if any tests fail.
5151
#' @param stop_on_warning If `TRUE`, throw an error if any tests generate
5252
#' warnings.
5353
#' @inheritParams test_file
54-
#' @return The results of the reporter function on all test results.
55-
#' @return The results as a "testthat_results" (list)
54+
#' @return A list of test results.
5655
#' @export
5756
#' @examples
58-
#' \dontrun{test_package("testthat")}
57+
#' test_dir(testthat_examples(), reporter = "summary")
58+
#' test_dir(testthat_examples(), reporter = "minimal")
5959
test_dir <- function(path,
6060
filter = NULL,
6161
reporter = default_reporter(),
62-
env = test_env(), ...,
62+
env = test_env(),
63+
...,
6364
encoding = "unknown",
6465
load_helpers = TRUE,
6566
stop_on_failure = FALSE,

R/test-files.R

+26-10
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,37 @@ find_test_scripts <- function(path, filter = NULL, invert = FALSE, ...) {
8181
filter_test_scripts(files, filter, invert, ...)
8282
}
8383

84-
#' Run all tests in specified file.
84+
#' Run all tests in specified file
8585
#'
86-
#' @param path path to file
87-
#' @param reporter reporter to use
88-
#' @param env environment in which to execute the tests
86+
#' Execute code in the specified file, displaying results using a `reporter`.
87+
#' Use this function when you want to run a single file's worth of tests.
88+
#' You are responsible for ensuring that the functions to test are available
89+
#' in the global environment.
90+
#'
91+
#' @param path Path to file.
92+
#' @param env Environment in which to execute the tests. Expert use only.
8993
#' @param load_helpers Source helper files before running the tests?
90-
#' @param encoding File encoding, default is "unknown"
91-
#' `unknown`.
94+
#' See [source_test_helpers()] for more details.
95+
#' @param encoding Deprecated. All files now assumed to be UTF-8.
9296
#' @inheritParams with_reporter
9397
#' @inheritParams source_file
94-
#' @return the results as a "testthat_results" (list)
98+
#' @return Invisibily, a list with one element for each test.
9599
#' @export
96-
test_file <- function(path, reporter = default_reporter(), env = test_env(),
97-
start_end_reporter = TRUE, load_helpers = TRUE,
98-
encoding = "unknown", wrap = TRUE) {
100+
#' @examples
101+
#' path <- testthat_example("success")
102+
#' test_file(path, reporter = "minimal")
103+
#'
104+
#' # test_file() invisibly returns a list, with one element for each test.
105+
#' # This can be useful if you want to compute on your test results.
106+
#' out <- test_file(path, reporter = "minimal")
107+
#' str(out[[1]])
108+
test_file <- function(path,
109+
reporter = default_reporter(),
110+
env = test_env(),
111+
start_end_reporter = TRUE,
112+
load_helpers = TRUE,
113+
encoding = "unknown",
114+
wrap = TRUE) {
99115
library(testthat)
100116

101117
if (!file.exists(path)) {

inst/examples/test-failure.R

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
plus <- function(x, y) 1 + 1
2+
3+
test_that("one plus one is two", {
4+
expect_equal(plus(1, 1), 2)
5+
})
6+
7+
test_that("two plus two is four", {
8+
expect_equal(plus(2, 2), 4)
9+
})

inst/examples/test-success.R

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
test_that("one plus one is two", {
2+
expect_equal(1 + 1, 2)
3+
})
4+
5+
test_that("you can skip tests if needed", {
6+
skip("This tests hasn't been written yet")
7+
})
8+
9+
test_that("some tests have warnings", {
10+
expect_equal(log(-1), NaN)
11+
})
12+
13+
test_that("some more successes just to pad things out", {
14+
expect_true(TRUE)
15+
expect_false(FALSE)
16+
})

man/Reporter.Rd

+20-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/default_reporter.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/reporter-accessors.Rd

+16-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)