Make it possible to run tests live on a device (#3936)

This makes it possible to substitute 'flutter run' for 'flutter test'
and actually watch a test run on a device.

For any test that depends on flutter_test:

1. Remove any import of 'package:test/test.dart'.

2. Replace `testWidgets('...', (WidgetTester tester) {`
      with `testWidgets('...', (WidgetTester tester) async {`

3. Add an "await" in front of calls to any of the following:
    * tap()
    * tapAt()
    * fling()
    * flingFrom()
    * scroll()
    * scrollAt()
    * pump()
    * pumpWidget()

4. Replace any calls to `tester.flushMicrotasks()` with calls to
   `await tester.idle()`.

There's a guarding API that you can use, if you have particularly
complicated tests, to get better error messages. Search for
TestAsyncUtils.
This commit is contained in:
Ian Hickson
2016-05-16 12:53:13 -07:00
parent 5fa6c0e9b0
commit 3252701753
134 changed files with 3254 additions and 1987 deletions

View File

@@ -0,0 +1,4 @@
The files in this directory are used as part of tests in the
`flutter_tools` package. They are here because here these tests need a
`pubspec.yaml` that references the flutter framework (which is
intentionally not true of the `flutter_tools` package).

View File

@@ -0,0 +1,21 @@
.*(this line contains the test framework's output with the clock and so forth)?
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following assertion was thrown running a test:
Guarded function conflict\. You must use "await" with all Future-returning test APIs\.
The guarded "guardedHelper" function was called from .*dev/manual_tests/test_data/test_async_utils_guarded_test\.dart on line [0-9]+\.
Then, the "expect" function was called from .*dev/manual_tests/test_data/test_async_utils_guarded_test\.dart on line [0-9]+\.
The first function \(guardedHelper\) had not yet finished executing at the time that the second function \(expect\) was called\. Since both are guarded, and the second was not a nested call inside the first, the first must complete its execution before the second can be called\. Typically, this is achieved by putting an "await" statement in front of the call to the first\.
If you are confident that all test APIs are being called using "await", and this expect\(\) call is not being invoked at the top level but is itself being called from some sort of callback registered before the guardedHelper method was called, then consider using expectSync\(\) instead\.
When the first function \(guardedHelper\) was called, this was the stack:
<<skip until matching line>>
\(elided .+\)
When the exception was thrown, this was the stack:
<<skip until matching line>>
\(elided .+\)
════════════════════════════════════════════════════════════════════════════════════════════════════
.*(this line has more of the test framework's output)?
Test failed\. See exception logs above\.
*
.*..:.. \+0 -1: Some tests failed\. *

View File

@@ -0,0 +1,23 @@
// Copyright 2016 The Chromium 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/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
Future<Null> guardedHelper(WidgetTester tester) {
return TestAsyncUtils.guard(() async {
await tester.pumpWidget(new Text('Hello'));
});
}
void main() {
testWidgets('TestAsyncUtils - custom guarded sections', (WidgetTester tester) async {
debugPrint = (String message, { int wrapWidth }) { print(message); };
await tester.pumpWidget(new Container());
expect(find.byElementType(Container), isNotNull);
guardedHelper(tester);
expect(find.byElementType(Container), isNull);
// this should fail
});
}

View File

@@ -0,0 +1,20 @@
.*..:.. \+0: - TestAsyncUtils - handling unguarded async helper functions *
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following assertion was thrown running a test:
Guarded function conflict\. You must use "await" with all Future-returning test APIs\.
The guarded method "pump" from class WidgetTester was called from .*dev/flutter/dev/manual_tests/test_data/test_async_utils_unguarded_test.dart on line [0-9]+\.
Then, it was called again from .*dev/manual_tests/test_data/test_async_utils_unguarded_test.dart on line [0-9]+\.
The first method had not yet finished executing at the time that the second method was called\. Since both are guarded, and the second was not a nested call inside the first, the first must complete its execution before the second can be called\. Typically, this is achieved by putting an "await" statement in front of the call to the first\.
When the first method was called, this was the stack:
<<skip until matching line>>
(elided [0-9]+ frames from .+)
When the exception was thrown, this was the stack:
<<skip until matching line>>
(elided [0-9]+ frames from .+)
════════════════════════════════════════════════════════════════════════════════════════════════════
.*..:.. \+0 -1: - TestAsyncUtils - handling unguarded async helper functions *
Test failed. See exception logs above.
*
.*..:.. \+0 -1: Some tests failed\. *

View File

@@ -0,0 +1,19 @@
// Copyright 2016 The Chromium 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/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
Future<Null> helperFunction(WidgetTester tester) async {
await tester.pump();
}
void main() {
testWidgets('TestAsyncUtils - handling unguarded async helper functions', (WidgetTester tester) async {
debugPrint = (String message, { int wrapWidth }) { print(message); };
helperFunction(tester);
helperFunction(tester);
// this should fail
});
}

View File

@@ -4,15 +4,14 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:test/test.dart';
import '../card_collection.dart' as card_collection;
void main() {
testWidgets("Card Collection smoke test", (WidgetTester tester) {
testWidgets("Card Collection smoke test", (WidgetTester tester) async {
card_collection.main(); // builds the app and schedules a frame but doesn't trigger one
tester.pump(); // see https://github.com/flutter/flutter/issues/1865
tester.pump(); // triggers a frame
await tester.pump(); // see https://github.com/flutter/flutter/issues/1865
await tester.pump(); // triggers a frame
Finder navigationMenu = find.byWidgetPredicate((Widget widget) {
if (widget is Tooltip)
@@ -22,18 +21,18 @@ void main() {
expect(navigationMenu, findsOneWidget);
tester.tap(navigationMenu);
tester.pump(); // start opening menu
tester.pump(const Duration(seconds: 1)); // wait til it's really opened
await tester.tap(navigationMenu);
await tester.pump(); // start opening menu
await tester.pump(const Duration(seconds: 1)); // wait til it's really opened
// smoke test for various checkboxes
tester.tap(find.text('Make card labels editable'));
tester.pump();
tester.tap(find.text('Let the sun shine'));
tester.pump();
tester.tap(find.text('Make card labels editable'));
tester.pump();
tester.tap(find.text('Vary font sizes'));
tester.pump();
await tester.tap(find.text('Make card labels editable'));
await tester.pump();
await tester.tap(find.text('Let the sun shine'));
await tester.pump();
await tester.tap(find.text('Make card labels editable'));
await tester.pump();
await tester.tap(find.text('Vary font sizes'));
await tester.pump();
});
}