Dart Testing
In this lesson, you will learn how to test Dart applications to ensure your code works correctly, reliably, and consistently.
Testing is a critical part of professional software development. Well-tested code reduces bugs, improves confidence, and makes future changes safer.
Why Testing Is Important
Testing helps you:
- Catch bugs early
- Verify expected behavior
- Prevent regressions
- Improve code quality
In real-world projects, automated tests are essential for CI/CD pipelines.
Types of Tests in Dart
Dart supports multiple types of testing:
- Unit Tests – test individual functions
- Integration Tests – test multiple components together
- End-to-End Tests – test full workflows
In this lesson, we focus mainly on unit testing.
Installing the Test Package
Dart uses the test package for testing.
Add it to your project:
dart pub add test --dev
This installs the testing framework as a development dependency.
Project Structure for Tests
Tests are usually placed inside a test folder.
Example structure:
project/
├─ lib/
│ └─ calculator.dart
└─ test/
└─ calculator_test.dart
Writing Your First Unit Test
Let’s test a simple addition function.
File: lib/calculator.dart
int add(int a, int b) {
return a + b;
}
Now create a test file.
File: test/calculator_test.dart
import 'package:test/test.dart';
import '../lib/calculator.dart';
void main() {
test('adds two numbers', () {
expect(add(2, 3), equals(5));
});
}
Running Tests
Run all tests using:
dart test
If the test passes, Dart will show a success message.
Testing Multiple Scenarios
You can add multiple test cases to cover different inputs.
void main() {
test('positive numbers', () {
expect(add(10, 5), equals(15));
});
test('negative numbers', () {
expect(add(-3, -2), equals(-5));
});
test('mixed numbers', () {
expect(add(-2, 4), equals(2));
});
}
Using Groups in Tests
Test groups help organize related tests.
void main() {
group('Addition Tests', () {
test('2 + 3 = 5', () {
expect(add(2, 3), equals(5));
});
test('0 + 0 = 0', () {
expect(add(0, 0), equals(0));
});
});
}
Testing for Exceptions
Sometimes you want to verify that errors are thrown correctly.
void divide(int a, int b) {
if (b == 0) {
throw Exception('Division by zero');
}
}
void main() {
test('throws exception on divide by zero', () {
expect(() => divide(10, 0), throwsException);
});
}
Testing Asynchronous Code
Dart tests support async and await.
Future fetchData() async {
await Future.delayed(Duration(seconds: 1));
return 42;
}
void main() {
test('async data fetch', () async {
final result = await fetchData();
expect(result, equals(42));
});
}
Best Practices for Testing
- Write tests for critical logic
- Keep tests simple and readable
- Test edge cases
- Run tests frequently
📝 Practice Exercises
Exercise 1
Write a test for a function that subtracts two numbers.
Exercise 2
Test a function that checks if a number is even.
Exercise 3
Write a test that verifies an exception is thrown for invalid input.
What’s Next?
In the next lesson, you will learn how to optimize Dart performance and make your applications faster and more efficient.