0Pricing
Zig Academy · 课时

使用 std.testing 进行断言

掌握 expect、expectEqual 等断言。

使用 std.testing 进行断言 是 CoddyKit 上的免费 Zig Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Zig Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Zig Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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

常见问题解答

「使用 std.testing 进行断言」课时是免费的吗?

是的 — 「使用 std.testing 进行断言」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Zig Academy 课程的其余内容,请升级到 CoddyKit PRO。 Zig Academy 课程共包含 4 节课。

「使用 std.testing 进行断言」这节课中我会学到什么?

掌握 expect、expectEqual 等断言。 你通过在浏览器中直接运行的动手代码来练习 Zig Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Zig Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Zig Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「使用 std.testing 进行断言」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Zig Academy 课中编写并运行代码吗?

能。每节 Zig Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 编写 test 代码块
  2. 使用 std.testing 进行断言
  3. 测试分配器捕获内存泄漏
  4. 运行和筛选测试
← 返回 Zig Academy