Skip to content

Writing tests — start here

This guide teaches you to write automated tests for a Roku/BrightScript project from scratch. It assumes you have never used Rooibos, BrighterScript, or any BrightScript test tool before. No prior testing experience is required — we define every term the first time it appears.

What is a unit test?

A unit test is a small program that runs one piece of your code with known inputs and checks that it produces the expected output. If the check fails, the test "fails" and tells you what went wrong. Tests let you change code confidently: run them, and if they still pass, you probably didn't break anything.

A trivial example, in plain English:

Given add(2, 3), I expect the result to be 5. If it isn't, fail and show me what I got instead.

That "I expect … to be …" line is called an assertion. A test is mostly a few lines of setup followed by one or more assertions.

The mental model for this project

You write tests once, in Rooibos syntax, and run them two ways:

  • Headless is your everyday loop: instant feedback, runs in CI, no hardware. Nearly everything lives here — including @SGNode node tests and code coverage (--coverage), all with no device.
  • Device is the fidelity reference: it runs everything on real hardware, and --cross-check diffs the two lanes so you can trust the fast one. Only behavior that leans on real render-thread timing needs it.

The same file works in both. You write one kind of test; the rare one that genuinely needs hardware you mark @deviceOnly, and the fast lanes skip it.

Vocabulary you'll see

TermMeaning
AssertionA check like m.assertEqual(actual, expected). A failed assertion fails the test.
Test (@it)One scenario with a name and some assertions.
Group (@describe)A named bunch of related tests.
Suite (@suite)A class holding groups and tests, usually one per file.
Spec fileA *.spec.bs file containing one or more suites.
Fixture / setupData or state prepared before a test runs.
Test doubleA stand-in for a real dependency — a mock, stub, or spy.
HeadlessRunning without a Roku device (on a simulator in Node).

How this guide is organized

Read in order the first time; later, use it as a reference.

  1. Your first test — set up a project and watch a test pass, then fail, then pass.
  2. Anatomy of a test file — every line of a spec, explained.
  3. Assertions — the full checklist of m.assert* methods.
  4. Organizing tests — suites, groups, and file layout.
  5. Parameterized tests — run one test over many inputs.
  6. Setup & teardown — shared fixtures and cleanup.
  7. Mocks, stubs & spies — isolating the unit under test.
  8. SceneGraph & async tests — testing nodes (they run headless).
  9. Headless vs device — what runs where, and designing for it.
  10. Cookbook — copy-paste recipes for common situations.
  11. Common mistakes — the errors everyone hits, and their fixes.

Prerequisites

You need brighttest installed and a project set up. If you haven't done that, do the Quick start first (about five minutes), then come back here.