0Pricing
Zig Academy · Lesson

Writing test Blocks

Tests live right next to your code.

Writing test Blocks is a free Zig 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 Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Tests Live in Your Code

In Zig you do not need a separate test file. A test block sits right beside the code it checks, in the very same source file.

The test Keyword

You start a test with the test keyword, a name in quotes, and a block. The compiler collects every one of these for you.

test "adds two numbers" {
    // checks go here
}

Name Your Tests Clearly

The string after test is its name. A clear name like "parses empty input" shows up in the report and tells you what failed.

test "parses empty input" {}

Import std to Assert

Most tests need helpers, so you bring in the standard library at the top with an @import call and use std.testing inside.

const std = @import("std");

A First Real Test

Inside the block you compute something and check it. Here expect passes only when the boolean condition is true.

test "two plus two" {
    try std.testing.expect(2 + 2 == 4);
}

Tests Can Fail

Assertion helpers return an error when the check is false. That is why you write try before them, letting the failure end the test.

try std.testing.expect(1 == 2);

Test a Function You Wrote

A test usually calls one of your own functions and checks its result. The function and the test can share the same file.

fn add(a: i32, b: i32) i32 {
    return a + b;
}

Checking the Result

Now exercise that function from a test and confirm the output is what you expect with a single expect call.

test "add works" {
    try std.testing.expect(add(2, 3) == 5);
}

Many Tests, One File

You can stack as many test blocks as you like. Each runs in isolation, so a failure in one does not block the others.

Tests Are Compiled Code

A test block is normal Zig, fully type-checked. If it does not compile, the test run stops before anything even executes.

Why Inline Tests Help

Keeping checks next to the code means tests evolve with it. When you change a function, its test is right there to update.

Quick Check

You want to declare a unit test in a Zig source file. What is the right shape?

Recap

You met the test block: named, inline, fully compiled Zig that you assert with try. Tests live right next to the code they protect. ✅

Frequently asked questions

Is the “Writing test Blocks” lesson free?

Yes — the full text of “Writing test Blocks” 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 “Writing test Blocks”?

Tests live right next to your code. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Writing test Blocks” 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

  1. Writing test Blocks
  2. Assertions with std.testing
  3. The Testing Allocator Catches Leaks
  4. Running and Filtering Tests
← Back to Zig Academy