55

I have an app, it is very simple and have just one widget. It is working fine, however when I run integration test by calling:

$ flutter drive --target=test_driver/app.dart 

I get the following error:

 file:///Users/myuser/flutter/packages/flutter_test/lib/src/accessibility.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui;
       ^
file:///Users/myuser/flutter/packages/flutter_test/lib/src/binding.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui;
       ^
file:///Users/myuser/flutter/packages/flutter_test/lib/src/matchers.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui;
       ^
file:///Users/myuser/flutter/packages/flutter_test/lib/src/matchers.dart:9:8: Error: Not found: 'dart:ui'
import 'dart:ui';
       ^
file:///Users/myuser/flutter/packages/flutter_test/lib/src/test_pointer.dart:12:1: Error: Not found: 'dart:ui'
export 'dart:ui' show Offset;
^
file:///Users/myuser/flutter/packages/flutter/lib/src/rendering/binding.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui show window;
       ^
file:///Users/myuser/flutter/packages/flutter/lib/src/rendering/box.dart:6:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui show lerpDouble;
       ^
file:///Users/myuser/flutter/packages/flutter/lib/src/rendering/debug_overflow_indicator.dart:6:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui;
       ^
file:///Users/myuser/flutter/packages/flutter/lib/src/rendering/editable.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui show TextBox;
       ^
file:///Users/myuser/flutter/packages/flutter/lib/src/rendering/error.dart:5:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui show Paragraph, ParagraphBuilder, ParagraphConstraints, ParagraphStyle, TextStyle;
       ^
Stopping application instance.
Driver tests failed: 254

Note that when I run the app from Android Studio, it runs successfully. But when I run from terminal using the command quoted above, the app shows a white screen and it doesn't move from there until I get the error on my terminal.

Assuming it's a path issue, like test_driver not finding flutter packages like dart:ui, how can I make sure test_driver knows where dart:ui is ?

RobertoAllende
  • 8,744
  • 4
  • 30
  • 49
  • 1
    Flutter drive tests consist of 2 parts. One part is code that runs in the emulator/device, and the other part is code that drives the code from the former part. The code in the later part can't import `dart:ui`, not directly and not transitively. – Günter Zöchbauer Sep 23 '18 at 11:03
  • 1
    just make sure your imports are `import 'package:flutter_driver/flutter_driver.dart';` and `import 'package:test/test.dart';` – AbdulMomen عبدالمؤمن Apr 23 '19 at 08:45
  • Hi @RobertoAllende, any update for your question? Plz write an answer If you have found the solution, coz I think I have a problem like this. tq – MNFS Aug 22 '20 at 16:26
  • @MNFS to me the answer is the one I already posted. I just marked as the answer because that's the one that solved my problem. – RobertoAllende Aug 24 '20 at 01:46

12 Answers12

25

Make sure that the import is set to this:

import 'package:test/test.dart';

instead of this:

import 'package:flutter_test/flutter_test.dart';
Umair Adil
  • 968
  • 1
  • 10
  • 24
  • But flutter_test is what the cookbook says to use. How can this be? https://flutter.dev/docs/cookbook/testing/widget/introduction#complete-example – Pete Alvin May 26 '21 at 17:11
  • 2
    @PeteAlvin that's a widget test, not an integration test. https://flutter.dev/docs/cookbook/testing/integration/introduction#5-write-the-tests integration test example uses the `test/test.dart`. – Mo... Jun 05 '21 at 23:10
21

The integration tests can have no imports to your main App code or other flutter code that runs in the App - otherwise they will fail with your seen error.

Have a read of https://flutter.io/cookbook/testing/integration-test-introduction/ as this gives an example of integration tests with an app starting point that allows you to run setup code before your actual driver tests run if this is what you are looking to do. Otherwise don't use key values that use constants from your main code (as mentioned here http://cogitas.net/write-integration-test-flutter/).

Chris
  • 411
  • 3
  • 8
11

This is old post, but I found another possible reason why test gave me this error:

When I was creating new Bloc class I created constructor with @required annotation and when I used Option+Enter for importing appropriate library, Android Studio imported 'package:flutter/cupertino.dart' library instead of 'package:meta/meta.dart' and that is the reason my unit test didn't start in the first place.

After importing correct library, all tests have passed!

Happy testing your code! :D

