3

How do i create a directory of shared code between Integration tests and Widget tests in flutter? There could be quite a bit of it since they use the same API now as of the new official

in Android native you can do the below in order to have code that is shared between local non-instrumented UI tests (Roboelectric under the hood) and instrumented Espresso tests with the Espresso API. And of course miscellaneous shared helpers, mocks, whatever.

How do i achieve a similar goal in Flutter?

 // This allows us to share classes between androidTest & test directories.
    android.sourceSets {
        test {
            java.srcDirs += "$projectDir/src/testShared"
        }
        androidTest {
            java.srcDirs += "$projectDir/src/testShared"
        }
    }
nAndroid
  • 862
  • 1
  • 10
  • 25

1 Answers1

0

This could be achieved using a shared package (namely test_base).

  1. Create a package for your test codes (confusion avoidance: your code will be written in its lib folder).

run:

flutter create --template=package test_base
  1. Add your main package as a dependency to test_base so you have access to your main package codes.

test_base/pubspec.yaml:

dependencies:
  <your-package-name>:
    path: ../
  1. Add test_base to your main library as a dev-dependency.

./pubspec.yaml:

dev_dependencies:
  test_base:
    path: test_base/

Now you can import the test_base package in your widget/integration tests.

Note: This way within test_base you can import packages of transitive dependencies (dependencies of your main package) without a compilation error. But it is discouraged in dart to explicitly import packages of dependencies you don't explicitly define in test_base/pubspec.yaml and it's probable to face linter or compilation warnings in future versions of dart (currently it's 2.13.4 and no warnings). Which means then you need to explicitly define those dependencies in test_base/pubspec.yaml. Though I think this is the case it would be harmless to import transitive dependencies packages.

navid
  • 1,022
  • 9
  • 20