[go: nahoru, domu]

Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[instrumentation_adapter] Add support for running tests with Flutter driver #2050

Merged
merged 3 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Support for running tests with Flutter driver
  • Loading branch information
collinjackson committed Sep 9, 2019
commit 34d06fd6db8995e4837d49038ec9dbd9f324b97b
4 changes: 4 additions & 0 deletions packages/instrumentation_adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.2

* Added support for running tests using Flutter driver.

## 0.1.1

* Updates about using *androidx* library.
Expand Down
12 changes: 12 additions & 0 deletions packages/instrumentation_adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,15 @@ gcloud firebase test android run --type instrumentation \
--results-bucket=<RESULTS_BUCKET> \
--results-dir=<RESULTS_DIRECTORY>
```

## Flutter driver support

`InstrumentationAdapterFlutterBinding` also reports test results to `FlutterDriver`
when run on the command line via `flutter drive`.

```dart
final FlutterDriver driver = await FlutterDriver.connect();
final String result = await driver.requestData(null, timeout: const Duration(minutes: 1));
driver.close();
exit(result == 'pass' ? 0 : 1);
```
36 changes: 36 additions & 0 deletions packages/instrumentation_adapter/lib/instrumentation_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
Expand All @@ -16,9 +17,13 @@ class InstrumentationAdapterFlutterBinding
tearDownAll(() async {
await _channel.invokeMethod<void>(
'allTestsFinished', <String, dynamic>{'results': _results});
if (!_allTestsPassed.isCompleted)
_allTestsPassed.complete(true);
});
}

final Completer<bool> _allTestsPassed = Completer<bool>();

static WidgetsBinding ensureInitialized() {
if (WidgetsBinding.instance == null) {
InstrumentationAdapterFlutterBinding();
Expand All @@ -32,14 +37,45 @@ class InstrumentationAdapterFlutterBinding

static Map<String, String> _results = <String, String>{};

// Emulates the Flutter driver extension, returning 'pass' or 'fail'.
@override
void initServiceExtensions() {
super.initServiceExtensions();
Future<Map<String, dynamic>> callback(Map<String, String> params) async {
final String command = params['command'];
Map<String, String> response;
switch (command) {
case 'request_data':
final bool allTestsPassed = await _allTestsPassed.future;
response = <String, String>{
'message': allTestsPassed ? 'pass' : 'fail',
};
break;
case 'get_health':
response = <String, String>{ 'status': 'ok' };
break;
default:
throw UnimplementedError('$command is not implemented');
}
return <String, dynamic> {
'isError' : false,
'response': response,
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would this function return "isError": true?

Copy link
Contributor Author
@collinjackson collinjackson Sep 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to attach 'isError': false to the return value or the response will be rejected by FlutterDriver. I thought about returning 'isError': true in the event of test failure, but decided not to for now to match the current behavior running tests under Flutter driver.

}
registerServiceExtension(name: 'driver', callback: callback);
}

@override
Future<void> runTest(Future<void> testBody(), VoidCallback invariantTester,
{String description = '', Duration timeout}) async {
// TODO(jackson): Report the results individually instead of all at once
// See https://github.com/flutter/flutter/issues/38985
TestExceptionReporter valueBeforeTest = reportTestException;
reportTestException =
(FlutterErrorDetails details, String testDescription) {
_results[description] = 'failed';
_allTestsPassed.complete(false);
valueBeforeTest(details, testDescription);
};
await super.runTest(testBody, invariantTester,
description: description, timeout: timeout);
Expand Down