Running Flutter tests for Android

Android tests are run on the Linux OS. Flutter (check the current version with the flutter doctor command) and Android SDK version 29.0.2 are preinstalled on a VM machine.

Create a shell script file called run-tests.sh. This file will contain commands to execute the test.

  1. Unzip test files:

    unzip tests.zip
    
  2. Accept SDK licenses:

    yes | flutter doctor --android-licenses
    
  3. Run Flutter doctor:

    flutter doctor
    
  4. (Optional.) Go to the Flutter project directory (sample command):

    cd my_app
    
  5. (Optional.) Clean the project folder (build/packages):

    flutter clean
    rm -r .packages
    rm pubspec.lock
    
  6. Run Flutter integration tests (sample command):

    flutter drive --target=test_driver/main.dart
    
  7. (Optional.) If test results (a file called TEST-all.xml) or screenshots should be displayed in the cloud UI:

    • Go back to the root directory (test result files are searched in the root directory):

      cd ...
      
    • Move “TEST-all.xml” to the root directory:

      mv my_app/TEST-all.xml TEST-all.xml
      
    • Create a “screenshots” directory and move screenshots there (sample command):

      mkdir -p screenshots
      mv /tmp/screenshots/test/ screenshots
      

Create a zip file containing the run-tests.sh file and your Flutter project directory. Note that the “run-tests.sh” file must be in the root of the zip file, otherwise, it is not found. Here is a sample command to create a zip file: zip -r android-test-files run-tests.sh my_app. Upload this file to the cloud and use it as the “Use to run the test” file.

An Appium Server Side-type test project expects an APK file to be uploaded. Upload a small sample app file and use it as “Install on the device”. Note that the app needs to be able to get installed to the device, otherwise, the test will fail. An Actual Flutter app to test is built during the test run.

Create a Flutter Android test run

To create a Flutter Android test run in BitBar Cloud:

  1. Create an Appium Android Server Side -type test run.

    Android test run
    Appium Android Server Side test run
  2. Upload the test zip file and the actual app APK file or a small sample app APK file (the app needs to be able to get installed to the device), the test does not use this app.

    Uploading the test zip and app files
  3. Select devices.

  4. Set a test timeout period. Make sure it is long enough.

  5. Run tests.

A simplified run-tests.sh example for Android integration tests

Here is an example of a complete run-tests.sh file for an Android integration test with minimal content.

unzip tests.zip
yes | flutter doctor --android-licenses
cd flutter_app
flutter clean
rm -r .packages
rm pubspec.lock
flutter drive --target=test_driver/main.dart

See Also

Publication date: