Experiment to run tests inside a running app, for faster feedback loop.
Usually, flutter tests are executed by "flutter test" command or IDE equivalent. Unfortunately, this method is pretty slow (around 5s of startup before anything executes) and does not support hot reload. Additionaly, you have to manually rerun those tests on each change, which takes your focus from actual code.
Instead of using flutter test to run tests, hottie allows you to embed them (with some hacks) directly into the running app. This way, you get hot reload support and instant feedback. Combined with automatic hot reload from your IDE, this gives you a very smooth TDD experience. Hottie does not affect production apps - you access it with separate main file which ensures that no test code will be actually embedded in your release builds.
- Run example/test/main_hottie.dart (VSCode config is already added, AS users are on their own).
- Notice green indicator in the bottom left corner.
- Break some tests or tested methods. Save and hot reload.
- See error page appearing with failed test.
- Fix tests.
- See that error page disappeared.
- Add hottie to your dev_dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
hottie:
git: https://github.com/szotp/hottie
- Create this main_hottie.dart file in your test directory, and configure your IDE to use it.
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hottie/hottie.dart';
import 'package:example/main.dart' as m;
Future<void> main() async {
runApp(
TestRunner(main: testAll, child: m.MyApp()),
);
}
@pragma('vm:entry-point')
void hottie() => hottieInner();
void testAll() {}
- Import all your test files and run them in testAll method:
import 'package:flutter/widgets.dart';
import 'package:hottie/hottie.dart';
import 'package:example/main.dart' as m;
import 'standard_test.dart' as t1;
import 'widgets_test.dart' as t2;
void main() {
runApp(
TestRunner(main: testAll, child: m.MyApp()),
);
}
void testAll() {
t1.main();
t2.main();
}
- Run your app and enjoy rapid testing.
Provider fork with hottie: https://github.com/szotp/provider/tree/hottie On my machine, all 274 tests execute in around 1 second.
- macOS
- iOS simulator
- Android (without file access)
- Tests from packages can't be accessed from app project, unless they have been moved into lib directory (which is not great because code completion for flutter_test items does not work).
- Hottie doesn't fully support running on device because test resources will not be bundled with the app.
- Instead of embedding hottie into the app, it may be possible to create console flutter app that would import the tests and run them.
- Better formatting for errors.
- Add ability to disable tests from UI.
- Benchmark tests and disable the heavy ones automatically.
- Customizable delay from hot reload to test execution.
File access on Android is currently not possible. Ideas how to implement this are welcome.
To setup file access for your test, in iOS and macOS, call HottiePlugin.instance.setRoot()
where plugin registration happens (check the example project for more details). Additionally, for macOS, you will also need to disable sandboxing, or add an exception for the test directory.
To omit tests that use filesystem, add "File" tag to them, or simply do not call them in your testAllMethod.