简体   繁体   中英

Flutter - How to run unit tests over a plugin native code for Android

We are building a plugin for flutter that iterable.com here la-haus/iterable-flutter , and we want to test the android implementation of our plugin, we are already able to run tests for the flutter side but not each native implementation tests.

Folders and tests

So we have:

  • lib/ : library implementation, the interace that clients use.
    • tests/ : test for the flutter side of the library
  • android/

What we have tried

Question

How can I run Android implementation android/src/test tests?

Our current issue is https://github.com/la-haus/iterable-flutter/issues/22

We were asking us the same question and were surprised to not find any examples of running native tests with flutter - as described in https://github.com/flutter/flutter/wiki/Plugin-Tests (thanks for providing the link btw).

Solution with gradle (without flutter_plugin_tools)

Assumption: the plugin structure got scaffolded by the flutter create --template=plugin --platforms=android ... command.

To run the native tests, make sure that the local gradle property files ./example/android/local.properties and ./android/local.properties are created properly. This can be achieved by running flutter pub get or flutter test in the root directory of the plugin.

Then you're ready to run gradle --project-dir ./example/android test , which executes the test target for all subprojects from the plugin example (which includes the effective plugin code)

Here's an example of a (slightly stripped down) github actions workflow for testing a plugin (name: plgn1 ):

name: CI/CD, test code

on: [push]

jobs:
  plgn1-plugin:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./app/3rdparty/plgn1

    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-java@v2
        with:
          java-version: "11"
          distribution: "adopt"
          cache: 'gradle'
      - uses: subosito/flutter-action@v2
        with:
          flutter-version: "3.0.1"
          channel: "stable"
      - name: Config flutter
        run: flutter config --no-analytics

      - name: "Run flutter pub get"
        run: flutter pub get

      - name: "Run Flutter test(s)"
        run: flutter test 

      - name: "Run Flutter test(s) for plugin example"
        run: flutter test 
        working-directory: ./app/3rdparty/plgn1/example

      - name: "Run Android test(s) for plugin example"
        run: gradle --project-dir ./example/android --no-daemon test

Skipping flutter_plugin_tools (opinion, own experience)

The way how the plugin tests are described for native-tests sounded quite easy and feasible, but after playing 1h around I lost my patience (and I eventually got the feeling that it's not designed for a flutter plugin like ours).

I skipped eventually flutter_plugin_tools and went on with gradle directly. That's probably not the way how it should be run in the flutter ecosystem, but it's working for us.

Please let me know when you find an easier or more elegant way to run native tests of a flutter plugin!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM