github.com/blugnu/test-report@v0.1.3/parser_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "os" 8 "os/exec" 9 "testing" 10 11 "github.com/blugnu/test" 12 ) 13 14 func generate() { 15 pwd, _ := os.Getwd() 16 defer func() { _ = os.Chdir(pwd) }() 17 if err := os.Chdir("testdata"); err != nil { 18 fmt.Printf("chdir 'testdata': %s\n", err) 19 return 20 } 21 22 testPackages := func(dest string, pkgs ...string) { 23 cmd := exec.Command("go", append([]string{"test", "-json"}, pkgs...)...) 24 output, err := cmd.CombinedOutput() 25 if err != nil { 26 fmt.Printf("%s: %s\n", cmd, err) 27 } 28 29 file, err := os.Create(dest) 30 if err != nil { 31 fmt.Printf("create testdata/%s: %s\n", dest, err) 32 } 33 defer file.Close() 34 if err := cmd.Run(); err != nil { 35 fmt.Printf("run '%s': %s\n", cmd, err) 36 } 37 38 if _, err := io.Copy(file, bytes.NewReader(output)); err != nil { 39 fmt.Printf("save output to 'testdata/%s': %s\n", dest, err) 40 } 41 } 42 43 testPackages("packages.json", "./pkga", "./pkgb") 44 testPackages("no-test-files.json", "./no-test-files") 45 testPackages("no-code.json", "./no-code") 46 } 47 48 func TestParse(t *testing.T) { 49 // ARRANGE 50 generate() 51 52 // ARRANGE 53 testcases := []struct { 54 scenario string 55 exec func(t *testing.T) 56 }{ 57 {scenario: "packages.json", 58 exec: func(t *testing.T) { 59 report := &testrun{} 60 input, err := os.Open("./testdata/packages.json") 61 if err != nil { 62 t.Fatalf("error loading test data: %s", err) 63 } 64 defer input.Close() 65 p := parser{} 66 67 // ACT 68 err = p.parse(input, report) 69 70 // ASSERT 71 test.Error(t, err).IsNil() 72 test.That(t, len(report.packages)).Equals(2, "number of packages") 73 test.That(t, report.numTests).Equals(9, "number of tests") 74 test.That(t, report.numPassed).Equals(3, "tests passed") 75 test.That(t, report.numFailed).Equals(4, "tests failed") 76 test.That(t, report.numSkipped).Equals(2, "tests skipped") 77 78 test.Map(t, report.packages[1].tests[1].output).Equals(map[string][]string{ 79 "pkgb_test.go:11": { 80 "this test fails", 81 "with four", 82 "lines of output", 83 " the last is indented", 84 "raw output is not indented (unlike test failure output)", 85 }, 86 }) 87 }, 88 }, 89 {scenario: "no-test-files.json", 90 exec: func(t *testing.T) { 91 report := &testrun{} 92 input, err := os.Open("./testdata/no-test-files.json") 93 if err != nil { 94 t.Fatalf("error loading test data: %s", err) 95 } 96 defer input.Close() 97 p := parser{} 98 99 // ACT 100 err = p.parse(input, report) 101 102 // ASSERT 103 test.Error(t, err).IsNil() 104 test.That(t, len(report.packages)).Equals(1, "number of packages") 105 test.That(t, report.numTests).Equals(0, "number of tests") 106 test.That(t, report.numPassed).Equals(0, "tests passed") 107 test.That(t, report.numFailed).Equals(0, "tests failed") 108 test.That(t, report.numSkipped).Equals(0, "tests skipped") 109 }, 110 }, 111 } 112 for _, tc := range testcases { 113 t.Run(tc.scenario, func(t *testing.T) { 114 tc.exec(t) 115 }) 116 } 117 }