0Pricing
Dart Academy · Lesson

Your First test and expect

Assert behavior with matchers.

Your First test and expect is a free Dart Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Tests Matter

A test is code that checks your code still works. Write one once, and it guards that behavior forever. 🛡️

Add the Package

The official test package powers Dart testing. Add it under dev_dependencies so it ships only with your tooling.

dev_dependencies:
  test: ^1.25.0

Where Tests Live

By convention, tests sit in a top-level test/ folder and each file ends with _test.dart so the runner can find them.

Import the Library

Every test file starts by importing the test library, which gives you the test and expect helpers.

import 'package:test/test.dart';

The test Function

You wrap each check in a test call: a short description plus a body that runs your assertions.

test('adds two numbers', () {
  expect(2 + 2, 4);
});

Describe Behavior, Not Code

Good test descriptions read like sentences. Say what should happen, like returns zero for an empty list, not which line you call.

Meet expect

The expect function compares an actual value to what you expect. If they differ, the test fails with a clear message.

expect(actual, expected);

Matchers Are the Magic

The second argument can be a plain value or a matcher like equals, isTrue, or isNull for richer, readable checks.

expect(name, equals('Dart'));
expect(found, isTrue);

Run the Suite

From your project root run dart test. It finds every _test.dart file and reports passes and failures.

dart test

Reading the Output

A green dot or +1 means a pass; a red failure shows the expected and actual values so you can fix the gap fast.

One Assertion, One Idea

Keep early tests focused: one clear expect per behavior makes failures obvious and tests easy to read.

Quick Check

Which call asserts that a value equals 4?

Recap

You added the test package, wrote a test with a clear name, asserted with expect, and ran dart test. That is the whole core loop. ✅

Frequently asked questions

Is the “Your First test and expect” lesson free?

Yes — the full text of “Your First test and expect” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “Your First test and expect”?

Assert behavior with matchers. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Your First test and expect” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Your First test and expect
  2. group, setUp, and tearDown
  3. Testing async Code and Streams
  4. Mocks, Fakes, and Coverage
← Back to Dart Academy