Skip to content

Testing Guide

Cursor Agent edited this page Nov 29, 2026 · 5 revisions

Testing Guide

Tests are Rust tests, run by Cargo. There is no separate test binary and no CMake test runner.

cargo test --workspace
cargo test -p fanalor-physics
cargo test -p fanalor-physics ray_starts_inside
cargo clippy --workspace --all-targets -- -D warnings
cargo fmt --all

A filter after -- is a substring of the test name. -- --test-threads=1 is available when a test touches process-global state; the suites are written so they do not need it.

Where tests live

  • Unit tests sit in the file they test, in #[cfg(test)] mod tests.
  • Integration tests sit in crates/<name>/tests/ or games/<name>/tests/.
  • A test that needs a GPU goes through fanalor-testkit (headless_device, read_texture, write_png). It is skipped when the machine has no adapter, so CI without a GPU still passes. Say so in the test rather than failing the job.
  • fanalor-testkit is publish = false and only ever a dev-dependency.

Headless gameplay does not need a GPU. fanalor_core::Headless loads a scene, steps it, presses actions, feeds touches, and reads the world. games/platformer/tests/level.rs and a new game's tests/plugin.rs are the pattern. See Building Games.

Scratch files

Tests that write a temporary project use a scratch folder. The folder is removed when the test ends. Folders left by a dead process are swept, except one younger than a minute, so a test still running is not deleted out from under it.

FANALOR_KEEP_SCRATCH=1 cargo test -p fanalor-scene -- --nocapture

Any value of FANALOR_KEEP_SCRATCH keeps the folder when the test fails, which is how a bad scene file is inspected after the fact.

Text-rendering tests compare against PNGs under the scene crate's reference directory. FANALOR_BLESS=1 writes those references again instead of comparing. Bless only when the new pixels are the intended result, and commit the PNGs with the change.

The guide and the benchmarks

cargo xtask guide

This runs every shell block in docs/guide/ that sits under a <!-- ci --> marker, in order. CI runs the same command. If a documented command breaks, the build fails. The wiki's commands match that guide; the guide is what CI executes.

cargo xtask bench
cargo xtask bench --runs 3 --save target/bench/base.json
cargo xtask bench --runs 3 --compare target/bench/base.json

fanalor-bench times the hot paths in release: the sprite batch, tilemap meshing, physics, UI and text, scene parsing, a headless frame, animation sampling, interest, and lag compensation. CI's bench job fails a pull request that makes one slower than its threshold. --runs 3 keeps each benchmark's best of three (a busy machine only adds time). --compare fails on a regression. --filter batch/ runs names containing that text. --threshold 10 compares every benchmark at 10% instead of its own limit. --quick only checks that the suite runs. Reports from two different machines, or from a debug build, are refused rather than compared.

A new hot path gets a benchmark in crates/fanalor-bench/src/suite/ that builds its input in code, not from a scene file someone might edit.

Networking and the web

cargo test -p fanalor-net --test lobby --test lobby_peers --test discovery
cargo test -p arena --test match
node crates/fanalor-player/web/test/first-frame.mjs
node crates/fanalor-player/web/test/play.mjs

The arena match test starts a dedicated server and bot clients, finds the server on the LAN, readies up, and plays a round. The web scripts build with cargo xtask dist --web, serve the folder, and drive it. See Export and Networking.

Hot reload of a game crate has an ignored end-to-end test because it is slow:

cargo test -p fanalor-platformer --test hot_reload -- --ignored

What a test is expected to pin

Assert the behaviour a later edit would break silently: a stale entity handle must not read the entity that reused the slot, a sensor must not generate a contact, a release build must still log the refusal to parent a rigid body. Do not assert a log line's wording when the return value already says the same thing.

unwrap() is allowed inside tests. It is not allowed in the library and binary code those tests cover.

Clone this wiki locally