Sub topic 14
Testing against different configurations
Assume you know basic Dart and Flutter testing, can run flutter test, and have an AppConfig class that holds values such as apiBaseUrl and enableLogging. The safest testing approach is to test app behavior with an explicit AppConfig, then run a smaller number of checks for each compiled environment.
Test configuration as data
Most unit and widget tests should not depend on the developer's current environment. Construct the configuration directly and pass it into the object under test.
import 'package:flutter_test/flutter_test.dart';
import 'package:my_app/app_config.dart';
void main() {
test('the API client receives the configured base URL', () {
const config = AppConfig(
apiBaseUrl: 'https://api.test.example.com',
enableLogging: false,
);
expect(config.apiBaseUrl, 'https://api.test.example.com');
expect(config.enableLogging, isFalse);
});
}This test is quick and deterministic. It does not need Android product flavors, an iOS scheme, or a network connection.
If your application creates services from AppConfig, inject a fake service in widget tests. This keeps the test focused on app behavior rather than on a real development or production server.
Test compile-time values separately
String.fromEnvironment and bool.fromEnvironment read values compiled into the test process. Run the same test with different defines when you need to verify the environment wiring.
import 'package:flutter_test/flutter_test.dart';
import 'package:my_app/app_config.dart';
void main() {
test('the selected environment is compiled into AppConfig', () {
expect(
AppConfig.fromEnvironment.apiBaseUrl,
'https://api.staging.example.com',
);
expect(AppConfig.fromEnvironment.enableLogging, isTrue);
});
}Run it with individual values:
flutter test \
--dart-define=API_BASE_URL=https://api.staging.example.com \
--dart-define=ENABLE_LOGGING=trueOr use the same file used by your build:
flutter test --dart-define-from-file=config/staging.jsonA test that expects staging values must not run without those defines. Otherwise it may read the default URL and produce a misleading failure.
Use a small configuration matrix
Do not repeat every unit test for every environment. Test shared application behavior once with injected configuration. Add a smoke test for each supported configuration to check that its values, assets, and native setup are connected correctly.
| Test level | What it checks | Native flavor required |
|---|---|---|
| Unit test | Configuration and service logic | No |
| Widget test | UI behavior with injected settings | No |
| Compile-time test | `--dart-define` mapping | No |
| Device smoke test | App name, icon, bundle ID, and startup | Yes |
Check native flavors on a device
A Dart test cannot prove that Android installed the correct application ID or that iOS selected the correct scheme. Run each important flavor and verify:
- The app starts with the expected API endpoint.
- The displayed app name is correct.
- The icon and bundle or application ID are correct.
- A dev build can be installed beside a prod build.
- Production logging and debug controls are disabled.
Keep these checks small. Their purpose is to catch wiring errors between Dart defines, native flavors, assets, and service files.
Exercise
Exercise
Which test should verify that API_BASE_URL is read from config/staging.json, and which test should verify that the staging app has the correct Android application ID?
Show answer
A compile-time Flutter test should verify the URL from the configuration file. A device smoke test using the staging native flavor should verify the Android application ID.