You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/challenging-tests.Rmd
+5-39Lines changed: 5 additions & 39 deletions
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ knitr::opts_chunk$set(
16
16
17
17
Testing is easy when your functions are pure: they take some inputs and return predictable outputs. But real-world code often involves randomness, external state, graphics, user interaction, and other challenging elements. This vignette provides practical solutions these tricky scenarios.
18
18
19
-
In principle, it's often possible to test these things by explicitly parameterising them as arguments to your functions so you can more easily override the default values. And where possible you should do so, especially when testing internal functions. But it's often impractical to provide arguments to explicitly control every last feature without exploding user-facing interfaces. So the techniques in this vignette will help you test all your code, regardless of where it lives and what it does.
19
+
In principle, it's often possible to test these things by explicitly parameterising them as arguments to your functions so you can more easily override the default values. And where possible you should do so, especially when testing internal functions. But it's often impractical to provide arguments to explicitly control every last feature without exploding user-facing interfaces. So the techniques in this vignette will help you test all your code, regardless of where it lives and what it does. In other situations, it might not be your function that is the problem, but some other function that you can't edit directly.
20
20
21
21
This vignette is divided into sections based on the underlying tool you'll use:
22
22
@@ -39,7 +39,7 @@ library(testthat)
39
39
40
40
### Options, env vars, and working directory
41
41
42
-
If your code depends on global options, environment variables, or the working directory. In most cases, it's good practice to make these dependencies explicit by making them the default value of an argument so you can control directly in your tests. However, sometimes you are testing deeply embedded code and it would be painful to thread the values all the way through to the right place. In this case can temporarily override with withr functions:
42
+
In this case can temporarily override with withr functions:
43
43
44
44
* Temporarily change options with `withr::local_options()`.
45
45
* Temporarily change env vars with `withr::local_envvar()`.
{withr} provides a number of other `local_` helpers; see the package docs for me.
64
+
63
65
### Random numbers
64
66
65
67
Random number generation also falls into the same bucket because it depends on the value of the special `.Random.seed` variable which is updated whenever you generate a random number. You can temporarily change this seed and reproducibly generate "random" numbers with `withr::local_seed()`.
@@ -103,7 +105,6 @@ local({
103
105
local_my_helper()
104
106
getOption("x")
105
107
})
106
-
107
108
```
108
109
109
110
We strongly recommend giving such functions a `local_` prefix to clearly communicate that they have "local" effects.
When generating sophisticated error messages that use cli's interpolation and formatting features, snapshot tests are essential for ensuring the messages render correctly with proper styling and content.
0 commit comments