-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path07-tests.Rmd
85 lines (53 loc) · 1.49 KB
/
07-tests.Rmd
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
```{r echo = FALSE}
knitr::opts_chunk$set(
comment = "#>",
warning = FALSE,
message = FALSE
)
```
# Tests {#tests}
Tests are really important to a healthy package.
Some people even start off with tests! 😮 see [TDD, or "test driven development"](https://en.wikipedia.org/wiki/Test-driven_development)
## test infrastructure
### Exercise: set up test infrastructure
Create manually, or run
```r
usethis::use_testthat()
```
which:
- creates `tests/testthat.R`
- creates `tests/testthat/`
- adds `testthat` package as a Suggested package
## write some tests
### Exercise: write tests
Run
```r
usethis::use_test('file-name')
```
Or create a file in `tests/testthat/` of the form `test-foobar.R`
Within the test file, if you ran `use_test()` you'll have something like:
```r
context("test-stuff")
test_that("multiplication works", {
expect_equal(2 * 2, 4)
})
```
Add a test like that above if you don't have it already.
## run tests
### Exercise: run tests
from the command line
```sh
Rscript -e 'devtools::test()'
```
from R
```r
devtools::test()
```
Or buttons in IDE
## structure of a test file
- `context` states the overall goal of the file, e.g., tests for function `helloworld`
- `context` can be repeated whenever you like
- `context` are the things printed when you run tests
- `test_that` blocks, or function calls, can have any code run and at least one test expectation, e.g., `expect_equal`
## further reading
[the tests chapter](http://r-pkgs.had.co.nz/tests.html)