-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathREADME.Rmd
More file actions
189 lines (138 loc) · 5.39 KB
/
Copy pathREADME.Rmd
File metadata and controls
189 lines (138 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "",
fig.path = "man/figures/README-",
out.width = "100%",
eval = FALSE
)
# Copy reference/images to man/images
# reference folder is required to work with pkgdown
if (!dir.exists("man/figures")) {dir.create("man/figures")}
file.copy(list.files("reference/figures", full.names = TRUE),
"man/figures", overwrite = TRUE)
```
# testdown <img src="man/figures/logo.png" align="right" alt="" width="120" />
<!-- badges: start -->
[](https://www.tidyverse.org/lifecycle/#maturing)
[](https://github.com/ThinkR-open/testdown/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
The goal of `{testdown}` is to generate a bookdown report of `{testthat}` results
## Installation
You can install the dev version of `{testdown}`
- from r-universe
```r
# Enable universe(s) by thinkr-open
options(repos = c(
thinkropen = 'https://thinkr-open.r-universe.dev',
CRAN = 'https://cloud.r-project.org'))
# Install some packages
install.packages('testdown')
```
- from GitHub with:
``` r
remotes::install_github("ThinkR-open/testdown")
```
## About
This package has two exported functions:
### `test_down()`
This function turns the `{testthat}` results into a `{bookdown}` report.
It takes:
+ A `project_name`, which is the name you want to give to the project.
The default is `NULL`, which will then be converted to `basename(here::here())`.
+ An `author`, if you want your report to be have an author name.
Default is NULL, and then the report won't have any name on it.
+ `pkg`, the path to the package that will be documented.
Default is `here::here()`
+ `environment`, a name for the testing environment.
Default is `here::here()`
+ `book_path`, the folder where you want the `{bookdown}` report to be created.
Default is `"tests/testdown"`.
+ `with_help` Should the help appendix be added?
Default is `TRUE`.
+ `open` Should the report be opened once compiled?
Default is `interactive()`.
### `test_down_example()`
This function will compile the report for an example package contained inside `{testdown}`.
## Custom `{testdown}` roxygen tags
You can add a `description` to your tests using the roxygen tag `@description`.
Note that this test __must__ be on the line just before the expectation.
Here is an example:
```{r eval = FALSE}
test_that("hello_world() works", {
#' @description Testing that hello_world("colin") returns "Hello world, my name is colin!"
expect_equal(hello_world("colin"), "Hello world, my name is colin!")
#' @description Testing that hello_world("colin") returns a character vector
expect_is(hello_world("colin"), "character")
#' @description Testing that hello_world("colin") has World in it
expect_match(hello_world("colin"), "World")
#' @description Testing that hello_world("colin") has Hello in it
expect_match(hello_world("colin"), "Hello")
})
```
```{r child = "inst/testdownhelp.md", eval = TRUE}
```
## Writing custom expectation
For reliable results, all expectations should start by `expect`, notably if you need to count the skipped tests.
In other words, the skipped expectations count relies on counting all the functions starting with `expect`, so naming custom expectation differently will prevent this count from being correct.
## Known limitations
`{testdown}` report relies on `{testthat}` and the results are based on these outputs.
Here are known example of results that will be discarded by `{testthat}` and hence will make `{testdown}` behave in a weird way.
+ Using `{withr}`
```{r}
test_that("Files exist", {
with_dir(
"new/dir",
{
#' @description Checking that style.css is created
expect_file_exists("tests/testdown/style.css")
#' @description Checking that _bookdown.yml is created
expect_file_exists("tests/testdown/_bookdown.yml")
#' @description Checking that _output.yml is created
expect_file_exists("tests/testdown/_output.yml")
}
)
})
```
As testthat doesn't count the expectations from `with_dir`, this will make the `{testdown}` result weird.
Always add the expectations at the top level of your test_that.
+ Loops
Same goes with for loops:
```{r eval = FALSE}
for (i in names(df)){
#' @description Checking the names of the output are correct
expect_true(
i %in% c(
"context",
"test",
"expectation",
"description",
"location",
"test_time",
"result",
"file",
"message"
)
)
}
```
If ever you use this format, `{testthat}` won't catch the tests, so they won't be reported.
+ HTML expectations
As testdown render the text straight in html, if your expecatation contains html, it will break the rendering.
For example, the following test will break the rendering:
```{r}
expect_match(
tag,
"<h2>this</h2>"
)
```
## Sponsor
The development of this package has been sponsored by:
<a href = "https://www.servier.fr/"><img src = "man/figures/servier.png"></img></a>
## CoC
Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md).
By participating in this project you agree to abide by its terms.