Flutter unit test failed

2,667

The actual test you posted looks like a regular "test" test, as opposed to a "flutter_test" test. So you probably just want to run it using regular dart rather than via flutter's test harness.

The diagnostics we give in these situations are horrific. That's being tracked at: https://github.com/flutter/flutter/issues/6187

Share:
2,667
TaylorR
Author by

TaylorR

I am in China, often blocked by GFW. But I long for free talks and free thoughts! Develop with PHP, MySQL, Vue.js, dart. Learning Flutter, Dart now.

Updated on December 01, 2022

Comments

  • TaylorR
    TaylorR over 1 year

    I was trying to do a unit test in Flutter.

    In pubspec.yaml the dependency is included:

    test: any

    Running flutter analyze shows no warnings.

    Created a unit_test.dart under test folder in my project:

    import 'package:test/test.dart';
    
    void main()
    {
        test('this is a test', (){
            expect(42, equals(42));
            //expect(42,42);
        });
    }
    

    Running flutter test shows me the following error:

    ➜  HelloFlutter flutter test test/unit_test.dart
    00:00 +0: loading /home/tr/programs/HelloFlutter/test/unit_test.dart            00:00 +0 -1: loading /home/tr/programs/HelloFlutter/test/unit_test.dart         00:00 +0 -1: loading /home/tr/programs/HelloFlutter/test/unit_test.dart                            
      Failed to load "/home/tr/programs/HelloFlutter/test/unit_test.dart": Failed assertion: boolean expression must not be null
      package:test              test
      test/unit_test.dart 6:2   main
      dart:async                _StreamController.add
      websocket_impl.dart 1111  _WebSocketImpl._WebSocketImpl._fromSocket.<fn>
      dart:async                _EventSinkWrapper.add
      websocket_impl.dart 333   _WebSocketProtocolTransformer._messageFrameEnd
      websocket_impl.dart 228   _WebSocketProtocolTransformer.add
    
    00:00 +0 -1: Some tests failed.     
    

    Any hint?

  • TaylorR
    TaylorR over 7 years
    Tried the above method, when running flutter packages get, it gets these errors: Incompatible dependencies on flutter... pub get failed
  • Alexandre Ardhuin
    Alexandre Ardhuin over 7 years
    It works on my project. Could you post your pubspec.yaml ?
  • Günter Zöchbauer
    Günter Zöchbauer over 7 years
    I also tried it in my flutter project and this fixed it for me.
  • TaylorR
    TaylorR over 7 years
    The pubspec.yaml is the default one created by flutter create. Added the lines above per Alex's input only.
  • Alexandre Ardhuin
    Alexandre Ardhuin over 7 years
    Really strange because I did flutter create, created a file test/my_test.dart with your test content and ran flutter test to see the same error you faced. After putting only the dependency mention in my answer the error went away and the test was run.
  • TaylorR
    TaylorR over 7 years
    Let me try later today and revert.
  • Ian Hickson
    Ian Hickson over 7 years
    Oh man, that explains why I keep seeing people do expects(42, 42) !
  • TaylorR
    TaylorR over 7 years
    Tried again today and seems working with Alex's method.