Note 23 / Flutter / Testing / CI
Flutter tests that pass on my Mac and fail on CI: Platform.isIOS reads the machine running the test
A test green on macOS went red on Ubuntu CI because the rule read dart:io Platform. Why defaultTargetPlatform behaves differently in tests, and the override pattern.
- By
- Trần Thế Anh, Software Engineer
- Published
- Work done
- October 2026, AI Note Taker (Flutter)
- Stack
- Flutter, flutter_test, dart:io Platform, defaultTargetPlatform, GitHub Actions on Ubuntu
- Reading
- 7 min, 1,365 words
On the Ubuntu CI run of AI Note Taker’s unit tests, one test failed that was green on my Mac. It was a test for Sign in with Apple: the helper that opens the Apple sheet returned null, and the assertion that expected a credential failed, at the nonce test. The helper had decided it was not on an Apple platform, and for the runner it was right, because the runner was Ubuntu.
Where the tests run
The release workflow has a test job that calls a reusable job running flutter test on an Ubuntu 24.04 runner, and the build jobs list it in needs, so a red test blocks the build. The Flutter version there is pinned in the workflow to 3.44.8. My laptop has 3.47.9, and the framework source quoted below is from that checkout; I did not read the 3.44.8 source, so the exact lines may differ there.
The rule that read the wrong machine
The helper decides whether Sign in with Apple is available. The first version was this:
static bool get appleAvailable =>
!kIsWeb && (Platform.isIOS || Platform.isMacOS);
Platform here is dart:io. It reports the operating system of the process that is executing. In production that is the phone, so the rule is correct. In a unit test it is the machine running flutter test: macOS on my desk, Linux on CI. With Platform.isMacOS in the condition, my Mac made the rule true and the test that expected a credential passed. On Ubuntu the same line was false, so the helper returned null.
The push service had the same shape, a check of Platform.isIOS before it would register for notifications. That one is a different failure and a worse one. Platform.isIOS is false both on macOS and on Linux, so on neither machine could a test reach the iOS branch at all. I am reading that off the semantics of dart:io, not off a test I ran, but it matches how the code behaved: no test of that branch had been possible before the change.
What Flutter does in tests
Flutter has a second way to ask the same question, defaultTargetPlatform from foundation. In the Flutter source on my machine, the implementation for dart:io platforms first maps the host OS to a TargetPlatform, then, in debug mode, replaces the result with android if the FLUTTER_TEST environment variable is set, and then replaces it with debugDefaultTargetPlatformOverride if that is not null. The doc comment in the framework says the same thing in words: in a test environment the platform returned is Android regardless of the host.
That makes the rule independent of the host. The default in a test is the same on Mac and on Linux, so a test cannot pass on one and fail on the other because of the operating system. The official documentation describes the override as a debug-build setting that is generally useful only for tests, and says that setting it back to null restores the real value.
The fixed helper reads:
static bool get _onApplePlatform =>
!kIsWeb &&
(defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.macOS);
and the test chooses the platform it wants:
test('on iOS Apple receives the hashed nonce', () async {
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
final AppleCredential? credential;
try {
credential = await SocialSignIn.appleCredential();
} finally {
debugDefaultTargetPlatformOverride = null;
}
expect(credential, isNotNull);
});
A second test loops over android and linux and expects appleAvailable to be false and appleCredential() to return null without opening the sheet. Before the change, “not on an Apple platform” was something the test could only observe by luck on a Linux runner. Now it is an explicit case that runs everywhere.
Reset inside the test body, not in a tear-down
The finally block is deliberate. The override is a global; if a test leaves it set, the next test in the same isolate sees the wrong platform, and nothing points back to the test that caused it. In testWidgets, flutter_test goes further. In the binding source I read, the invariant check calls debugAssertAllFoundationVarsUnset right after the test body runs, and it fails the test if a foundation debug variable is still changed. The usual instinct is to reset in addTearDown. My own comment in the test file says tear-downs run after that check, which is why the reset sits in a finally inside the body. I confirmed the position of the invariant check in the Flutter source; I did not trace the tear-down ordering in package:test myself, so that half rests on the comment and on the fact that the tests passed. The tests in this file use plain test, where the leak would be silent, which is the more dangerous case.
I also wrote the lesson into the header of the test file so the next test follows the same rule: no test may depend on the operating system of the machine running it, and the platform is set by the override and restored in finally.
Proving the fix and scanning the class
For the check I used two things. One was a mutation: I took the old code and made the test host look like Linux, and the Apple test went red; with the fix it is green. The other was a second test case, the push lifecycle test, which sets the platform to iOS to exercise a path that was previously unreachable. The full suite then passed, 186 tests at the time. Both facts are from the lesson I recorded then. The original red on Ubuntu is also from that record, a CI failure at the Apple nonce test, not something I re-ran on a Linux machine for this write-up.
The class scan is a grep:
grep -rn "Platform\.is" lib
It does not come back empty today, and I would rather say so. Ten code lines still use dart:io for a platform decision: avatar compression, the rating sheet, the platform string of one request, the host rewrite for the Android emulator in the API config (kept on purpose, it is a development-only path), four lines in the purchases helper, and two in the feedback mailer. Apart from the purchases key picker, these only test isIOS or isAndroid, which are false on both macOS and Linux, so both machines get the same value. That is why they have not failed on CI, but it is not a design I trust: the iOS or Android branch is simply unreachable in a test, the same weakness the push service had. The key picker also names Platform.isMacOS, so it can differ by host if a key is passed at build time; with no --dart-define both hosts return an empty string, and I have not checked a case with a key set.
Where a function needs to be tested on both branches, the code can take the platform as a parameter. One such function in the billing repository already does, and a comment there says it takes the platform as an argument for that reason. That is cleaner than the override when the logic is pure.
What I would add to the gate
A lint would catch the class before CI does: a custom rule or a CI grep that fails on Platform.is outside a small allow-list. I have not added it. What I did add is the comment on the helper naming the trap, so the next person who types Platform.isIOS in a rule sees why defaultTargetPlatform is used there. If you work on Flutter with CI on Linux and development on macOS, run the grep on your own codebase first, and then look at which of the hits include isMacOS, because those are the lines that already differ between the two machines.
The push service shows the same pattern from another angle, and its story is in Push tokens and several accounts on one phone.
Sources
- Flutter API,
debugDefaultTargetPlatformOverride(debug-build override, null restores the real value, generally only useful for tests): https://api.flutter.dev/flutter/foundation/debugDefaultTargetPlatformOverride.html - Flutter framework source,
packages/flutter/lib/src/foundation/platform.dart(doc comment: in a test environment the platform isandroidregardless of the host),foundation/_platform_io.dartandpackages/flutter_test/lib/src/binding.dart, read in a local Flutter 3.47.9 checkout (https://github.com/flutter/flutter).