0
0
Fluttermobile~5 mins

Test coverage in Flutter

Choose your learning style9 modes available
Introduction

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.

When you want to make sure your app works well before releasing it.
When you add new features and want to check they don't break old ones.
When you fix bugs and want to confirm the fix works.
When you want to improve your app's quality step by step.
When you share your code with others and want to show it's tested.
Syntax
Flutter
flutter test --coverage

This command runs your tests and collects coverage data.

You need to have test files in your test/ folder.

Examples
Runs all tests and creates a coverage report.
Flutter
flutter test --coverage
Generates a readable HTML report from coverage data (run in terminal after tests).
Flutter
genhtml coverage/lcov.info -o coverage/html
Sample App

This simple test checks if the add function returns the right sum. Running flutter test --coverage will show coverage for this function.

Flutter
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);
  });
}
OutputSuccess
Important Notes

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.

Summary

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.