Penegasan dengan std.testing
expect, expectEqual, dan kawan-kawan.
Penegasan dengan std.testing adalah pelajaran Zig Academy gratis di CoddyKit. Ini adalah pelajaran 2 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.
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. ✅
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Penegasan dengan std.testing” gratis?
Ya — teks lengkap “Penegasan dengan std.testing” 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 “Penegasan dengan std.testing”?
expect, expectEqual, dan kawan-kawan. 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 2 dari 4.
Berapa lama pelajaran “Penegasan dengan std.testing” 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
- Menulis Blok test
- Penegasan dengan std.testing
- Pengalokasi Pengujian Menangkap Kebocoran
- Menjalankan dan Memfilter Pengujian