Integrating Firebase Test Lab with Flutter CI/CD Pipeline

Continuous Integration and Continuous Deployment (CI/CD) are essential practices for modern software development, providing automated workflows to build, test, and deploy applications. Integrating Firebase Test Lab into your Flutter CI/CD pipeline enhances your development process by enabling automated testing on real devices. This blog will guide you through the steps to integrate Firebase Test Lab with your Flutter CI/CD pipeline.

Flutter CI CD Pipeline


Why Use Firebase Test Lab?

Firebase Test Lab provides a cloud-based infrastructure for testing Android and iOS apps on a wide range of real and virtual devices. It allows developers to:

  • Run tests on multiple device configurations.
  • Identify and fix issues early in the development cycle.
  • Ensure a higher quality app experience across different devices.

Prerequisites

Before integrating Firebase Test Lab with your Flutter CI/CD pipeline, ensure you have:

  • A Flutter project.
  • A Firebase project set up.
  • Firebase CLI installed.
  • GitHub repository for your Flutter project.

Step 1: Set Up Firebase Test Lab

  1. Create a Firebase Project:

    • Go to the Firebase Console.
    • Click on "Add Project" and follow the instructions to create a new project.
  2. Enable Firebase Test Lab:

    • Navigate to the Firebase Test Lab section in the Firebase Console.
    • Enable the Test Lab API.
  3. Install Firebase CLI:

    npm install -g firebase-tools
  4. Login to Firebase:

    firebase login
  5. Initialize Firebase in Your Project:

    firebase init

Step 2: Write Flutter Integration Tests

Create integration tests for your Flutter app. Place these tests in the test_driver directory of your Flutter project.

Example:

// test_driver/app.dart import 'package:flutter_driver/driver_extension.dart'; import 'package:your_app/main.dart' as app; void main() { enableFlutterDriverExtension(); app.main(); } // test_driver/app_test.dart import 'package:flutter_driver/flutter_driver.dart'; import 'package:test/test.dart'; void main() { group('App Test', () { final buttonFinder = find.byValueKey('increment'); FlutterDriver driver; setUpAll(() async { driver = await FlutterDriver.connect(); }); tearDownAll(() async { if (driver != null) { driver.close(); } }); test('starts at 0 and increments', () async { expect(await driver.getText(find.text('0')), '0'); await driver.tap(buttonFinder); expect(await driver.getText(find.text('1')), '1'); }); }); }

Step 3: Set Up CI/CD Pipeline

For this example, we'll use GitHub Actions for CI/CD.

  1. Create GitHub Actions Workflow:
    • In your GitHub repository, create a directory .github/workflows.
    • Create a file firebase-test-lab.yml with the following content:
name: Firebase Test Lab Integration on: push: branches: - main jobs: test: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Set up Flutter uses: subosito/flutter-action@v2 with: flutter-version: '2.5.0' - name: Install dependencies run: flutter pub get - name: Build APK run: flutter build apk --release - name: Upload APK to Firebase Test Lab env: FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }} run: | gcloud auth activate-service-account --key-file ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }} gcloud --quiet config set project your-firebase-project-id gcloud firebase test android run \ --type instrumentation \ --app build/app/outputs/flutter-apk/app-release.apk \ --test build/app/outputs/apk/androidTest/release/app-release-androidTest.apk

Step 4: Configure Secrets

  1. Create a Service Account:

    • In the Firebase Console, go to "Project Settings" > "Service accounts".
    • Click "Generate new private key" and download the JSON file.
  2. Add Secrets to GitHub:

    • Go to your GitHub repository settings.
    • Navigate to "Secrets" and add two new secrets:
      • GOOGLE_APPLICATION_CREDENTIALS: Content of the JSON file.
      • FIREBASE_TOKEN: Firebase CLI token obtained by running firebase login:ci.

Conclusion

By integrating Firebase Test Lab with your Flutter CI/CD pipeline, you ensure that your app is tested on various real devices automatically, leading to higher quality releases and quicker feedback cycles. This integration helps in catching bugs early and provides confidence that your app performs well across different devices and configurations.

Feel free to ask if you have any questions or need further clarification on any steps.

Post a Comment

0 Comments