Assertions with std.testing
expect, expectEqual, and friends.
Assertions with std.testing is a free Zig Academy lesson on CoddyKit — lesson 2 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 Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The testing Namespace
Zig's assertion helpers live in std.testing. Pull it into a short name so each check reads cleanly inside your test blocks.
const testing = std.testing;expect for Booleans
The simplest helper is expect. It takes one boolean and fails the test when that condition is false.
try testing.expect(value > 0);Helpers Return Errors
Every testing helper returns an error on failure, so you always prefix it with try. That is what makes the test stop and report.
expectEqual for Values
To compare two values, reach for expectEqual. It checks equality and, on failure, prints both the expected and the actual value.
try testing.expectEqual(5, add(2, 3));Expected Comes First
With expectEqual the first argument is the expected value and the second is what your code produced. Order shapes the failure message.
try testing.expectEqual(42, result);Comparing Slices
Two slices are equal when their elements match, not their pointers. Use expectEqualSlices and tell it the element type.
try testing.expectEqualSlices(u8, "hi", out);Comparing Strings
For text, expectEqualStrings compares two byte slices and prints a readable diff when they differ, which is perfect for output checks.
try testing.expectEqualStrings("ok", msg);Asserting an Error
Sometimes a call should fail. Wrap it in expectError to assert it returns a specific error and not a value.
try testing.expectError(error.Bad, parse("x"));Approximate Floats
Floats rarely match exactly. Use expectApproxEqAbs with a small tolerance so tiny rounding differences do not fail your test.
try testing.expectApproxEqAbs(1.0, x, 0.001);Read the Failure Output
When an assertion fails, Zig prints the expected and actual values right in the report. That diff usually points straight at the bug.
Pick the Right Helper
Match the helper to the data: expectEqual for scalars, slice and string helpers for sequences, expectError for the failure path.
Quick Check
You need to assert that two text values are exactly equal in a test. Which helper fits best?
Recap
You learned the std.testing toolkit: expect, expectEqual, slice and string checks, plus expectError. Each one needs try in front. ✅
Frequently asked questions
Is the “Assertions with std.testing” lesson free?
Yes — the full text of “Assertions with std.testing” is free to read here on the web, and the Zig 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 Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Assertions with std.testing”?
expect, expectEqual, and friends. You practise Zig 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 Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Assertions with std.testing” 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 Zig Academy lesson?
Yes. Every Zig 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
- Writing test Blocks
- Assertions with std.testing
- The Testing Allocator Catches Leaks
- Running and Filtering Tests