What does setUp() do in a test file?
setUp() runs before every individual test, ensuring each test starts with a fresh state.
Write unit tests for your Dart functions and classes
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Unit tests verify individual functions and classes in complete isolation from the rest of your app. Each test focuses on a single piece of logic such as a calculation function, a model parser, or a validation method. Unit tests are the fastest type of test because they do not need a device, emulator, or any UI rendering. They catch bugs early in development and give you the confidence to refactor code knowing that if something breaks, a test will fail immediately. A well-tested Flutter app should have many unit tests covering all business logic, data transformations, and utility functions.
1// test/calculator_test.dart2import 'package:flutter_test/flutter_test.dart';34// Class to test5class Calculator {6int add(int a, int b) => a + b;7int divide(int a, int b) {8if (b == 0) throw ArgumentError('Cannot divide by zero');9return a ~/ b;10}11}1213void main() {14late Calculator calculator;1516// Runs before each test17setUp(() {18calculator = Calculator();19});2021group('Calculator', () {22test('add returns sum of two numbers', () {23expect(calculator.add(2, 3), equals(5));24expect(calculator.add(-1, 1), equals(0));25expect(calculator.add(0, 0), equals(0));26});2728test('divide returns integer division', () {29expect(calculator.divide(10, 3), equals(3));30expect(calculator.divide(9, 3), equals(3));31});3233test('divide by zero throws ArgumentError', () {34expect(35() => calculator.divide(10, 0),36throwsA(isA<ArgumentError>()),37);38});39});40}4142// Run: flutter test test/calculator_test.dart
Common matchers include equals(x), isTrue, isFalse, isNull, isNotNull, isA<Type>(), contains(), greaterThan(x), lessThan(x), and throwsA(). You can combine them for expressive assertions like expect(value, allOf(isNotNull, greaterThan(5))).
Strong unit tests target behavior boundaries, not only happy paths:
If a function can fail silently in production, it needs an explicit test for that path.
Write unit tests for your Dart functions and classes. Dart is the language behind Flutter, and clear Dart code makes your Flutter UI easier to maintain.
Think in architecture, reliability, and team-level maintainability.
Prefer readable names, explicit intent, and async/await over deeply nested callbacks.
When Dart basics are weak, state bugs, null crashes, and async confusion increase quickly.
Strong Dart habits lead to predictable APIs and cleaner boundaries between presentation and business logic.
As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.
setUp() runs before every individual test, ensuring each test starts with a fresh state.
Wrap the throwing call in a closure and use throwsA(isA<ExceptionType>()) matcher.
flutter test runs all tests in the test/ directory. You can also specify individual test files.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue