|
| 1 | +--- |
| 2 | +title: Test Runners |
| 3 | +--- |
| 4 | +Test runners are the methods that are used to declare and define tests. Typically, |
| 5 | +golden tests are defined with the `testWidgets()` test runner. However, `flutter_test_goldens` |
| 6 | +comes with its own test runners for convenience, e.g., `testGoldenScene()`. |
| 7 | + |
| 8 | +## The Standard Test Runner |
| 9 | +The standard test runner in `flutter_test_goldens` is `testGoldenScene()`. |
| 10 | + |
| 11 | +```dart |
| 12 | +void main() { |
| 13 | + testGoldenScene("my golden test", (tester) async { |
| 14 | + // TODO: implement golden test. |
| 15 | + }); |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | +The `testGoldenScene()` test runner wraps around Flutter's standard `testWidgets()` |
| 20 | +test runner, while making a few adjustments: |
| 21 | + |
| 22 | + * Sets the `devicePixelRatio` to `1.0` to unify all test pixel densities, and reduce scaling artifacts. |
| 23 | + * Sets the `textScaleFactor` to `1.0` to unify all test text scales, and reduce scaling artifacts. |
| 24 | + * Loads app fonts so that labels and decorations around your goldens are rendered with a real font. |
| 25 | + |
| 26 | + |
| 27 | +## Platform Specific Test Runners |
| 28 | +When writing golden tests, sometimes you may want to simulate a specific platform. For example, maybe |
| 29 | +you want to render a golden of your app with an iOS style UI, specifically. For this purpose, |
| 30 | +`flutter_test_goldens` provides per-platform test runners. |
| 31 | + |
| 32 | +```dart |
| 33 | +void main() { |
| 34 | + testGoldenSceneOnIOS("my iOS golden test", (tester) async { |
| 35 | + // TODO: implement golden test. |
| 36 | + }); |
| 37 | +
|
| 38 | + testGoldenSceneOnAndroid("my Android golden test", (tester) async { |
| 39 | + // TODO: implement golden test. |
| 40 | + }); |
| 41 | +
|
| 42 | + testGoldenSceneOnMac("my Mac golden test", (tester) async { |
| 43 | + // TODO: implement golden test. |
| 44 | + }); |
| 45 | +
|
| 46 | + testGoldenSceneOnWindow("my Windows golden test", (tester) async { |
| 47 | + // TODO: implement golden test. |
| 48 | + }); |
| 49 | +
|
| 50 | + testGoldenSceneOnLinux("my Linux golden test", (tester) async { |
| 51 | + // TODO: implement golden test. |
| 52 | + }); |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +The implementation of the platform-specific test runners are nearly identical to `testGoldenScene()`, except |
| 57 | +they set the `debugDefaultTargetPlatformOverride` property to the given platform. |
| 58 | + |
| 59 | +```dart |
| 60 | +debugDefaultTargetPlatformOverride = TargetPlatform.macOS; |
| 61 | +``` |
| 62 | + |
| 63 | +When the test completes, `debugDefaultTargetPlatformOverride` is reset to `null`. |
| 64 | + |
| 65 | +You could implement this behavior yourself, within the standard `testWidgets()` runner, but |
| 66 | +these platform-specific test runners are provided as a convenient way to simulate a platform without |
| 67 | +repeating the platform configuration every time. |
| 68 | + |
0 commit comments