How to configure debug Flutter integration test on Android Studio

Issue

I can run this command in the terminal to execute integration test on real device

flutter drive --target=test_driver/app.dart

But I don’t know how to create Run/Debug configuration on Android Studio so I can debug the test.
Please help me.

Solution

It doesn’t require any special Run/Debug configuration on Android Studio to set breakpoints in an integration test using the integration_test package. This works the same for real devices, emulators, and Flutter Desktop.

Steps to reproduce

  1. Make sure that your real device is connected:
$ flutter devices
3 connected devices:

XXXXXX (mobile) • XXXXXX     • android-arm64  • Android XX.XX (API XX)
Linux (desktop) • linux      • linux-x64      • Ubuntu 20.04.XX LTS XXX-generic
Chrome (web)    • chrome     • web-javascript • Google Chrome XXXXX

(See
Run Test Flutter Apps Directly on Real Android Device in Windows MAC Tutorial | flutter-examples.com
)

  1. Create a new Flutter project with the classic counter app:
flutter create debug_at_device_2
cd debug_at_device_2
flutter pub get
  1. Open the project in Android Studio.

  2. Set a breakpoint in the _incrementCounter method:

breakpoint

  1. Add the flutter_test and integration_test packages as dev dependency in the pubspec.yaml:
dev_dependencies:
  flutter_test:
    sdk: flutter
  integration_test:
    sdk: flutter
  1. Update the packages again:
flutter pub get
  1. Create a new directory integration_test and within that a file example_test.dart with the following contents:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:debug_at_device/main.dart' as app;

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  testWidgets("should increment counter", (WidgetTester tester) async {
    app.main();
    await tester.pumpAndSettle();

    await tester.tap(find.byIcon(Icons.add));
    await tester.pumpAndSettle();
    expect(find.text('1'), findsOneWidget);

    await tester.tap(find.byIcon(Icons.add));
    await tester.pumpAndSettle();
    expect(find.text('2'), findsOneWidget);

    await tester.tap(find.byIcon(Icons.add));
    await tester.pumpAndSettle();
    expect(find.text('3'), findsOneWidget);
  });
}
  1. Make sure that your real device is selected as the default device in the runner bar:

default runner

  1. Make sure that the phone is unlocked (i.e. no screensaver)

  2. Right-click on the run icon next to the main method in example_test.dart and then select the debug option from the context menu:

start test debug

  1. Step through the code once the breakpoint gets hit:

debugger

Environment

I have tested it using:

  • Flutter version 2.5.1
  • Android Studio version 2020.3.1
  • Flutter plugin version 60.1.2
  • Ubuntu 20.04
  • Android phone

Answered By – Janux

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.