0Pricing
Zig Academy · Lektion

Assertions mit std.testing

expect, expectEqual und weitere Funktionen.

Assertions mit std.testing ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 2 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.

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

Häufig gestellte Fragen

Ist die Lektion „Assertions mit std.testing“ kostenlos?

Ja — der vollständige Text von „Assertions mit std.testing“ 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 „Assertions mit std.testing“?

expect, expectEqual und weitere Funktionen. 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 2 von 4.

Wie lange dauert die Lektion „Assertions mit std.testing“?

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