๐Ÿงช Lesson 10: Testing Your Code

โฑ 30-40 min ๐Ÿ“Š Intermediate ๐Ÿงช Quality Assurance ๐Ÿ”ฅ Professional 2026 ๐Ÿ”— Official Source
๐Ÿงช

Why Testing Matters

Imagine building a bridge and never testing it. Scary, right? Testing your code is like quality control - it catches problems before your users do!

๐Ÿ›ก๏ธ

Prevent Regressions

Tests catch bugs when you change old code

๐Ÿ“‹

Living Documentation

Tests show how code should behave

๐Ÿ’ช

Refactor Confidence

Change code fearlessly with a safety net

๐Ÿš€

Faster Development

Less manual testing, more building

๐ŸŽฏ

Today's Project: "Calculator Test Suite"

We'll build a comprehensive test suite for a calculator:

  • โž• Test basic arithmetic operations
  • ๐Ÿ”„ Test edge cases (zero, negatives, overflow)
  • โŒ Test error handling (division by zero)
  • โฑ Test async operations
  • ๐Ÿ“Š Organized with test groups
Step 1

Understanding Test Structure

๐Ÿ” Anatomy of a Test

import 'package:test/test.dart'; Bring in testing tools
group('Calculator', () { Organize related tests
test('adds two numbers', () { Define a single test case
expect(add(2, 3), equals(5)); Assert expected result

๐Ÿ–ฅ๏ธ Live Test Runner Simulator

Write a test and see it run:

๐Ÿ“ Test Code:

๐Ÿ“ค Test Results:

$ dart test
00:00 +0: Loading tests...
Step 2

Matchers: The Expect Power Tools

๐ŸŽฏ Interactive Matcher Explorer

Select a matcher category and test values:

equals() โœ… true
isNot(equals()) โŒ false
same() -
isA<String>() -
isNull -
isNotNull -
contains() -
hasLength() -
isEmpty -
contains() -
startsWith() -
endsWith() -
greaterThan() -
lessThan() -
inInclusiveRange() -
throwsException -
throwsFormatException -
returnsNormally -
Step 3

Organizing with Test Groups

๐Ÿ—๏ธ Test Group Hierarchy Builder

Build and visualize your test organization:

๐Ÿ“ Calculator
๐Ÿ“ addition
๐Ÿงช positive numbers
๐Ÿงช negative numbers
๐Ÿงช zero handling
๐Ÿ“ division

๐Ÿ“„ Generated Test Structure:

group('Calculator', () {
  group('addition', () {
    test('positive numbers', () { ... });
    test('negative numbers', () { ... });
    test('zero handling', () { ... });
  });
  
  group('division', () {
    test('basic division', () { ... });
    test('division by zero', () { ... });
  });
});
Step 4

Testing Async Code

โฑ Async Test Simulator

See how different async test patterns work:

Pattern 1: async/await

test('fetchData returns value', () async {
  String result = await fetchData();
  expect(result, equals('data'));
});

Pattern 2: completion() matcher

test('fetchData completes with value', () {
  expect(
    fetchData(),
    completion(equals('data')),
  );
});

Pattern 3: throws with async

test('async operation throws', () async {
  expect(
    () async => await failingOp(),
    throwsException,
  );
});
Step 5

Testing JSON Models

๐Ÿ“ฆ Model Test Generator

Generate tests for your JSON models automatically:

Model Fields (name:type:required):

Valid JSON:

๐Ÿงช Generated Tests:

group('User model', () {
  test('parses valid JSON correctly', () {
    final json = {'id': 1, 'name': 'Alice', 'email': 'alice@test.com', 'age': 30};
    final user = User.fromJson(json);
    expect(user.id, equals(1));
    expect(user.name, equals('Alice'));
    expect(user.email, equals('alice@test.com'));
    expect(user.age, equals(30));
  });

  test('handles missing optional fields', () {
    final json = {'id': 1, 'name': 'Alice'};
    final user = User.fromJson(json);
    expect(user.email, isNull);
    expect(user.age, isNull);
  });

  test('throws FormatException for invalid JSON', () {
    expect(
      () => User.fromJson({'wrong': 'format'}),
      throwsFormatException,
    );
  });
});
๐Ÿ†

Build: Complete Calculator Test Suite

๐Ÿ”ข Calculator Implementation

๐Ÿงช Complete Test Suite

๐Ÿ“Š Simulated Test Run

Click "Run Test Suite" to see results!

๐ŸŽฎ Testing Challenge: Quality Master Quiz

๐Ÿงช Score: 0/5
โšก Streak: 0
โฑ Time: 0s
๐Ÿ“Š Coverage: 0%

Loading question...

Tests Passing: ๐ŸŸข๐ŸŸข๐ŸŸข๐ŸŸข๐ŸŸข