๐งช Lesson 10: Testing Your Code
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() โ
trueisNot(equals()) โ falsesame() -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:
๐ข๐ข๐ข๐ข๐ข