How to widget test of a single screen

I have a basic counter app with provider. I can run the test just fine with terminal.

But I want to use vscode instead. I want to see the green checkmarks.

When I run debug on the widget test file, I get the following exception:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════ I/flutter
( 1586): The following assertion was thrown building Scaffold(dirty,
state: ScaffoldState#e4d79(tickers: I/flutter ( 1586): tracking 2
tickers)): I/flutter ( 1586): No Directionality widget found.
I/flutter ( 1586): Scaffold widgets require a Directionality widget
ancestor.

My understanding of this exception is that the widget is being pumped without the material app. So I added and then added “ChangeNotifierProvider” as a parent.

Now this runs the app and produces an image of the home screen. Nothing in the “TEST RESULTS” tab in the terminal of vscode.

I’m learning testing to implemnt in my other app, with multiple screens and providers.

  1. How can I do a widget test inside vscode of a single screen ?
  2. Do I have to add the “ChangeNotifierProvider” and Material App in every widget test file for every screen?


import 'package:flutter/material.dart';
import 'package:learn_testing/counter_model.dart';
import 'package:learn_testing/decrement_button.dart';
import 'package:learn_testing/increment_button.dart';
import 'package:provider/provider.dart';

class CounterScreen extends StatelessWidget {
  const CounterScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        key: const ValueKey("counter screen appbar"),
        title: const Text("Home Screen"),
      ),
      body: SizedBox(
        width: double.infinity,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Consumer<CounterModel>(
              builder: (context, value, child) {
                return Text(
                  key: const ValueKey("counter value"),
                  value.counter.toString(),
                  style: TextStyle(
                    fontSize: Theme.of(context).textTheme.headlineLarge?.fontSize,
                  ),
                );
              },
            ),
          ],
        ),
      ),
      floatingActionButton: Container(
        margin: const EdgeInsets.only(left: 45, right: 15),
        child: const Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            DecrementButton(),
            IncrementButton(),
          ],
        ),
      ),
    );
  }
}

The test file

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

void main() {
  testWidgets(
    'counter screen test',
    (tester) async {
      await tester.pumpWidget(
        // const CounterScreen(),
        ChangeNotifierProvider<CounterModel>(
          create: (context) => CounterModel(),
          child: const MaterialApp(home: CounterScreen()),
        ),
      );

      final appbar = find.byKey(const ValueKey("counter screen appbar"));
      expect(appbar, findsOneWidget);

      final counterValue = find.byKey(const ValueKey("counter value"));
      expect(counterValue, findsOne);

      final incrementButton = find.byKey(const ValueKey("increment button"));
      expect(incrementButton, findsOne);

      final decrementButton = find.byKey(const ValueKey("decrement button"));
      expect(decrementButton, findsOne);

      final counterStartsAtZero = find.text("0");
      expect(counterStartsAtZero, findsOneWidget);
    },
  );
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật