Testing Flutter/Flame games and running them in GitLab CI
fluttertestingflame-testmocktailgladosproperty-testinggitlab-cipre-commit
A practical test stack for a Flutter game, what to test first, and the GitLab CI and git-hook setup Run uses (tests grew from 0 to 170 between 2026-03 and 2026-10).
Stack
flame_test:testWithFlameGame,testGolden,testRandom.mocktail(no codegen):class MockAudio extends Mock implements AudioController {};SharedPreferences.setMockInitialValues({})for prefs.glados(Dart QuickCheck with shrinking) +flutter_gladosarbitraries. Limits: at most 3 generated parameters, no async tests; useany.doubleInRange()to avoid NaN/infinity; write generators for your own types (vectors, states, cells).
What to test first (value per effort)
- Pure physics (
MovementSystemwith no engine deps): plain unit tests. - Simulations (liquid automata): property tests for invariants (velocity caps, bounds, collision symmetry, mass conservation).
- Hazard and ability logic via
testWithFlameGame. - Golden tests for themes/backgrounds (avoid text: fonts differ across platforms; goldens
need
xvfbin headless CI). - Serialization round trips: build every entity with non-default values, serialize, parse,
compare; plus a golden parse of every built-in level. This catches the "forgotten field"
class (Game content data models (stable ids, schemas, serialization)).
Extract math into pure static functions so it can be tested (Run's editor
panBy/zoomAbouthad a double-counted pinch until unit-tested). A sensible coverage target for a game is 30-50%, focused on logic, not rendering.
Gotchas
defaultTargetPlatformis Android underflutter test; platform checks need a debug override (Serving a phone-friendly Flutter web app from the same URL).- Widgets listening to plugin
EventChannels (gamepads) throw unhandled stream errors in tests. - Import your own code as
package:<pubspec name>/..., which may differ from the app id. flutter testpiped into another command loses the exit code.
Local hooks
A pre-commit hook runs dart format on staged files and flutter analyze --no-pub, blocking
on errors (not on info lints). A Claude Code PreToolUse hook meant to run an architect review
before git commit never effectively blocked anything: with a 10 s timeout and no
permissionDecision: "ask" the commit was simply allowed. Review agents are run manually
(/review) instead.
GitLab CI
- Image
ghcr.io/cirruslabs/flutter:<pinned version>(:stabledrifts). - Stages: analyze -> format (allow_failure, the local formatter's "tall style" can disagree with CI's version) -> test (with coverage) -> build:web -> deploy.
- Cache
.pub-cache/and.gradle/via project-relativePUB_CACHE/GRADLE_USER_HOME; disable the Gradle daemon (org.gradle.daemon=false). - Android signing in CI: base64 keystore in a protected variable, decoded at build time; build
numbers from
$CI_PIPELINE_IID. - Free tier is 400 CI minutes/month (~65 pipelines of ~7-8 min); a self-hosted runner on the VPS removes the cap.
- Analyzer warnings failed Run's pipeline at first (unused imports, missing super calls); fix them rather than relaxing the job.
- Check the last pipeline before pushing, not after: pipelines take minutes, and polling results arrive after you have moved on. If a required job failed, read its log via the GitLab API (token in a file, project id in project notes) and fix before stacking commits.
See also
- Flutter and Dart gotchas (collections, widgets, theming)
- Android unit and instrumentation testing gotchas