Skip to main content

Running unit and UI tests on Flutter apps

The Flutter Test Step allows you to run unit tests, widget tests, and integration (also referred to as end-to-end or UI testing) tests on your Flutter app on Bitrise. The Step runs the flutter test command and saves the json file that is generated by the command. You can then view the test results in the Test Reports add-on at any time.

Code coverage

The Flutter Test Step can also generate code coverage reports.

To run tests using Flutter Test:

Workflow Editor

bitrise.yml

  1. Include tests in your Flutter project.

    For more information on how to write tests and include them in your Flutter project, check the official Flutter documentation: Testing Flutter apps.

  2. Add the Flutter Install Step to your Workflow.

    This Step runs the initial setup of the Flutter SDK and installs any missing components.

  3. Make sure that the Flutter Install Step installs the correct version of the Flutter SDK for your app: check the Flutter SDK Git repository version input.

    flutter-install-step.png

    The input's default value is stable, which installs the latest stable version of the SDK. You can, however, set the value to either a specific version tag or a branch label:

    Installation bundle URL

    You can also install the Flutter SDK from an installation bundle URL: you just need to specify the URL in the Flutter SDK installation bundle URL input. Please note that if you do so, the Flutter SDK Git repository version input will be ignored entirely!

  4. Add the Flutter Test Step to your Workflow. It should be after the Flutter Install Step.

    The Step runs the flutter test command with optional flags. It can also check code coverage.

  5. Make sure the Project Location input points to the root directory of your Flutter project.

    flutter-test-step.png
    • If the project scanner automatically detected it as a Flutter project when adding it as an app on Bitrise, you don't have to change the default value.

    • If you configured the app manually, check that the location is correct. You can do so by going to the Env Vars tab on the Workflow Editor, and checking the BITRISE_FLUTTER_PROJECT_LOCATION Environment Variable.

  6. By default, the Step will find and run all tests. But you can configure it to run only selected tests: use the Test files pattern input to tell the Step which tests to run.

    The input accepts both * and ** glob formats. For example, lib/**/*_test.dart is a valid input.

    flutter-test-files-pattern.png
  7. Optionally, use the Additional parameters input to append flags to the flutter test command.

    To see the available options, run the flutter help test command. You can do so on your own device or in a Script Step on Bitrise.

  8. Optionally, set the Generate code coverage files? input to yes to generate code coverage files.

    With this input turned on, the --coverage flag is appended to the flutter test command.

  9. Optionally, you can access the generated json test report and the code coverage file (lcov.info) in subsequent Steps by using the BITRISE_FLUTTER_TESTRESULT_PATH and the BITRISE_FLUTTER_COVERAGE_PATH Environment Variables.

  10. To export the test results to Test Reports, add the Deploy to Bitrise.io Step to the Workflow.

  1. Include tests in your Flutter project.

    For more information on how to write tests and include them in your Flutter project, check the official Flutter documentation: Testing Flutter apps.

  2. Add the flutter-installer Step to your Workflow.

    This Step runs the initial setup of the Flutter SDK and installs any missing components.

    primary:
      description: |
        Builds project and runs tests.
      steps:
        - activate-ssh-key: {}
        - git-clone: {}
        - flutter-installer:
            inputs:
  3. Make sure that the Step installs the correct version of the Flutter SDK for your app: you can specify the version using the version input.

    The input's default value is stable, which installs the latest stable version of the SDK. If you do not see the version input in your bitrise.yml file, it is set to the default value. You can, however, set the value to either a specific version tag or a branch label:

    # Installing version 3.7.7 of the Flutter SDK
    - flutter-installer:
        inputs:
        - version: 3.7.7

    Installation bundle URL

    You can also install the Flutter SDK from an installation bundle URL: you just need to specify the URL in the installation-bundle-url input. Please note that if you do so, the version input will be ignored entirely!

    The URL is expected to begin with https://storage.googleapis.com/flutter_infra. For example:

    - flutter-installer:
        inputs:
        - installation_bundle_url: https://storage.googleapis.com/flutter_infra/releases/beta/macos/flutter_macos_v1.6.3-beta.zip
  4. Add the flutter-test Step to your Workflow. It should be after the flutter-installer Step.

    The Step runs the flutter test command with optional flags. It can also check code coverage.

    primary:
      description: |
        Builds project and runs tests.
      steps:
        - activate-ssh-key: {}
        - git-clone: {}
        - flutter-installer:
            inputs:
            - version: stable
            - is_update: false
        - restore-dart-cache: {}
        - flutter-test:
            inputs:
  5. Make sure the project_location input points to the root directory of your Flutter project.

    - flutter-test:
        inputs:
        - project_location: "$BITRISE_FLUTTER_PROJECT_LOCATION"
    • If the project scanner automatically detected it as a Flutter project when adding it as an app on Bitrise, you don't have to change the default value, which is BITRISE_FLUTTER_PROJECT_LOCATION.

    • If you configured the app manually, check that the location is correct. You can check where BITRISE_FLUTTER_PROJECT_LOCATION points in the envs property of the app:

      # Pointing to the root directory
      app:
        envs:
          - opts:
              is_expand: false
            BITRISE_FLUTTER_PROJECT_LOCATION: .
  6. By default, the Step will find and run all tests. But you can configure it to run only selected tests: use the tests_path_pattern input to tell the Step which tests to run.

    The input accepts both * and ** glob formats. For example, lib/**/*_test.dart is a valid input.

    - flutter-test:
        inputs:
        - tests_path_pattern: /lib/*_test.dart
  7. Optionally, use the additional_params input to append flags to the flutter test command.

    To see the available options, run the flutter help test command. You can do so on your own device or in a Script Step on Bitrise.

    - flutter-test:
        inputs:
    # setting a test timeout of 60 seconds
        - additional_params: --timeout 60s
  8. Optionally, set the generate_code_coverage_files input to 'yes' to generate code coverage files.

    With this input turned on, the --coverage flag is appended to the flutter test command.

    - flutter-test:
        inputs:
        - generate_code_coverage_files: 'yes'
  9. Optionally, you can access the generated json test report and the code coverage file (lcov.info) in subsequent Steps by using the BITRISE_FLUTTER_TESTRESULT_PATH and the BITRISE_FLUTTER_COVERAGE_PATH Environment Variables.

  10. To export the test results to Test Reports, add the deploy-to-bitrise-io Step to the Workflow.

    primary:
      description: |
        Builds project and runs tests.
      steps:
        - activate-ssh-key: {}
        - git-clone: {}
        - flutter-installer:
            inputs:
            - version: stable
            - is_update: false
        - restore-dart-cache: {}
        - flutter-test:
            inputs:
            - project_location: "$BITRISE_FLUTTER_PROJECT_LOCATION"
            - generate_code_coverage_files: 'yes'
            - tests_path_pattern: /lib/*_test.dart
       - save-dart-cache: {}    
       - deploy-to-bitrise-io: {}