|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:io'; |
| 3 | +import 'dart:convert'; |
| 4 | + |
| 5 | +import 'package:flutter_test/flutter_test.dart'; |
| 6 | + |
| 7 | +// Assumes that the flutter command is in `$PATH`. |
| 8 | +const String _flutterBin = 'flutter'; |
| 9 | +const String _e2eResultsPrefix = 'E2EWidgetsFlutterBinding test results:'; |
| 10 | + |
| 11 | +void main() async { |
| 12 | + group('E2E binding result', () { |
| 13 | + test('when multiple tests pass', () async { |
| 14 | + final Map<String, dynamic> results = |
| 15 | + await _runTest('test/data/pass_test_script.dart'); |
| 16 | + |
| 17 | + expect( |
| 18 | + results, |
| 19 | + equals({ |
| 20 | + 'passing test 1': 'success', |
| 21 | + 'passing test 2': 'success', |
| 22 | + })); |
| 23 | + }); |
| 24 | + |
| 25 | + test('when multiple tests fail', () async { |
| 26 | + final Map<String, dynamic> results = |
| 27 | + await _runTest('test/data/fail_test_script.dart'); |
| 28 | + |
| 29 | + expect( |
| 30 | + results, |
| 31 | + equals({ |
| 32 | + 'failing test 1': 'failed', |
| 33 | + 'failing test 2': 'failed', |
| 34 | + })); |
| 35 | + }); |
| 36 | + |
| 37 | + test('when one test passes, then another fails', () async { |
| 38 | + final Map<String, dynamic> results = |
| 39 | + await _runTest('test/data/pass_then_fail_test_script.dart'); |
| 40 | + |
| 41 | + expect( |
| 42 | + results, |
| 43 | + equals({ |
| 44 | + 'passing test': 'success', |
| 45 | + 'failing test': 'failed', |
| 46 | + })); |
| 47 | + }); |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +/// Runs a test script and returns the [E2EWidgetsFlutterBinding.result]. |
| 52 | +/// |
| 53 | +/// [scriptPath] is relative to the package root. |
| 54 | +Future<Map<String, dynamic>> _runTest(String scriptPath) async { |
| 55 | + final Process process = |
| 56 | + await Process.start(_flutterBin, ['test', '--machine', scriptPath]); |
| 57 | + |
| 58 | + // In the test [tearDownAll] block, the test results are encoded into JSON and |
| 59 | + // are printed with the [_e2eResultsPrefix] prefix. |
| 60 | + // |
| 61 | + // See the following for the test event spec which we parse the printed lines |
| 62 | + // out of: https://github.com/dart-lang/test/blob/master/pkgs/test/doc/json_reporter.md |
| 63 | + final String testResults = (await process.stdout |
| 64 | + .transform(utf8.decoder) |
| 65 | + .expand((String text) => text.split('\n')) |
| 66 | + .map((String line) { |
| 67 | + try { |
| 68 | + return jsonDecode(line); |
| 69 | + } on FormatException { |
| 70 | + // Only interested in test events which are JSON. |
| 71 | + } |
| 72 | + }) |
| 73 | + .where((dynamic testEvent) => testEvent != null && testEvent['type'] == 'print') |
| 74 | + .map((dynamic printEvent) => printEvent['message'] as String) |
| 75 | + .firstWhere( |
| 76 | + (String message) => message.startsWith(_e2eResultsPrefix))) |
| 77 | + .replaceAll(_e2eResultsPrefix, ''); |
| 78 | + |
| 79 | + return jsonDecode(testResults); |
| 80 | +} |
0 commit comments