Başar BallıözProduct Engineer
← All posts

Headless testing in Godot 4 without a framework

Tibblo has 42 test suites and about 3,500 checks, and none of them needs a test framework or a window. Each suite is a GDScript file that boots the real game headless, taps it the way a child would, and exits with a pass or fail code. The hard parts were not the assertions. They were time, saves, and two test runs sharing one computer.

By Başar Ballıöz · · 5 min read

The short answer

To test a Godot 4 game headless: write each suite as a script that extends SceneTree, run it with godot --headless --path . --script res://scripts/my_test.gd, print one line per check, and call quit(0) or quit(1) at the end. Then solve the three problems every game test suite runs into: time (drive it yourself), input (push real events), and saves (redirect them to a sandbox and prove the real ones were not touched).

Frameworks such as GUT and gdUnit4 exist and are good. Tibblo started with plain scripts and never needed more, so this is what plain scripts can do.

The game these suites test is Tibblo, a pocket pet game for families.

1. A suite is a SceneTree script

A script that extends SceneTree can be passed to Godot with --script, and it becomes the main loop. Each Tibblo suite has the same shape:

  1. _init() defers to _run(), so the engine is fully up first;
  2. _run() starts the sandbox, then calls the suite's test functions in order, awaiting frames where the game needs them;
  3. each check prints OK: … or FAIL: … and counts failures;
  4. the suite ends with quit(0) when everything passed and quit(1) otherwise.

That is the whole framework: a check helper, an exit code and output a person can read. Because suites instantiate the real main.tscn, they test the game the player runs, not a mock of it.

2. Real input, not function calls

A test that calls _on_feed_pressed() directly proves that a function works. It does not prove that a child can press the button: that it is visible, on screen, not covered by a sheet, and big enough to hit. So the suites build a real press and release, an InputEventMouseButton pair, at the centre of the control's on-screen rectangle, and push them through the root viewport with push_input(), a frame apart. If a popup covers the button, the test's tap lands on the popup, exactly like a real tap would.

The same suites set the window size, for example a 405 × 720 phone, so layout bugs that only appear on narrow screens show up in tests too.

3. Time you control

Games are full of time: countdowns, animations, daily rewards, a pet that gets hungry. Tests cannot wait for real seconds, and they cannot depend on the date they run on.

  • Frames are awaited, not slept. Tests await process_frame a known number of times for the UI to settle.
  • Game time is stepped. Mini-games expose a step(delta) that advances the run by an exact amount, so a test can play 60 seconds of a game in a fraction of a second, and a small autopilot can play it well enough to prove a good player can win.
  • Randomness is seeded. A run can start from a fixed seed, so a failure can be reproduced.
  • Calendar features are pinned. Anything chosen by today's date, such as the game of the day, is off in the sandbox, and the suites that test it opt back in with a fixed day.
  • The device language is pinned to English, so the same suite reads the same text on a Turkish computer.

4. A save sandbox, and proof it worked

The game saves constantly, and a test that overwrites the developer's own pet is a test that will be disabled within a week. Every suite starts with TestSandbox.begin("suite_name"), which redirects all persistence to user://test_sandbox/<suite>.<pid>/, wiped first. Systems that touch the outside world check for the sandbox and stand down: the battery saver stops capping frames so frame-counted waits keep their speed, and remote settings are not fetched.

The folder name includes the process id for a reason learned the hard way. user:// is keyed by the project name, so every checkout and every session on the computer shares it. With one folder per suite, a second run of the same suite wiped the first one's save mid-test, and its taps then "missed" because an item was already owned. Old folders are cleaned up by age: a suite writes every few seconds, so a folder untouched for an hour belongs to a run that has ended.

Before the sandbox starts, it takes an MD5 hash of the real pet save, settings, account and sync files. At the end, every suite checks that the hashes are unchanged. The runner does the same around the whole run. If any real file changed, the run fails with REAL-SAVE-GUARD, whatever the suites said.

5. A runner that trusts exit codes, not pass lines

A small PowerShell script (with a Bash twin for macOS) runs the suites one by one and prints the OK and FAIL lines:

  • Script errors fail the suite even if it printed its own pass line. A GDScript error can skip half a test and still reach the happy ending.
  • A non-zero exit without a FAIL line, a crash or an engine error, prints the last lines of output so the cause is visible.
  • One run at a time. Because every checkout shares the sandbox root, the runner refuses to start while another test run is live, instead of waiting or killing anything.
  • Window checks only on request. A few pixel checks need a real renderer and open a window briefly; they run only with a flag. Everything else is headless.
  • -Only runs a named subset. After a change, I run only the suites it can affect; the full set runs at the end of a milestone.

6. What the suites actually check

Most checks are about behaviour a player would notice: a run settles once and pays once, pause freezes a mini-game, a sheet never runs past the bottom of a short screen, every translated string keeps its placeholders, a save from an old version still loads. The mini-game arcade, save migrations and translations each have their own suites.

Common questions

Can I run Godot tests without opening a window?

Yes. Start Godot with --headless and --script pointing at a script that extends SceneTree. Rendering-dependent checks need a real renderer and a window.

How do I stop tests from overwriting my real save in Godot?

Route every save path through one function that returns a sandbox path during tests, and hash the real files before and after to prove they were not changed.

How do I simulate a tap in a Godot test?

Create an InputEventMouseButton press and a matching release at the centre of the control's global rectangle, and send them with the root viewport's push_input(), awaiting a frame between them.

What I learned

Test through the screen. Pushed input catches the bugs players hit: hidden, covered and off-screen buttons.

Own time completely. Stepped time, seeded randomness and pinned dates turned flaky tests into reliable ones.

Make the safety net prove itself. The real-save guard is one hash comparison, and it is the reason I trust running tests on the same computer I play on.

More posts

LLM API cost control per user: how RPMBase keeps the AI bill boundedHow RPMBase keeps OpenAI costs bounded per user: server-side limits, a worst-case cost reservation, atomic budgets in MongoDB, shared pools for free accounts and a hard cap. Keyword cannibalization in practice: one query, three moves, zero impressionsA real case from RPMVault: moving "motorcycle maintenance app" between pages three times cost its impressions. The query ownership rules, title limits and lock test I use now.