[integration_test] Add a run method for proper reporting of test results (#70075)
This commit is contained in:
@@ -1,79 +0,0 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
final String bat = Platform.isWindows ? '.bat' : '';
|
||||
final String _flutterBin = path.join(Directory.current.parent.parent.parent.path, 'bin', 'flutter$bat');
|
||||
const String _integrationResultsPrefix =
|
||||
'IntegrationTestWidgetsFlutterBinding test results:';
|
||||
const String _failureExcerpt = r'Expected: <false>\n Actual: <true>';
|
||||
|
||||
Future<void> main() async {
|
||||
group('Integration binding result', () {
|
||||
test('when multiple tests pass', () async {
|
||||
final Map<String, dynamic> results = await _runTest(path.join('test', 'data', 'pass_test_script.dart'));
|
||||
|
||||
expect(
|
||||
results,
|
||||
equals(<String, dynamic>{
|
||||
'passing test 1': 'success',
|
||||
'passing test 2': 'success',
|
||||
}));
|
||||
});
|
||||
|
||||
test('when multiple tests fail', () async {
|
||||
final Map<String, dynamic> results = await _runTest(path.join('test', 'data', 'fail_test_script.dart'));
|
||||
|
||||
expect(results, hasLength(2));
|
||||
expect(results, containsPair('failing test 1', contains(_failureExcerpt)));
|
||||
expect(results, containsPair('failing test 2', contains(_failureExcerpt)));
|
||||
});
|
||||
|
||||
test('when one test passes, then another fails', () async {
|
||||
final Map<String, dynamic> results = await _runTest(path.join('test', 'data', 'pass_then_fail_test_script.dart'));
|
||||
|
||||
expect(results, hasLength(2));
|
||||
expect(results, containsPair('passing test', equals('success')));
|
||||
expect(results, containsPair('failing test', contains(_failureExcerpt)));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// Runs a test script and returns the [IntegrationTestWidgetsFlutterBinding.result].
|
||||
///
|
||||
/// [scriptPath] is relative to the package root.
|
||||
Future<Map<String, dynamic>> _runTest(String scriptPath) async {
|
||||
final Process process =
|
||||
await Process.start(_flutterBin, <String>['test', '--machine', scriptPath]);
|
||||
|
||||
/// In the test [tearDownAll] block, the test results are encoded into JSON and
|
||||
/// are printed with the [_integrationResultsPrefix] prefix.
|
||||
///
|
||||
/// See the following for the test event spec which we parse the printed lines
|
||||
/// out of: https://github.com/dart-lang/test/blob/master/pkgs/test/doc/json_reporter.md
|
||||
final String testResults = (await process.stdout
|
||||
.transform(utf8.decoder)
|
||||
.expand((String text) => text.split('\n'))
|
||||
.map((String line) {
|
||||
try {
|
||||
return jsonDecode(line) as Map<String, dynamic>;
|
||||
} on FormatException {
|
||||
// Only interested in test events which are JSON.
|
||||
}
|
||||
})
|
||||
.where((Map<String, dynamic> testEvent) =>
|
||||
testEvent != null && testEvent['type'] == 'print')
|
||||
.map((Map<String, dynamic> printEvent) => printEvent['message'] as String)
|
||||
.firstWhere((String message) =>
|
||||
message.startsWith(_integrationResultsPrefix)))
|
||||
.replaceAll(_integrationResultsPrefix, '');
|
||||
|
||||
return jsonDecode(testResults) as Map<String, dynamic>;
|
||||
}
|
||||
@@ -2,23 +2,24 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:integration_test/integration_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
import '../utils.dart';
|
||||
|
||||
void main() {
|
||||
final IntegrationTestWidgetsFlutterBinding binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized() as IntegrationTestWidgetsFlutterBinding;
|
||||
|
||||
testWidgets('failing test 1', (WidgetTester tester) async {
|
||||
expect(true, false);
|
||||
testWidgets('Failing test 1', (WidgetTester tester) async {
|
||||
expect(false, true);
|
||||
});
|
||||
|
||||
testWidgets('failing test 2', (WidgetTester tester) async {
|
||||
expect(true, false);
|
||||
testWidgets('Failing test 2', (WidgetTester tester) async {
|
||||
expect(false, true);
|
||||
});
|
||||
|
||||
tearDownAll(() {
|
||||
print('IntegrationTestWidgetsFlutterBinding test results: ${jsonEncode(binding.results)}');
|
||||
print(
|
||||
'IntegrationTestWidgetsFlutterBinding test results: ${testResultsToJson(binding.results)}');
|
||||
});
|
||||
}
|
||||
@@ -2,24 +2,24 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:integration_test/integration_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
import '../utils.dart';
|
||||
|
||||
void main() {
|
||||
final IntegrationTestWidgetsFlutterBinding binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized() as IntegrationTestWidgetsFlutterBinding;
|
||||
|
||||
testWidgets('passing test 1', (WidgetTester tester) async {
|
||||
testWidgets('Passing test 1', (WidgetTester tester) async {
|
||||
expect(true, true);
|
||||
});
|
||||
|
||||
testWidgets('passing test 2', (WidgetTester tester) async {
|
||||
testWidgets('Passing test 2', (WidgetTester tester) async {
|
||||
expect(true, true);
|
||||
});
|
||||
|
||||
tearDownAll(() {
|
||||
print(
|
||||
'IntegrationTestWidgetsFlutterBinding test results: ${jsonEncode(binding.results)}');
|
||||
'IntegrationTestWidgetsFlutterBinding test results: ${testResultsToJson(binding.results)}');
|
||||
});
|
||||
}
|
||||
@@ -2,24 +2,24 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:integration_test/integration_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
import '../utils.dart';
|
||||
|
||||
void main() {
|
||||
final IntegrationTestWidgetsFlutterBinding binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized() as IntegrationTestWidgetsFlutterBinding;
|
||||
|
||||
testWidgets('passing test', (WidgetTester tester) async {
|
||||
testWidgets('Passing test', (WidgetTester tester) async {
|
||||
expect(true, true);
|
||||
});
|
||||
|
||||
testWidgets('failing test', (WidgetTester tester) async {
|
||||
expect(true, false);
|
||||
testWidgets('Failing test', (WidgetTester tester) async {
|
||||
expect(false, true);
|
||||
});
|
||||
|
||||
tearDownAll(() {
|
||||
print(
|
||||
'IntegrationTestWidgetsFlutterBinding test results: ${jsonEncode(binding.results)}');
|
||||
'IntegrationTestWidgetsFlutterBinding test results: ${testResultsToJson(binding.results)}');
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:integration_test/src/constants.dart';
|
||||
|
||||
import 'utils.dart';
|
||||
|
||||
// Assumes that the flutter command is in `$PATH`.
|
||||
const String _flutterBin = 'flutter';
|
||||
const String _integrationResultsPrefix = 'IntegrationTestWidgetsFlutterBinding test results:';
|
||||
|
||||
Future<void> main() async {
|
||||
test('When multiple tests pass', () async {
|
||||
final Map<String, dynamic> results = await _runTest('test/reporter/data/pass_test_script.dart');
|
||||
|
||||
expect(results, hasLength(2));
|
||||
expect(results, containsPair('Passing test 1', _isSuccess));
|
||||
expect(results, containsPair('Passing test 2', _isSuccess));
|
||||
});
|
||||
|
||||
test('When multiple tests fail', () async {
|
||||
final Map<String, dynamic> results = await _runTest('test/reporter/data/fail_test_script.dart');
|
||||
|
||||
expect(results, hasLength(2));
|
||||
expect(results, containsPair('Failing test 1', _isSerializedFailure));
|
||||
expect(results, containsPair('Failing test 2', _isSerializedFailure));
|
||||
});
|
||||
|
||||
test('When one test passes, then another fails', () async {
|
||||
final Map<String, dynamic> results = await _runTest('test/reporter/data/pass_then_fail_test_script.dart');
|
||||
|
||||
expect(results, hasLength(2));
|
||||
expect(results, containsPair('Passing test', _isSuccess));
|
||||
expect(results, containsPair('Failing test', _isSerializedFailure));
|
||||
});
|
||||
}
|
||||
|
||||
/// Runs a test script and returns the [IntegrationTestWidgetsFlutterBinding.result].
|
||||
///
|
||||
/// [scriptPath] is relative to the package root.
|
||||
Future<Map<String, dynamic>> _runTest(String scriptPath) async {
|
||||
final Process process = await Process.start(_flutterBin, <String>['test', '--machine', scriptPath]);
|
||||
|
||||
/// In the test [tearDownAll] block, the test results are encoded into JSON and
|
||||
/// are printed with the [_integrationResultsPrefix] prefix.
|
||||
///
|
||||
/// See the following for the test event spec which we parse the printed lines
|
||||
/// out of: https://github.com/dart-lang/test/blob/master/pkgs/test/doc/json_reporter.md
|
||||
final String testResults = (await process.stdout
|
||||
.transform(utf8.decoder)
|
||||
.expand((String text) => text.split('\n'))
|
||||
.map<dynamic>((String line) {
|
||||
try {
|
||||
return jsonDecode(line);
|
||||
} on FormatException {
|
||||
// Only interested in test events which are JSON.
|
||||
}
|
||||
})
|
||||
.where((dynamic testEvent) =>
|
||||
testEvent != null && testEvent['type'] == 'print')
|
||||
.map((dynamic printEvent) => printEvent['message'] as String)
|
||||
.firstWhere((String message) =>
|
||||
message.startsWith(_integrationResultsPrefix)))
|
||||
.replaceAll(_integrationResultsPrefix, '');
|
||||
|
||||
return jsonDecode(testResults) as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
bool _isSuccess(Object object) => object == success;
|
||||
|
||||
bool _isSerializedFailure(dynamic object) => object.toString().contains(failureExcerpt);
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:integration_test/common.dart';
|
||||
|
||||
import 'utils.dart';
|
||||
|
||||
void main() {
|
||||
test('When multiple tests fail', () async {
|
||||
final List<TestResult> results = await runAndCollectResults(_testMain);
|
||||
|
||||
expect(results, <dynamic>[
|
||||
isFailure('Failing testWidgets()'),
|
||||
isFailure('Failing test()')
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
void _testMain() {
|
||||
testWidgets('Failing testWidgets()', (WidgetTester tester) async {
|
||||
expect(false, true);
|
||||
});
|
||||
|
||||
test('Failing test()', () {
|
||||
expect(false, true);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:integration_test/common.dart';
|
||||
|
||||
import 'utils.dart';
|
||||
|
||||
void main() {
|
||||
test('When multiple tests pass', () async {
|
||||
final List<TestResult> results = await runAndCollectResults(_testMain);
|
||||
|
||||
expect(results, <dynamic>[
|
||||
isSuccess('Passing testWidgets()'),
|
||||
isSuccess('Passing test()')
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
void _testMain() {
|
||||
testWidgets('Passing testWidgets()', (WidgetTester tester) async {
|
||||
expect(true, true);
|
||||
});
|
||||
|
||||
test('Passing test()', () {
|
||||
expect(true, true);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:integration_test/common.dart';
|
||||
|
||||
import 'utils.dart';
|
||||
|
||||
void main() {
|
||||
test('when one test passes, then another fails', () async {
|
||||
final List<TestResult> results = await runAndCollectResults(_testMain);
|
||||
|
||||
expect(results, <dynamic>[
|
||||
isSuccess('Passing test'),
|
||||
isFailure('Failing test')
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
void _testMain() {
|
||||
testWidgets('Passing test', (WidgetTester tester) async {
|
||||
expect(true, true);
|
||||
});
|
||||
|
||||
testWidgets('Failing test', (WidgetTester tester) async {
|
||||
expect(false, true);
|
||||
});
|
||||
}
|
||||
44
packages/integration_test/test/reporter/utils.dart
Normal file
44
packages/integration_test/test/reporter/utils.dart
Normal file
@@ -0,0 +1,44 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:integration_test/common.dart';
|
||||
import 'package:integration_test/integration_test.dart';
|
||||
import 'package:integration_test/src/constants.dart';
|
||||
|
||||
const String failureExcerpt = 'Expected: <true>';
|
||||
|
||||
dynamic isSuccess(String methodName) => isA<Success>()
|
||||
.having((Success s) => s.methodName, 'methodName', methodName);
|
||||
|
||||
dynamic isFailure(String methodName) => isA<Failure>()
|
||||
.having((Failure e) => e.methodName, 'methodName', methodName)
|
||||
.having((Failure e) => e.error.toString(), 'error', contains(failureExcerpt));
|
||||
|
||||
|
||||
Future<List<TestResult>> runAndCollectResults(
|
||||
FutureOr<void> Function() testMain,
|
||||
) async {
|
||||
final _TestReporter reporter = _TestReporter();
|
||||
await run(testMain, reporter: reporter);
|
||||
return reporter.results;
|
||||
}
|
||||
|
||||
class _TestReporter implements Reporter {
|
||||
final Completer<List<TestResult>> _resultsCompleter = Completer<List<TestResult>>();
|
||||
Future<List<TestResult>> get results => _resultsCompleter.future;
|
||||
|
||||
@override
|
||||
Future<void> report(List<TestResult> results) async => _resultsCompleter.complete(results);
|
||||
}
|
||||
|
||||
String testResultsToJson(Map<String, TestResult> results) {
|
||||
return jsonEncode(<String, Object>{
|
||||
for (TestResult result in results.values)
|
||||
result.methodName: result is Failure ? result : success
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user