Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a shared directory to share code in flutter between integration tests and widget tests?

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"
        }
    }
like image 531
nAndroid Avatar asked Oct 23 '25 00:10

nAndroid


2 Answers

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.

like image 162
navid Avatar answered Oct 25 '25 13:10

navid


You should be able to do that without any specific code. It is working for iphone/android but not for web integration test at that time (a bug is still open on flutter side https://github.com/flutter/flutter/issues/102469)

The simpliest workaround is to add a symlink to your test folder in your integration_test folder

cd integration_test
ln -s ../test/ test

then in your integration test import your test code with a relative import

import './test/utils/test_utils.dart';
like image 34
ArnaudR Avatar answered Oct 25 '25 15:10

ArnaudR