Flutter Tricks
Published on

Running Analyzer and Tests in Github Actions for Flutter

Integrating analyzer and tests during your development process, is an essential step in ensuring the quality and reliability of your code. Analyzer checks your code for errors, warnings, and lints, while tests validate the functionality of your code. This article will guide you on how to set up and run analyzer and tests in Github Actions for your Flutter project.

Setting Up Github Actions

Github Actions allows you to automate, customize, and execute your software development workflows right in your Github repository. You can set up Github Actions by creating a workflow file in your repository. This file will define when and how your actions will run.

Below is a sample workflow file for setting up and running analyzer and tests in a Flutter project:

name: Analyze And Test

on:
  push:

jobs:
  analyze-and-test:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          channel: stable
          cache: true
          cache-key: flutter-:os:-:channel:-:version:-:arch:-:hash:-${{ hashFiles('**/pubspec.lock') }}

      - name: Install Dependencies
        run: |
          flutter config --no-analytics
          flutter --disable-telemetry
          flutter pub get

Once the above steps are complete, your environment is ready for running analyzer and tests.

Running Analyzer and Tests

To ensure that your code is formatted correctly and meets the Dart style guide, you can use the Dart formatter. This can be achieved by adding the 'Check Formatting' step to your workflow file, as shown below:

name: Check Formatting
  run: dart format --line-length 80 --set-exit-if-changed lib test

You can then run the analyzer to check your project for potential errors. Add the 'Analyze' step to your workflow file, as shown below:

name: Analyze
  run: flutter analyze

Finally, run your tests to ensure your code is working as expected. Add the 'Run Tests' step to your workflow file:

name: Run Tests
  run: flutter test --test-randomize-ordering-seed random

With these steps, whenever you push your code, Github Actions will automatically run the analyzer and tests, alerting you of any potential issues with your code.

Wrap-Up

Setting up and running analyzer and tests in Github Actions for your Flutter project might seem like a daunting task, but it is an essential step in ensuring the quality of your code. It not only helps in catching potential errors early but also ensures that your code is clean, maintainable, and conforming to the set coding standards. By integrating these actions into your development workflow, you can create robust and reliable Flutter applications.