From 2908997b0ead4fb068df0c4a7b9fe11012094d8e Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Wed, 17 Jan 2018 09:06:43 -0800 Subject: [PATCH] Dump exceptions live when they happen when testing tests with flutter run (#14102) * Dump exceptions live when they happen when testing tests with flutter run. * Update binding.dart --- packages/flutter_test/lib/src/binding.dart | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/packages/flutter_test/lib/src/binding.dart b/packages/flutter_test/lib/src/binding.dart index 95cce4eff0..399a0226d9 100644 --- a/packages/flutter_test/lib/src/binding.dart +++ b/packages/flutter_test/lib/src/binding.dart @@ -352,6 +352,18 @@ abstract class TestWidgetsFlutterBinding extends BindingBase _currentTestCompleter.complete(null); } + /// Called when the framework catches an exception, even if that exception is + /// being handled by [takeException]. + /// + /// This is called when there is no pending exception; if multiple exceptions + /// are thrown and [takeException] isn't used, then subsequent exceptions are + /// logged to the console regardless (and the test will fail). + @protected + void reportExceptionNoticed(FlutterErrorDetails exception) { + // By default we do nothing. + // The LiveTestWidgetsFlutterBinding overrides this to report the exception to the console. + } + Future _runTest(Future testBody(), VoidCallback invariantTester, String description) { assert(description != null); assert(_currentTestDescription == null); @@ -374,6 +386,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase library: 'Flutter test framework' ); } else { + reportExceptionNoticed(details); // mostly this is just a hook for the LiveTestWidgetsFlutterBinding _pendingExceptionDetails = details; } }; @@ -952,6 +965,21 @@ class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding { return _runTest(testBody, invariantTester, description); } + @override + void reportExceptionNoticed(FlutterErrorDetails exception) { + final DebugPrintCallback testPrint = debugPrint; + debugPrint = debugPrintOverride; + debugPrint('(The following exception is now available via WidgetTester.takeException:)'); + FlutterError.dumpErrorToConsole(exception, forceReport: true); + debugPrint( + '(If WidgetTester.takeException is called, the above exception will be ignored. ' + 'If it is not, then the above exception will be dumped when another exception is ' + 'caught by the framework or when the test ends, whichever happens first, and then ' + 'the test will fail due to having not caught or expected the exception.)' + ); + debugPrint = testPrint; + } + @override void postTest() { super.postTest();