Test coverage shows how much of your app's code is checked by tests. It helps you find untested parts to make your app more reliable.
Test coverage in Flutter
flutter test --coverage
This command runs your tests and collects coverage data.
You need to have test files in your test/ folder.
flutter test --coverage
genhtml coverage/lcov.info -o coverage/html
This simple test checks if the add function returns the right sum. Running flutter test --coverage will show coverage for this function.
import 'package:flutter_test/flutter_test.dart'; int add(int a, int b) { return a + b; } void main() { test('add returns correct sum', () { expect(add(2, 3), 5); }); }
Test coverage does not guarantee your app has no bugs, but it helps find untested code.
Write simple tests first, then add more to cover different cases.
Use coverage reports to focus your testing efforts where it's missing.
Test coverage measures how much code your tests check.
Use flutter test --coverage to run tests and collect coverage.
Coverage helps improve app quality by showing untested parts.
Practice
What does test coverage measure in a Flutter app?
Solution
Step 1: Understand test coverage meaning
Test coverage shows what percentage of your code runs when tests are executed.Step 2: Compare options to definition
Only How much of the app's code is executed by tests matches this meaning by describing code executed by tests.Final Answer:
How much of the app's code is executed by tests -> Option AQuick Check:
Test coverage = code executed by tests [OK]
- Confusing coverage with number of tests
- Thinking coverage measures test speed
- Assuming coverage relates to app size
Which command runs Flutter tests and collects coverage data?
?Solution
Step 1: Recall Flutter test commands
The command to run tests and collect coverage isflutter test --coverage.Step 2: Eliminate incorrect commands
flutter runruns the app,flutter buildbuilds it, andflutter analyzechecks code style, none collect coverage.Final Answer:
flutter test --coverage -> Option AQuick Check:
Run tests with coverage = flutter test --coverage [OK]
- Using flutter run instead of flutter test
- Confusing build or analyze commands with testing
- Omitting the --coverage flag
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);
});
}Solution
Step 1: Analyze test coverage of increment()
The test only checks initial value; it never callsincrement().Step 2: Determine coverage for increment()
Sinceincrement()is never executed, coverage for it is 0%.Final Answer:
0% coverage for increment() method -> Option CQuick Check:
Method not called = 0% coverage [OK]
- Assuming all methods are covered by default
- Confusing test presence with code execution
- Ignoring that increment() is never called
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);
});
}Solution
Step 1: Check method call syntax
The code usescounter.increment;which references the method but does not call it.Step 2: Understand impact on coverage
Becauseincrement()is not called, coverage misses this method.Final Answer:
Missing parentheses to call increment() method -> Option BQuick Check:
Method call needs () to execute [OK]
- Forgetting () after method name
- Assuming method reference calls it
- Ignoring test name importance (not required)
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().
Solution
Step 1: Identify untested methods
Onlyadd()is tested;subtract()andmultiply()are not covered.Step 2: Add tests for untested methods
Writing tests that callsubtract()andmultiply()will increase coverage.Final Answer:
Add tests calling subtract() and multiply() methods -> Option DQuick Check:
Test all methods to increase coverage [OK]
- Testing only one method repeatedly
- Testing class existence without method calls
- Printing results without assertions doesn't count
Ready to prove it?
Pass the test and this pattern moves unknown
