Course / Tutorials / Week 12 / Q-35
Quick tutorial Q-35 · Week 12 · about 15 min · Foundation
Three tests: normal, missing, invalid
Write the three input tests every app must pass, with assert, for a Python copy of the book's resting-heart app, then feed it a real-sized file in the right and the wrong units.
- Ask
- Collect
- Check (this week)
- Interpret
- Coach (this week)
- Reassess
Download notebook ·Python file ·Read as Markdown ·Data folder ·Chapter 12 (sections 12.4.1, 12.4.4)
Every cell runs in this page; the first run downloads Python (about 30 s). Nothing you load leaves your browser. Helpful first: Q-34 A calculation in a web page.
The decision
Before an app shows anyone a number, decide what it must do with a good file, an empty file and a file in the wrong units, and write a test for each. An app that shows a wrong number in a large font is worse than no app.
The raw data
raw/rest-rr-synthetic.csv: SYNTHETIC: 331 RR intervals in milliseconds, about 5.4 minutes, with breathing-driven variation and three planted faults. Book example data, Chapter 12 (book/ch12-app/data/rest-rr.csv, made bycode/make_rr.py). The example file of the book's student app.
Try it
Users will feed your app the wrong file, an empty file, or a file in the wrong units. Section 12.4.4 asks every app to pass three tests before anything else:
- Normal input: a good file gives the right answer, checked by hand.
- Missing input: an empty file, a header with no rows, or no
rrcolumn gives no number and a message that says what is missing. - Invalid input: values that are present but cannot be right, such as RR intervals in seconds (0.80) instead of milliseconds (800), give no number and a message that says how to fix the file.
The functions are a Python copy of the book's student app, Resting heart check: read the rr column, refuse what cannot be used, and compute RMSSD.
The functions under test
The three tests
Each test states the expected behavior first, exactly as the hand answer was written before the known-answer test. assert stops with an error if the expectation fails; silence means it passed. The normal case is the book's hand calculation: 800, 820, 790, 810 and 800 ms give an RMSSD of √450 = 21.21 ms.
Type a prediction to enable Run.
Show the expected result without running
ok - test_normal ok - test_missing ok - test_invalid normal case: RMSSD 21.21 ms
The invalid test is the one that earned its place. Before the check existed, four intervals in seconds passed the only guard ("at least three intervals"), the cleaning rule then removed all four, and the page printed NaN where the number belongs (Section 12.4.4).
The same three tests in the app's own language, for node --test, from the book's student app (test/inputs.test.js):
test('normal: the five hand-worked beats pass and give the hand answer', () => {
const rr = parseRR('rr\n800\n820\n790\n810\n800\n');
assert.equal(checkInput(rr), null);
assert.ok(Math.abs(analyze(rr).rmssd - Math.sqrt(450)) <= 0.01); // 21.21 ms
});
test('missing: an empty file, a header alone, no rr column', () => {
assert.match(checkInput(parseRR('')), /Fewer than three RR/);
assert.match(checkInput(parseRR('time,rr\n')), /Fewer than three RR/);
assert.throws(() => parseRR('time,hr\n1,60\n'), /No "rr"/);
});
test('invalid: intervals in seconds are refused, never shown as NaN', () => {
const rr = parseRR('rr\n0.80\n0.82\n0.79\n0.81\n');
assert.equal(rr.length, 4); // read, but not usable
assert.match(checkInput(rr), /in milliseconds\?/);
});
A real-sized file, right and wrong
Now the book's synthetic example file, 331 intervals, as it is and as it would arrive if a logger had written seconds.
Show the expected result without running
milliseconds 331 intervals -> ok, analyze seconds 331 intervals -> Fewer than three intervals between 300 and 2000 ms. Are the values in milliseconds?

The first file passes and goes on to the analysis; the second is refused with a message the user can act on, and no number is shown. "It does not crash" was never the test.
For the athlete: "If the app says your file is not in milliseconds, export it again from the strap's app; it will never show you a number it could not check."
Check yourself
An RR file holds 0.80, 0.82, 0.79, 0.81. What should the app do?
Why is "it does not crash" not enough for the missing and invalid tests?
Acknowledgements
The functions and the JavaScript tests follow the book's student app (book/ch12-app/code/student-app, MIT License).
Built 2026-11-30 with Python packages numpy 2.5.5, matplotlib 3.11.2. In the browser the same code runs on Pyodide, so the last digit can differ.