Skip to main content

Android unit tests

Unit tests are useful if you want to verify individual code blocks, catch bugs or prevent crashes as early as possible. On Bitrise, you can run Android unit tests easily with the Android Unit Test Step. The Step finds and runs all unit tests included in the specified module and variant of your Android project.

Running multiple tests in parallel

You can run multiple unit tests in parallel, for different devices or shards, by using the Pipelines feature: Currently supported use cases for the Android platform.

To run your unit tests:

Workflow Editor

bitrise.yml

  1. Make sure you included unit tests in your Android project.

  2. Add the Android Unit Test Step to your Workflow.

  3. Make sure the Project Location input points to the root directory of your Android project.

    The root directory is the directory where your build.gradle file exists. If you configured your Android project automatically when adding it as an app on Bitrise, you don't have to change the default value.

  4. In the Module and Variant inputs, set the module and the variant you want to test. Leave the inputs blank to test all modules and/or variants.

    You can check the available modules and variants of your project in the Project window in Android Studio.

    android-unit-test.png

  5. In the Options input group, you can pass additional Gradle arguments to the build task in the Additional Gradle Arguments input.

  6. If you have custom output directories configured for the test results of the tests in your project, configure the Step to look for the test results in the correct location when exporting them:

    The Local unit test HTML result directory pattern input sets the directory for HTML test results.

    The Local unit test XML result directory pattern input sets the directory for XML test results.

    local-result-pattern.png

    Both directories are zipped and exported to BITRISE_DEPLOY_DIR. This ensures that your test results can be viewed, for example, in the Tests tab.

    If you don't have custom output directories configured, you do not need to change these inputs: the default values will work.

  7. Add the Deploy to Bitrise.io - Build Artifacts, Test Reports, and Pipeline intermediate files Step to your Workflow to be able to view your test results: Deploying and viewing test results.

  8. Make sure you included unit tests in your Android project.

  9. Add the android-unit-test Step to your Workflow.

    my-workflow:
    steps:
    - git-clone: {}
    - android-unit-test:
    inputs:
  10. Make sure the project_location input points to the root directory of your Android project.

    The root directory is the directory where your build.gradle file exists. If you configured your Android project automatically when adding it as an app on Bitrise, you don't have to change the default value.

    my-workflow:
    steps:
    - git-clone: {}
    - android-unit-test:
    inputs:
    - project_location: $BITRISE_SOURCE_DIR
  11. In the module and variant inputs, set the module and the variant you want to test. If you don't set these inputs, the Step will test all modules and/or variants.

    You can check the available modules and variants of your project in the Project window in Android Studio.

    my-workflow:
    steps:
    - git-clone: {}
    - android-unit-test:
    inputs:
    - module: app
    - variant: debug
    - project_location: $BITRISE_SOURCE_DIR
  12. In the arguments input, you can pass additional Gradle arguments to the build task.

    my-workflow:
    steps:
    - git-clone: {}
    - android-unit-test:
    inputs:
    - module: app
    - variant: debug
    - arguments: --task
    - project_location: $BITRISE_SOURCE_DIR
  13. If you have custom output directories configured for the test results of the tests in your project, configure the Step to look for the test results in the correct location when exporting them:

    The report_path_pattern input sets the directory for HTML test results.

    The result_path_pattern input sets the directory for XML test results.

    my-workflow:
    steps:
    - git-clone: {}
    - android-unit-test:
    inputs:
    - module: app
    - variant: debug
    - arguments: --task
    - report_path_pattern: '*build/reports/tests'
    - result_path_pattern: '*build/test-results'
    - project_location: $BITRISE_SOURCE_DIR

    Both directories are zipped and exported to BITRISE_DEPLOY_DIR. This ensures that your test results can be viewed, for example, in the Tests tab.

    If you don't have custom output directories configured, you do not need to change these inputs: the default values will work.

  14. Add the deploy-to-bitrise-io Step to your Workflow to be able to view your test results: Deploying and viewing test results.

    my-workflow:
    steps:
    - git-clone: {}
    - android-unit-test:
    inputs:
    - module: app
    - variant: debug
    - arguments: --task
    - report_path_pattern: '*build/reports/tests'
    - result_path_pattern: '*build/test-results'
    - project_location: $BITRISE_SOURCE_DIR
    - deploy-to-bitrise-io: {}