package app import ( "os" "path/filepath" "testing" ) // ev 是 testEvent 的简写,只用于测试里造事件。 func ev(a *App, action, pkg, test string, elapsed float64, output string) { a.applyTestEvent(action, pkg, test, elapsed, output) } // TestTestTreeBasic 一个包里的一个用例从 run 到 pass。 func TestTestTreeBasic(t *testing.T) { a := &App{} ev(a, "run", "demo", "TestAdd", 0, "") ev(a, "pass", "demo", "TestAdd", 0.02, "") if len(a.test.nodes) != 2 { t.Fatalf("应有包节点 + 用例节点共 2 个,实际 %d", len(a.test.nodes)) } pkg := a.test.nodes[0] if pkg.Name != "" || pkg.Depth != 0 { t.Fatalf("第一个应是包节点: %+v", pkg) } n := a.test.nodes[1] if n.Name != "TestAdd" || n.Status != testPass || n.Elapsed != 0.02 { t.Fatalf("用例节点不对: %+v", n) } } // TestTestTreeSubtest 子测试靠名字里的斜杠还原层级。 func TestTestTreeSubtest(t *testing.T) { a := &App{} ev(a, "run", "demo", "TestAdd/a", 0, "") ev(a, "pass", "demo", "TestAdd/a", 0.01, "") ev(a, "run", "demo", "TestAdd/b/c", 0, "") ev(a, "fail", "demo", "TestAdd/b/c", 0.03, "") byName := map[string]*testNode{} for _, n := range a.test.nodes { byName[n.Name] = n } if byName["TestAdd/a"].Depth != 2 { t.Fatalf("一级子测试缩进应为 2,实际 %d", byName["TestAdd/a"].Depth) } if byName["TestAdd/b/c"].Depth != 3 { t.Fatalf("二级子测试缩进应为 3,实际 %d", byName["TestAdd/b/c"].Depth) } if byName["TestAdd/b/c"].Status != testFail { t.Fatalf("失败状态没记上: %+v", byName["TestAdd/b/c"]) } } // TestTestTreeOutput 输出挂在对应节点上,包级输出挂在包节点上。 func TestTestTreeOutput(t *testing.T) { a := &App{} ev(a, "run", "demo", "TestFoo", 0, "") ev(a, "output", "demo", "TestFoo", 0, "=== RUN TestFoo\n") ev(a, "output", "demo", "TestFoo", 0, " foo_test.go:12: 期望 1 得到 2\n") ev(a, "fail", "demo", "TestFoo", 0.05, "") ev(a, "output", "demo", "", 0, "FAIL\tdemo\t0.05s\n") byName := map[string]*testNode{} for _, n := range a.test.nodes { byName[n.Name] = n } if got := byName["TestFoo"].Output; len(got) != 2 { t.Fatalf("用例输出应有 2 行,实际 %d: %v", len(got), got) } if got := byName[""].Output; len(got) != 1 { t.Fatalf("包输出应有 1 行,实际 %d", len(got)) } } // TestTestCounts 统计只算真正的用例,不把包节点算进去。 func TestTestCounts(t *testing.T) { a := &App{} ev(a, "pass", "demo", "TestA", 0, "") ev(a, "fail", "demo", "TestB", 0, "") ev(a, "skip", "demo", "TestC", 0, "") pass, fail, skip := a.testCounts() if pass != 1 || fail != 1 || skip != 1 { t.Fatalf("统计不对: %d/%d/%d", pass, fail, skip) } if label := a.testTabLabel(); label != "测试 1✗" { t.Fatalf("有失败时页签要带角标,实际 %q", label) } } // TestTestTreeFailLocation 失败输出里的 file:line 要还原成磁盘路径挂到节点上—— // 这是「点失败用例跳源码」的数据来源,整条链一起测。 func TestTestTreeFailLocation(t *testing.T) { root := t.TempDir() if err := os.WriteFile(filepath.Join(root, "go.mod"), []byte("module demo\n\ngo 1.21\n"), 0o644); err != nil { t.Fatal(err) } dir := filepath.Join(root, "internal", "app") if err := os.MkdirAll(dir, 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(dir, "app_test.go"), []byte("package app\n"), 0o644); err != nil { t.Fatal(err) } a := &App{root: root} ev(a, "run", "demo/internal/app", "TestAdd", 0, "") ev(a, "output", "demo/internal/app", "TestAdd", 0, "=== RUN TestAdd\n") ev(a, "output", "demo/internal/app", "TestAdd", 0, " app_test.go:9: 期望 1 得到 2\n") ev(a, "fail", "demo/internal/app", "TestAdd", 0.01, "") var n *testNode for _, x := range a.test.nodes { if x.Name == "TestAdd" { n = x } } if n == nil || n.File == "" { t.Fatalf("失败节点应带定位: %+v", n) } if want := filepath.Join(dir, "app_test.go"); n.File != want || n.Line != 9 { t.Fatalf("定位不对: %s:%d,期望 %s:9", n.File, n.Line, want) } } // TestRunFilterForAnchor 与 go test -run 的语义对齐:整段锚定、子测试分段。 func TestRunFilterForAnchor(t *testing.T) { if got := runFilterFor("TestAdd/b/c"); got != "^TestAdd$/^b$/^c$" { t.Fatalf("子测试过滤串不对: %q", got) } } // TestPackagePattern 文件路径换算成 go test 的包模式。 func TestPackagePattern(t *testing.T) { cases := []struct{ root, path, want string }{ {`D:\proj`, `D:\proj\internal\app\x.go`, "./internal/app"}, {`D:\proj`, `D:\proj\main.go`, "."}, {`D:\proj`, `D:\proj\a\b\c.go`, "./a/b"}, } for _, c := range cases { if got := packagePattern(c.root, c.path); got != c.want { t.Fatalf("%s → 期望 %s 实际 %s", c.path, c.want, got) } } } // TestTestStatusMark 状态符号。 func TestTestStatusMark(t *testing.T) { cases := map[testStatus]string{ testPass: "✓", testFail: "✗", testSkip: "○", testRunning: "⟳", testNone: "·", } for st, want := range cases { if got := st.mark(); got != want { t.Fatalf("%v 的符号应为 %q 实际 %q", st, want, got) } } }