2021年11月24日 星期三

[flutter, test] The First Step for Flutter App Integration Test

The First Step for Flutter App Integration Test

井民全, Jing, mqjing@gmail.com

  • [flutter, install] How to Install Flutter SDK and Create a Project (view)
  • [main, hybrid, cross] Ionic, React Native, Cordova, Electron (view)


Basically, I followed the flutter official doc[1], and easily write an integration test for a Flutter App project on my iOS simulator. If you are looking for Mobile App testing tool, you should check Appium, also.

1. Create an app to test

flutter create myapp     # create a demo app for the test

open -a Simulator         # launch an iPhone simulator

flutter run





2. Create Integration test

Step 1: Add the integration_test dependency to the dev_dependencies 

File: pubspec.yaml

...

dev_dependencies:

  flutter_test:

    sdk: flutter

  integration_test:

    sdk: flutter


...


Step 2: Create the test files

mkdir integration_test


cd integration_test

touch app_test.dart


Result

myapp/

  lib/

    main.dart

  integration_test/

    app_test.dart


Step 3: Write integration test code

File: app_test.dart

import 'package:flutter_test/flutter_test.dart';

import 'package:myapp/integration_test.dart';


import 'package:introduction/main.dart' as app;


void main() {

  IntegrationTestWidgetsFlutterBinding.ensureInitialized();


  group('end-to-end test', () {

    testWidgets('tap on the floating action button, verify counter',

        (WidgetTester tester) async {

      app.main();

      await tester.pumpAndSettle();


      // Verify the counter starts at 0.

      expect(find.text('0'), findsOneWidget);


      // Finds the floating action button to tap on.

      final Finder fab = find.byTooltip('Increment');


      // Emulate a tap on the floating action button.

      await tester.tap(fab);


      // Trigger a frame.

      await tester.pumpAndSettle();


      // Verify the counter increments by 1.

      expect(find.text('1'), findsOneWidget);

    });

  });

}



3. Run

flutter test integration_test/app_test.dart


Result

4. Reference

  1. https://docs.flutter.dev/cookbook/testing/integration/introduction