-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.qmd
More file actions
325 lines (231 loc) · 11.3 KB
/
testing.qmd
File metadata and controls
325 lines (231 loc) · 11.3 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
---
title: "Testing Your Module"
---
Every JASP module must have unit tests. Tests catch regressions, verify tables and plots produce expected output, and run automatically in CI on every push.
## Framework
JASP uses `testthat` through the `jaspTools` package, which wraps `testthat` with JASP-specific helpers for running analyses, comparing tables, and validating plots.
### Setup
```r
# Install jaspTools (one-time)
remotes::install_github("jasp-stats/jaspTools")
library(jaspTools)
```
For interactive debugging of analyses with `jaspTools` (including `browser()`), see @sec-jasptools-workflow.
## Test File Structure
```
tests/
├── testthat.R # Runner script
└── testthat/
├── test-analysisname.R # One file per analysis
├── _snaps/ # Auto-generated plot snapshots
│ └── test-analysisname/
│ └── plot-name.svg
└── jaspfiles/ # Test data (recommended)
├── library/ # Datasets from the JASP data library
├── verified/ # Verified .jasp example files
└── other/ # Additional test data
```
Test files follow the pattern: `test-{source}-{filename}.R`, where `{source}` indicates the data origin (e.g., `test-verified-ttest.R`, `test-library-BinomialTest.R`).
## Recommended: Generate Tests from Example Files {#sec-example-tests}
The preferred way to create tests is to **auto-generate them from `.jasp` example files**. This approach:
- Ensures your examples are always tested
- Produces consistent, comprehensive test coverage with minimal effort
- Keeps tests in sync with the actual user-facing examples
### Step 1: Add .jasp Files
Place your example `.jasp` files in the appropriate subfolder:
| Folder | Contents |
|--------|----------|
| `tests/testthat/jaspfiles/verified/` | Files from the JASP verification project |
| `tests/testthat/jaspfiles/library/` | Files from the JASP data library |
| `tests/testthat/jaspfiles/other/` | Other example files for testing |
### Step 2: Generate Tests
```r
library(jaspTools)
jaspTools::makeTestsFromExamples("jaspTTests")
```
This reads each `.jasp` example, extracts the options, runs the analysis, and generates test code. Generated test files are named `test-{source}-{filename}.R`.
### Step 3: Review
Always eyeball the generated tests. There are edge cases where `makeTestsFromExamples()` fails due to complex variable encoding (e.g., some SEM syntax, ordinal constraints). Add `skip()` for those cases:
```r
test_that("Complex SEM model runs", {
skip("Complex variable encoding not yet supported by makeTestsFromExamples")
# ...
})
```
### Step 4: Keep in Sync
When you update an example `.jasp` file, regenerate the corresponding test. The `verified/` folder is protected from accidental overwrites by default.
::: {.callout-important}
### Migrate Existing Tests
If your module still has the old `examples/` folder layout, move your `.jasp` files into `tests/testthat/jaspfiles/`, delete the old auto-generated test files, and re-run `makeTestsFromExamples()`. Make sure you have the latest `jaspTools` installed.
:::
## Writing Tests Manually
For cases not covered by example files — edge cases, specific error conditions, or fine-grained option combinations — write tests manually.
### Basic Structure
```r
test_that("Independent Samples T-Test produces correct table", {
options <- analysisOptions("TTestIndependentSamples")
options$dependent <- "contNormal"
options$groupingVariable <- "facGender"
options$effectSize <- TRUE
results <- runAnalysis("TTestIndependentSamples", "test.csv", options)
table <- results[["results"]][["ttest"]][["data"]]
jaspTools::expect_equal_tables(table, list(
-0.153, 0.878, "contNormal", -0.214, 99, 0.831
))
})
```
### Setting Up Options
Use `analysisOptions()` to get an options list pre-populated with defaults:
```r
options <- analysisOptions("TTestIndependentSamples")
# Then override only what you need:
options$dependent <- "contNormal"
options$meanDifference <- TRUE
```
### Table Tests
`expect_equal_tables()` compares the analysis output table to a reference list of values:
```r
jaspTools::expect_equal_tables(table, list(
"value1", "value2", "value3" # expected cell values in row order
))
```
To generate the expected values list automatically:
```r
# Run the analysis, then:
jaspTools::makeTestTable(table)
# Prints a list(...) you can paste into your test
```
You can also bootstrap a manual test file by setting `makeTests = TRUE`:
```r
results <- runAnalysis("TTestIndependentSamples", "test.csv", options,
makeTests = TRUE)
# Prints boilerplate test code to the console — copy, refine, and save
```
### Plot Tests
Plot tests use SVG snapshot comparison:
```r
test_that("T-Test plot matches", {
options <- analysisOptions("TTestIndependentSamples")
options$dependent <- "contNormal"
options$groupingVariable <- "facGender"
options$descriptivesPlots <- TRUE
results <- runAnalysis("TTestIndependentSamples", "test.csv", options)
plotName <- results[["results"]][["descriptives"]][["collection"]][["descriptives_descriptivesPlot"]][["data"]]
testPlot <- results[["state"]][["figures"]][[plotName]][["obj"]]
jaspTools::expect_equal_plots(testPlot, "descriptives-plot")
})
```
On first run, the reference SVG is created in `_snaps/`. Subsequent runs compare against it.
### Managing Plot Snapshots
When a plot intentionally changes, update the reference:
```r
jaspTools::manageTestPlots()
```
This opens a Shiny app showing old vs. new plots. Accepting a change updates the SVG snapshot.
### Structural Plot Fallback {#sec-plot-fallback}
Sometimes plot tests pass locally but fail on GitHub. This happens because SVG snapshots are compared as exact text — tiny cross-platform rendering differences (e.g., a coordinate `250.678901` vs `250.678903`) are enough to make the comparison fail, even though the plots look identical. If you notice plot failures on a GitHub PR while your local `testAll()` is fine, you can use the **structural fallback**.
The fallback works by comparing the underlying ggplot data, labels, scales, and layers (instead of exact SVG text). If these match, the test passes with a warning:
```
Warning: vdiffr mismatch for 'my-plot-subplot-1' accepted by structural fallback.
```
To enable it, generate `.rds` reference files that store the plot structure:
```r
options(jaspTools.plotStructure.update = TRUE)
jaspTools::testAll()
options(jaspTools.plotStructure.update = FALSE)
```
The resulting `.rds` files are automatically stored in `tests/testthat/_snaps/<context>/reference_plotobject/`. After committing/pushing these files, failing SVG comparisons will fall back to comparing against these `.rds` files.
To inspect what GitHub sees differently, download the `snapshot-diffs` artifact from the Actions run page. Place the `.new.svg` files in the `_snaps/<context>/` folder and run `testthat::snapshot_review("<context>/")` to compare them side-by-side against your local reference SVGs.
### Testing Errors and Validation
```r
test_that("T-Test gives validation error with zero-variance variable", {
options <- analysisOptions("TTestIndependentSamples")
options$dependent <- "debMiss30"
options$groupingVariable <- "facGender"
results <- runAnalysis("TTestIndependentSamples", "test.csv", options)
expect_identical(results[["status"]], "validationError")
})
```
## Running Tests
```r
# All tests in the module
jaspTools::testAll()
# A single analysis
jaspTools::testAnalysis("TTestIndependentSamples")
```
### Debugging Failures
| Symptom | Cause | Fix |
|---------|-------|-----|
| **Failure** (values differ) | Output changed intentionally | Update reference with `makeTestTable()` / `manageTestPlots()` |
| **Failure** (values differ) | Unintentional regression | Fix the R code |
| **Error** (test crashes) | R code throws an exception | Run the analysis interactively in RStudio to debug |
| **Plot failure** | SVG differs | Run `manageTestPlots()` to review; accept if change is intentional |
## GitHub Actions CI {#sec-ci}
Every JASP module should have a CI workflow that runs tests on push and PR:
```yaml
# .github/workflows/unittests.yml
name: Unit Tests
on: [push, pull_request]
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, macOS-latest]
steps:
- uses: actions/checkout@v4
- uses: jasp-stats/jasp-actions/setup-test-env@master
- uses: jasp-stats/jasp-actions/run-unit-tests@master
```
If your module requires JAGS:
```yaml
- uses: jasp-stats/jasp-actions/setup-test-env@master
with:
requiresJAGS: true
```
## Test Coverage {#sec-coverage}
### Coverage Goals
| Module type | Minimum coverage |
|------------|-----------------|
| Official JASP module | ≥ 70% of analyses have tests |
| Community module | At least one test per analysis that runs without error |
For the full module checklists, see @sec-publishing.
### Adding a Coverage Action
Every official JASP module should include a coverage workflow to track which parts of the code are covered by unit tests. This helps maintainers identify vulnerable areas and improve coverage in a targeted manner. Setting this up takes about 5 minutes per module.
::: {.callout-note}
Make sure you have the unit test workflow (`.github/workflows/unittests.yml`, described in @sec-ci) in place before adding the coverage action.
:::
#### Step 1: Add the workflow file
Create `.github/workflows/test-coverage.yml` with the following content:
```yaml
# .github/workflows/test-coverage.yml
on:
push:
branches:
- master # Essential for Codecov baseline
paths: ['**.R', 'tests/**', '**.c', '**.cpp', '**.h', '**.hpp',
'DESCRIPTION', 'NAMESPACE', 'MAKEVARS', 'MAKEVARS.win', '**.yml']
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
paths: ['**.R', 'tests/**', '**.c', '**.cpp', '**.h', '**.hpp',
'DESCRIPTION', 'NAMESPACE', 'MAKEVARS', 'MAKEVARS.win']
jobs:
coverage:
# Run on push (merge) OR if the PR is NOT a draft
if: github.event_name == 'push' || github.event.pull_request.draft == false
uses: jasp-stats/jasp-actions/.github/workflows/coverage.yml@master
with:
needs_JAGS: false
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
```
If your module relies on JAGS, set `needs_JAGS: true` (see e.g., `jaspRegression`).
#### Step 2: Add the Codecov token
1. Go to [app.codecov.io/gh/jasp-stats](https://app.codecov.io/gh/jasp-stats) and log in via GitHub.
2. Find your repository and go to the **Configuration** tab.
3. Under **General**, locate the **Repository upload token** (see @fig-codecov-token).
4. In your GitHub repository, go to **Settings → Secrets and variables → Actions** (see @fig-github-repo-secret).
5. Add a new repository secret named `CODECOV_TOKEN` with the token value.
{#fig-codecov-token}
{#fig-github-repo-secret}
Once coverage is set up, add the coverage badge to your module's README — see @sec-module-readme for details.