知识卡片
子测试按行为场景分组
t.Run为同一行为的不同输入或前提建立有名字的子测试,使失败能指向具体场景。示例将指定人名和空名默认值分开,既共享断言工具又保留各自期望。共用代码不意味着共用可变状态一定安全,安排测试数据仍要明确。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Hello,World · 查看原书依据
func TestHello(t *testing.T) { t.Run("saying hello to people", func(t *testing.T) { got := Hello("Chris") want := "Hello, Chris" if got != want { t.Er
参考来源
- 位置:《通过测试学习 Go 编程》Hello,World,spine005(book-sources;extracted/通过测试学习 Go 编程/_epub-src/OEBPS/Text/04.html)。
- 结论依据:两个问候场景和共享assertCorrectMessage的实际结构。
- 原始内容:func TestHello(t *testing.T) { t.Run("saying hello to people", func(t *testing.T) { got := Hello("Chris") want := "Hello, Chris" if got != want { t.Er