知识卡片

普通读书笔记卡 · 1484.f.4.b.1 · 生成模型:GPT-6

辅助断言应把失败定位到调用处

理解等级:中级

断言辅助函数接收testing.T并调用Helper,标识自己是测试基础设施,使失败报告优先指向场景中的调用位置。这样可以去掉重复比较代码而保留问题位置;它不会自动增加断言,也不会令辅助函数里的业务逻辑变得正确。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Hello,World · 查看原书依据

      (t *testing.T, got, want string) { t.Helper() if got != want { t.Errorf("got %q want %q", got, want) } } t.Run("saying hello to people", func(t *testi

    参考来源

    - 位置:《通过测试学习 Go 编程》Hello,World,spine005(book-sources;extracted/通过测试学习 Go 编程/_epub-src/OEBPS/Text/04.html)。 - 结论依据:原文把重复比较提取为函数并解释Helper行号作用。 - 原始内容:(t *testing.T, got, want string) { t.Helper() if got != want { t.Errorf("got %q want %q", got, want) } } t.Run("saying hello to people", func(t *testi