'testing dart code in android studio (no widgets involved, only data structures and concepts)

I want to run this code in android studio. I got the entire file from this webpage: https://flutter.dev/docs/cookbook/persistence/sqlite. I already have the pubspec.yaml packages already set up, but im getting errors. Here's the widget_test.dart file that came by default. I commented out the await tester.pumpWidget(myApp()); because myApp() is no longer defined.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:sqlite_example/main.dart';

void main() {
  testWidgets('Counter increments smoke test', (WidgetTester tester) async {
    // Build our app and trigger a frame.
//    await tester.pumpWidget(myApp());

    // Verify that our counter starts at 0.
    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsNothing);

    // Tap the '+' icon and trigger a frame.
    await tester.tap(find.byIcon(Icons.add));
    await tester.pump();

    // Verify that our counter has incremented.
    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsOneWidget);
  });
}

error message:

Testing started at 23:12 ...
/Users/leozhang/flutter/bin/flutter --no-color test --machine test/widget_test.dart
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
  Expected: exactly one matching node in the widget tree
  Actual: _TextFinder:<zero widgets with text "0" (ignoring offstage widgets)>
   Which: means none were found but one was expected

When the exception was thrown, this was the stack:
#4      main.<anonymous closure> (file:///Users/leozhang/Desktop/DesktopStuff/flutter_playground_files/sqlite_example/test/widget_test.dart:19:5)
#5      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:124:25)
#6      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:696:19)
<asynchronous suspension>
#9      TestWidgetsFlutterBinding._runTest (package:flutter_test/src/binding.dart:679:14)
#10     AutomatedTestWidgetsFlutterBinding.runTest.<anonymous closure> (package:flutter_test/src/binding.dart:1050:24)
#16     AutomatedTestWidgetsFlutterBinding.runTest (package:flutter_test/src/binding.dart:1047:15)
#17     testWidgets.<anonymous closure> (package:flutter_test/src/widget_tester.dart:121:22)
#18     Declarer.test.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/declarer.dart:171:27)
<asynchronous suspension>
#19     Invoker.waitForOutstandingCallbacks.<anonymous closure> (package:test_api/src/backend/invoker.dart:242:15)
#24     Invoker.waitForOutstandingCallbacks (package:test_api/src/backend/invoker.dart:239:5)
#25     Declarer.test.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/declarer.dart:169:33)
#30     Declarer.test.<anonymous closure> (package:test_api/src/backend/declarer.dart:168:13)
#31     Invoker._onRun.<anonymous closure>.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/invoker.dart:392:25)
#45     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
#46     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
#47     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
(elided 28 frames from class _FakeAsync, package dart:async, package dart:async-patch, and package stack_trace)

This was caught by the test expectation on the following line:
  file:///Users/leozhang/Desktop/DesktopStuff/flutter_playground_files/sqlite_example/test/widget_test.dart line 19
The test description was:
  Counter increments smoke test
════════════════════════════════════════════════════════════════════════════════════════════════════

Test failed. See exception logs above.
The test description was: Counter increments smoke test

my main.dart just has the entire code from the website I mentioned earlier. Thanks for reading!



Solution 1:[1]

The issue here is that you're running Widget tests with testWidgets() function. To run tests without widgets, you can run unit tests using test() function as demonstrated on this guide.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Omatt