0Pricing
Zig Academy · Lektion

test-Blöcke schreiben

Tests befinden sich direkt neben Ihrem Code.

test-Blöcke schreiben ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Zig Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. ✅

Häufig gestellte Fragen

Ist die Lektion „test-Blöcke schreiben“ kostenlos?

Ja — der vollständige Text von „test-Blöcke schreiben“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Zig Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „test-Blöcke schreiben“?

Tests befinden sich direkt neben Ihrem Code. Du übst Zig Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Zig Academy zu starten?

Keine Vorkenntnisse erforderlich. Zig Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „test-Blöcke schreiben“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Zig Academy-Lektion Code schreiben und ausführen?

Ja. Jede Zig Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. test-Blöcke schreiben
  2. Assertions mit std.testing
  3. Der Testing Allocator erkennt Leaks
  4. Tests ausführen und filtern
← Zurück zu Zig Academy