Build: Simple Counter Test
A screen with a counter and a button to increase the count. We want to write tests to cover the button tap and counter display.
Target UI
Counter: 0 [ Increment ]
Counter: 0 [ Increment ]
import 'package:flutter/material.dart'; class CounterScreen extends StatefulWidget { @override State<CounterScreen> createState() => _CounterScreenState(); } class _CounterScreenState extends State<CounterScreen> { int _counter = 0; void _increment() { setState(() { _counter++; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Counter Test')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Counter: $_counter', key: const Key('counterText')), const SizedBox(height: 20), ElevatedButton( key: const Key('incrementButton'), onPressed: _increment, child: const Text('Increment'), ), ], ), ), ); } } // TODO: Write widget tests for initial counter and increment button
import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; class CounterScreen extends StatefulWidget { @override State<CounterScreen> createState() => _CounterScreenState(); } class _CounterScreenState extends State<CounterScreen> { int _counter = 0; void _increment() { setState(() { _counter++; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Counter Test')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Counter: $_counter', key: const Key('counterText')), const SizedBox(height: 20), ElevatedButton( key: const Key('incrementButton'), onPressed: _increment, child: const Text('Increment'), ), ], ), ), ); } } void main() { testWidgets('Initial counter value is 0', (WidgetTester tester) async { await tester.pumpWidget(MaterialApp(home: CounterScreen())); expect(find.byKey(const Key('counterText')), findsOneWidget); expect(find.text('Counter: 0'), findsOneWidget); }); testWidgets('Tapping increment button increases counter', (WidgetTester tester) async { await tester.pumpWidget(MaterialApp(home: CounterScreen())); final Finder button = find.byKey(const Key('incrementButton')); await tester.tap(button); await tester.pump(); expect(find.text('Counter: 1'), findsOneWidget); }); }
This code defines a simple counter screen with a button to increase the count. The key parts are:
Counter: 0 [ Increment ]
What does test coverage measure in a Flutter app?
Which command runs Flutter tests and collects coverage data?
?flutter test --coverage.flutter run runs the app, flutter build builds it, and flutter analyze checks code style, none collect coverage.Given this test code snippet, what is the coverage result for increment() method?
class Counter {
int value = 0;
void increment() {
value++;
}
}
void main() {
final counter = Counter();
test('value starts at 0', () {
expect(counter.value, 0);
});
}increment().increment() is never executed, coverage for it is 0%.Identify the error in this test setup that causes coverage to be incomplete:
void main() {
final counter = Counter();
test('increment test', () {
counter.increment;
expect(counter.value, 1);
});
}counter.increment; which references the method but does not call it.increment() is not called, coverage misses this method.You want to improve test coverage for this class. Which test addition best increases coverage?
class Calculator {
int add(int a, int b) => a + b;
int subtract(int a, int b) => a - b;
int multiply(int a, int b) => a * b;
}Current tests only call add().
add() is tested; subtract() and multiply() are not covered.subtract() and multiply() will increase coverage.Pass the test and this pattern moves unknown