0Pricing
Zig Academy · Pelajaran

Menulis Blok test

Pengujian berada tepat di samping kode Anda.

Menulis Blok test adalah pelajaran Zig Academy gratis di CoddyKit. Ini adalah pelajaran 1 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Zig Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Zig Academy mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Menulis Blok test” gratis?

Ya — teks lengkap “Menulis Blok test” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Zig Academy, upgrade ke CoddyKit PRO. Kursus Zig Academy mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Menulis Blok test”?

Pengujian berada tepat di samping kode Anda. Kamu berlatih Zig Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Zig Academy?

Tidak diperlukan pengalaman sebelumnya. Zig Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 1 dari 4.

Berapa lama pelajaran “Menulis Blok test” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Zig Academy ini?

Ya. Setiap pelajaran Zig Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Menulis Blok test
  2. Penegasan dengan std.testing
  3. Pengalokasi Pengujian Menangkap Kebocoran
  4. Menjalankan dan Memfilter Pengujian
← Kembali ke Zig Academy