Skip to content

Commit 27322a5

Browse files
jiahaogdanielroek
authored andcommitted
[integration_test] Recommend tests to be in integration_test/, fix example (flutter#2986)
* [integration_test] Recommend tests to be in `integration_test/`, fix example In this way, there is a clear distinction between integration tests that run on a device (in `integration_test/`, and widget tests that run with the flutter tester in `test/`. Fix flutter/flutter#64690
1 parent 6b63ca2 commit 27322a5

14 files changed

+79
-61
lines changed

.cirrus.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ task:
5959
- ./chromedriver/chromedriver --port=4444 &
6060
test_script:
6161
- cd $INTEGRATION_TEST_PATH/example/
62-
- flutter drive -v --target=test_driver/example_integration.dart -d web-server --release --browser-name=chrome
62+
- flutter drive -v --driver=test_driver/integration_test_driver.dart --target=integration_test/example_test.dart -d web-server --release --browser-name=chrome
6363
- name: build-apks+java-test+firebase-test-lab
6464
env:
6565
matrix:

packages/integration_test/README.md

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,78 +7,95 @@ and native Android instrumentation testing.
77
## Usage
88

99
Add a dependency on the `integration_test` and `flutter_test` package in the
10-
`dev_dependencies` section of pubspec.yaml. For plugins, do this in the
11-
pubspec.yaml of the example app.
10+
`dev_dependencies` section of `pubspec.yaml`. For plugins, do this in the
11+
`pubspec.yaml` of the example app.
1212

13-
Invoke `IntegrationTestWidgetsFlutterBinding.ensureInitialized()` at the start
14-
of a test file, e.g.
13+
Create a `integration_test/` directory for your package. In this directory,
14+
create a `<name>_test.dart`, using the following as a starting point to make
15+
assertions.
1516

1617
```dart
1718
import 'package:flutter_test/flutter_test.dart';
1819
import 'package:integration_test/integration_test.dart';
1920
2021
void main() {
2122
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
23+
2224
testWidgets("failing test example", (WidgetTester tester) async {
2325
expect(2 + 2, equals(5));
2426
});
2527
}
2628
```
2729

28-
## Test locations
30+
### Driver Entrypoint
2931

30-
It is recommended to put integration_test tests in the `test/` folder of the app
31-
or package. For example apps, if the integration_test test references example
32-
app code, it should go in `example/test/`. It is also acceptable to put
33-
integration_test tests in `test_driver/` folder so that they're alongside the
34-
runner app (see below).
32+
An accompanying driver script will be needed that can be shared across all
33+
integration tests. Create a `integration_test_driver.dart` in the `test_driver/`
34+
directory with the following contents:
3535

36-
## Using Flutter Driver to Run Tests
36+
```dart
37+
import 'package:integration_test/integration_test_driver.dart';
3738
38-
`IntegrationTestWidgetsTestBinding` supports launching the on-device tests with
39-
`flutter drive`. Note that the tests don't use the `FlutterDriver` API, they
40-
use `testWidgets` instead.
39+
Future<void> main() => integrationDriver();
40+
```
4141

42-
Put the a file named `<package_name>_integration_test.dart` in the app'
43-
`test_driver` directory:
42+
You can also use different driver scripts to customize the behavior of the app
43+
under test. For example, `FlutterDriver` can also be parameterized with
44+
different [options](https://api.flutter.dev/flutter/flutter_driver/FlutterDriver/connect.html).
45+
See the [extended driver](https://github.com/flutter/plugins/tree/master/packages/integration_test/example/test_driver/integration_test_extended_driver.dart) for an example.
4446

45-
```dart
46-
import 'dart:async';
47+
### Package Structure
4748

48-
import 'package:integration_test/integration_test_driver.dart';
49+
Your package should have a structure that looks like this:
4950

50-
Future<void> main() async => integrationDriver();
51+
```
52+
lib/
53+
...
54+
integration_test/
55+
foo_test.dart
56+
bar_test.dart
57+
test/
58+
# Other unit tests go here.
59+
test_driver/
60+
integration_test_driver.dart
5161
```
5262

53-
To run a example app test with Flutter driver:
63+
[Example](https://github.com/flutter/plugins/tree/master/packages/integration_test/example)
5464

55-
```sh
56-
cd example
57-
flutter drive test/<package_name>_integration.dart
58-
```
65+
## Using Flutter Driver to Run Tests
66+
67+
These tests can be launched with the `flutter drive` command.
5968

60-
To test plugin APIs using Flutter driver:
69+
To run the `integration_test/foo_test.dart` test with the
70+
`test_driver/integration_test_driver.dart` driver, use the following command:
6171

6272
```sh
63-
cd example
64-
flutter drive --driver=test_driver/<package_name>_test.dart test/<package_name>_integration_test.dart
73+
flutter drive \
74+
--driver=test_driver/integration_test_driver.dart \
75+
--target=integration_test/foo_test.dart
6576
```
6677

67-
You can run tests on web in release or profile mode.
78+
### Web
79+
80+
Make sure you have [enabled web support](https://flutter.dev/docs/get-started/web#set-up)
81+
then [download and run](https://flutter.dev/docs/cookbook/testing/integration/introduction#6b-web)
82+
the web driver in another process.
6883

69-
First you need to make sure you have downloaded the driver for the browser.
84+
Use following command to execute the tests:
7085

7186
```sh
72-
cd example
73-
flutter drive -v --target=test_driver/<package_name>dart -d web-server --release --browser-name=chrome
87+
flutter drive \
88+
--driver=test_driver/integration_test_driver.dart \
89+
--target=integration_test/foo_test.dart \
90+
-d web-server
7491
```
7592

76-
## Android device testing
93+
## Android Device Testing
7794

7895
Create an instrumentation test file in your application's
7996
**android/app/src/androidTest/java/com/example/myapp/** directory (replacing
8097
com, example, and myapp with values from your app's package name). You can name
81-
this test file MainActivityTest.java or another name of your choice.
98+
this test file `MainActivityTest.java` or another name of your choice.
8299

83100
```java
84101
package com.example.myapp;
@@ -96,7 +113,7 @@ public class MainActivityTest {
96113
```
97114

98115
Update your application's **myapp/android/app/build.gradle** to make sure it
99-
uses androidx's version of AndroidJUnitRunner and has androidx libraries as a
116+
uses androidx's version of `AndroidJUnitRunner` and has androidx libraries as a
100117
dependency.
101118

102119
```gradle
@@ -160,7 +177,7 @@ You can pass additional parameters on the command line, such as the
160177
devices you want to test on. See
161178
[gcloud firebase test android run](https://cloud.google.com/sdk/gcloud/reference/firebase/test/android/run).
162179

163-
## iOS device testing
180+
## iOS Device Testing
164181

165182
You need to change `iOS/Podfile` to avoid test target statically linking to the plugins. One way is to
166183
link all of the plugins dynamically:
@@ -189,4 +206,4 @@ change the code. You can change `RunnerTests.m` to the name of your choice.
189206
INTEGRATION_TEST_IOS_RUNNER(RunnerTests)
190207
```
191208
192-
Now you can start RunnerTests to kick out integration tests!
209+
Now you can start RunnerTests to kick-off integration tests!

packages/integration_test/example/README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
Demonstrates how to use the `package:integration_test`.
44

5-
## Getting Started
5+
To run `integration_test/example_test.dart`,
66

7-
This project is a starting point for a Flutter application.
7+
Android / iOS:
88

9-
A few resources to get you started if this is your first Flutter project:
9+
```sh
10+
flutter drive \
11+
--driver=test_driver/integration_test_driver.dart \
12+
--target=integration_test/example_test.dart
13+
```
1014

11-
- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab)
12-
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook)
15+
Web:
1316

14-
For help getting started with Flutter, view our
15-
[online documentation](https://flutter.dev/docs), which offers tutorials,
16-
samples, guidance on mobile development, and a full API reference.
17+
```sh
18+
flutter drive \
19+
--driver=test_driver/integration_test_driver.dart \
20+
--target=integration_test/example_test.dart \
21+
-d web-server
22+
```

packages/integration_test/example/test_driver/example_integration.dart renamed to packages/integration_test/example/integration_test/example_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import 'package:integration_test/integration_test.dart';
99

10-
import 'example_integration_io.dart'
11-
if (dart.library.html) 'example_integration_web.dart' as tests;
10+
import '_example_test_io.dart' if (dart.library.html) '_example_test_web.dart'
11+
as tests;
1212

1313
void main() {
1414
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

packages/integration_test/example/test_driver/example_integration_extended.dart renamed to packages/integration_test/example/integration_test/extended_test.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// This is a Flutter widget test can take a screenshot.
22
//
3-
// NOTE: Screenshots are only supported on Web for now.
3+
// NOTE: Screenshots are only supported on Web for now. For Web, this needs to
4+
// be executed with the `test_driver/integration_test_extended_driver.dart`.
45
//
56
// To perform an interaction with a widget in your test, use the WidgetTester
67
// utility that Flutter provides. For example, you can send tap and scroll
@@ -9,8 +10,8 @@
910

1011
import 'package:integration_test/integration_test.dart';
1112

12-
import 'example_integration_io_extended.dart'
13-
if (dart.library.html) 'example_integration_web_extended.dart' as tests;
13+
import '_extended_test_io.dart' if (dart.library.html) '_extended_test_web.dart'
14+
as tests;
1415

1516
void main() {
1617
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

packages/integration_test/example/test_driver/example_integration_test.dart

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import 'package:integration_test/integration_test_driver.dart';
2+
3+
Future<void> main() => integrationDriver();

packages/integration_test/example/test_driver/example_integration_extended_test.dart renamed to packages/integration_test/example/test_driver/integration_test_extended_driver.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:async';
2-
31
import 'package:flutter_driver/flutter_driver.dart';
42
import 'package:integration_test/integration_test_driver_extended.dart';
53

packages/integration_test/example/test_driver/failure_test.dart renamed to packages/integration_test/example/test_driver/integration_test_failure_driver.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import 'dart:async';
2-
3-
import 'package:integration_test/common.dart' as common;
41
import 'package:flutter_driver/flutter_driver.dart';
2+
import 'package:integration_test/common.dart' as common;
53
import 'package:test/test.dart';
64

75
Future<void> main() async {

0 commit comments

Comments
 (0)