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"
}
}
This could be achieved using a shared package (namely test_base).
run:
flutter create --template=package test_base
test_base so you have access to your main package codes.test_base/pubspec.yaml:
dependencies:
<your-package-name>:
path: ../
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.
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';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With