-
Notifications
You must be signed in to change notification settings - Fork 1
/
testing_slides.Rmd
166 lines (113 loc) · 4.2 KB
/
testing_slides.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
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
---
title: "Introduction to testing R code"
subtitle: "https://stirlingcodingclub.github.io/code_testing"
author: "Brad Duthie"
date: "22 March 2023"
output:
beamer_presentation:
theme: "default"
colortheme: "default"
fonttheme: "default"
ioslides_presentation: default
slidy_presentation: default
header-includes:
- \usepackage{hyperref}
- \usepackage{tikz}
- \usepackage{caption}
- \definecolor{links}{HTML}{2A1B81}
- \hypersetup{colorlinks,linkcolor=,urlcolor=links}
colorlinks: true
linkcolor: blue
urlcolor: blue
---
```{r, echo = FALSE}
library(knitr);
opts_chunk$set(echo = FALSE);
```
## Introduction: Why bother testing your code?
\pause
- You already do this informally (checking if a function works) \pause \newline
- Code that initially works might not later under different conditions \pause \newline
- Encourages better coding (writing shorter, more manageable functions) \pause \newline
- Reassuring to double-check that *everything* works after changing something \pause \newline
- Gratifying to watch code pass multiple automated tests (it looks cool).
## Testing all of your code at once is satisfying
<center><br>
![](images/gmse_test.png){width=70%}
</center><br>
## Getting started: install the testthat package
Can install [testthat](https://github.com/r-lib/testthat) from [CRAN](https://cran.r-project.org/).
```{r, eval = FALSE, echo = TRUE}
install.packages("testthat")
```
**Or** install from GitHub with the [devtools](https://github.com/r-lib/devtools) R package.
```{r, eval = FALSE, echo = TRUE}
devtools::install_github("r-lib/testthat");
```
Load [testthat](https://github.com/r-lib/testthat) into Rstudio just like any other R package.
```{r, eval = TRUE, echo = TRUE}
library(testthat);
```
## Two simple functions to be tested
**Consider one [R script](https://github.com/StirlingCodingClub/SCC_R_package/blob/master/R/temp_conversion.R) (file with .R extension) with functions.**
\hrule \vspace{3 mm}
*Function 1*: converts a temperature from Fahrenheit to Celsius.
```{r, echo = TRUE}
F_to_C <- function(F_temp){
C_temp <- (F_temp - 32) * 5/9;
return(C_temp);
}
```
\pause
*Function 2*: converts from Celsius to Fahrenheit.
```{r, echo = TRUE}
C_to_F <- function(C_temp){
F_temp <- (C_temp * 9/5) + 32;
return(F_temp);
}
```
## Two simple functions to be tested
**Consider one [R script](https://github.com/StirlingCodingClub/SCC_R_package/blob/master/R/temp_conversion.R) (file with .R extension) with functions.**
\hrule \vspace{3 mm}
*Function 1*: converts a temperature from Fahrenheit to Celsius.
```{r, echo = TRUE}
F_to_C(50)
```
\pause
*Function 2*: converts from Celsius to Fahrenheit.
```{r, echo = TRUE}
C_to_F(10)
```
## How the `test_that` function works
Example of a testthat R script, < [test-temp_conversion.R](https://github.com/StirlingCodingClub/code_testing/blob/master/test-temp_conversion.R) >
```{r, echo = TRUE, eval = FALSE}
library(testthat);
context("Temperature function testing");
source("temp_conversion.R"); # Functions to test
test_that("Fahrenheit to Celsius", {
temp_C <- F_to_C(50);
# Test that the result is numeric
expect_that( is.numeric(temp_C), equals(TRUE) );
# Test that the result is the correct value
expect_that( temp_C, equals(10) );
})
```
## How to run many tests quickly
**Check Rstudio is set to the same directory as the test R script(s)**
```{r, eval = FALSE, echo = TRUE}
test_dir("."); # Runs all of your tests
```
The above will run all files with the prefix `test-` and extension `.R`.
\vspace{8 mm} \hrule \vspace{3 mm} \pause
**Test output looks like this:**
<center><br>
![](images/test_output.png)
</center><br>
## Testing as part of your coding work flow
**Hadley Wickham also [makes several key points](http://r-pkgs.had.co.nz/tests.html) about testing**
- Helps spot bugs in the code earlier and see what needs fixing
- Multiple unit tests encourages smaller, manageable functions
- Writing failing tests can be useful when fixing bugs \pause
**Additional resources**
- [Example of unit testing R code with testthat](https://www.johndcook.com/blog/2013/06/12/example-of-unit-testing-r-code-with-testthat/) (John D. Cook)
- [Testing R packages](http://r-pkgs.had.co.nz/tests.html) (Hadley Wickam)