Draško
  • 2,119
  • 4
  • 41
  • 73
  • 2
    This was my issue too! I was importing 'package:flutter/foundation.dart'. I switched to package:meta/meta.dart' and the error disappeared. Thanks! – Victor Eronmosele Jan 23 '21 at 19:53
10

Here is the solution for adding these test cases:

In Android studio in the run dropdown you

  1. select Edit Configurations
  2. Press the + button and select Flutter test
  3. Make sure the Test scope is All in file and point it at your test file. You can now run the individual test file and also debug it in android studio by selecting this configuration in the run drop-down.
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ash
  • 5,525
  • 1
  • 40
  • 34
  • Ok in Android Studio. In VS Code, execute the command 'flutter test' in a terminal window. Ex: 'flutter test .\test\screens\test_screen_mixin.dart' – Jean-Pierre Schnyder Jun 10 '22 at 01:39
6

Remove all imports to package:flutter/... from the test driver's code, like:

import 'package:flutter/widgets.dart';

Roger Gusmao
  • 3,788
  • 1
  • 20
  • 17
  • This was the most helpful for me; it includes _anything_ which starts with `flutter/` (in my case I was trying to do debugPrint, which requires a flutter import) – devrobf Apr 04 '22 at 16:03
3

I was getting these erros because I was trying to import a widget on a test_driver file. I also got the same error if I try to use find.byWidget.

I was trying to import a widget or use find.byWidget because I wanted to check the existence of a widget on a screen.

These erros are pretty much similar to

The built-in library 'dart:ui' is not available on the stand-alone VM.

Then, in order to check the existence of a widget on screen in a test_driver file, I had to use find.byKeyValue. For example, given the following dummy widget, defined in a file within my app:

class MyDummyWidget extends StatelessWidget {

  MyDummyWidget(): super(key: new Key('MyDummyWidget'));

  @override
  Widget build(BuildContext context) {
    return Center();
  }
}

To check if it is being displayed on screen, I define the following test within the test_driver:

void main() {
  group('My tests', () {
    FlutterDriver driver;
    SerializableFinder myDummyWidget = find.byValueKey('MyDummyWidget');

    setUpAll(() async {
      driver = await FlutterDriver.connect();
    });

    tearDownAll(() async {
      if (driver != null) {
        driver.close();
      }
    });

    test('check if MyDummyWidget is being displayed', () async {
      await driver.waitFor(myDummyWidget);
    });
  });
}

Where the key definition is the required one in the first file, and then, the find.byValueKey and await driver ones are the essentials in the test file.

RobertoAllende
  • 8,744
  • 4
  • 30
  • 49
3

In Android Studio, right-click on the test file in the Project tree and select "Run". You should see 2 options:

enter image description here

Make sure you select the second option: "tests in widget_test..."

If you select the first option you will see the Error: Not found: 'dart:ui' and you lose the option to make a selection from the Run context menu.

user4617883
  • 1,277
  • 1
  • 11
  • 21
2

Don't simply run the test. It is running the pp virtually. So we can't import dart: UI. It'll give the error. Use command flutter drive --target=test_driver/app.dart to test it.

Guru Prasad mohapatra
  • 1,809
  • 13
  • 19
0

Finally, I got run widget tests in my app.

This soluction worked for me.

I found out why, I chose the first option instead of the flutter test. how silly I was, now removing this wrong test type from configuration and run with flutter test works now. See the image about run in Android Studio

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](https://stackoverflow.com/review/late-answers/29453014) – Adam Marshall Jul 23 '21 at 21:59
0

Just check that you don't have imports which load flutter packages.

In my case I had 'cupertino' package import from my file app_keys.dart which listed all keys in a format Key('<something>').

BUT!! UI tests use Key('<something>') constants for Finder but Integration tests - simple strings.

Reference

Henadzi Rabkin
  • 6,834
  • 3
  • 32
  • 39
0

This because the app is running virtually, thus it can't import UI.

Set up your project as specified here:

lib/
  ...
integration_test/
  foo_test.dart
  bar_test.dart
test/
  # Other unit tests go here.
test_driver/
  integration_test.dart

and run the exact command:

flutter drive \
  --driver=test_driver/integration_test.dart \
  --target=integration_test/foo_test.dart

Refer to the official repo documentation (above is an exact copy of it)

Ravindranath Akila
  • 209
  • 3
  • 35
  • 45
0

Make sure to name the folder integration_test or else it will not work, in my case it was test_integration, I changed it to integration_test and it worked.