File: summary_counts_test.go

package info (click to toggle)
golang-github-mfridman-tparse 0.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,780 kB
  • sloc: makefile: 53; sh: 41
file content (118 lines) | stat: -rw-r--r-- 3,344 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package parsetest

import (
	"os"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/mfridman/tparse/parse"
)

func TestSummaryCounts(t *testing.T) {
	t.Parallel()

	// This test depends on metrics_test.jsonl, which contains the output of 9 std lib packages:
	// go test -count=1 fmt strings bytes bufio crypto log mime sort time -json

	fileName := "./testdata/metrics_test.jsonl"
	f, err := os.Open(fileName)
	require.NoError(t, err)

	summary, err := parse.Process(f)
	require.NoError(t, err)
	assert.Len(t, summary.Packages, 9)

	for name, pkg := range summary.Packages {
		if pkg.Summary == nil {
			t.Fatalf("package %q cannot contain nil summary", name)
		}
		if pkg.Summary.Action != parse.ActionPass {
			t.Logf("failed pkg: %v", name)
			t.Fatalf("unexpected action %q, want %q", pkg.Summary.Action, parse.ActionPass)
		}
	}

	tests := []struct {
		name                           string
		total, passed, skipped, failed int
		elapsed                        float64
	}{
		{"fmt", 59, 58, 1, 0, 0.22},
		{"strings", 107, 107, 0, 0, 5.494},
		{"bytes", 123, 123, 0, 0, 3.5380000000000003},
		{"bufio", 69, 69, 0, 0, 0.07},
		{"crypto", 5, 5, 0, 0, 0.016},
		{"log", 8, 8, 0, 0, 0.085},
		{"mime", 20, 20, 0, 0, 0.025},
		{"sort", 37, 36, 1, 0, 3.117},
		{"time", 118, 117, 1, 0, 7.157},
	}

	for _, test := range tests {
		t.Run(test.name+"_test", func(t *testing.T) {
			pkg := summary.Packages[test.name]
			if len(pkg.Tests) != test.total {
				t.Fatalf("got %d total tests in package %q, want %d total tests", len(pkg.Tests), test.name, test.total)
			}

			pa := pkg.TestsByAction(parse.ActionPass)
			if len(pa) != test.passed {
				t.Fatalf("got %d passed tests in package %q, want %d passed tests", len(pa), test.name, test.passed)
			}

			sk := pkg.TestsByAction(parse.ActionSkip)
			if len(sk) != test.skipped {
				t.Fatalf("got %d passed tests in package %q, want %d passed tests", len(sk), test.name, test.skipped)
			}

			fa := pkg.TestsByAction(parse.ActionFail)
			if len(fa) != test.failed {
				t.Fatalf("got %d failed tests in package %q, want %d failed tests", len(fa), test.name, test.failed)
			}

			if pkg.Summary.Elapsed != test.elapsed {
				t.Fatalf("got elapsed time %f for package %q, want %f", pkg.Summary.Elapsed, test.name, test.elapsed)
			}
		})
	}
}

func TestElapsed(t *testing.T) {
	t.Parallel()

	// This test depends on elapsed_test.jsonl, which contains the output of 2 std lib tests
	// with known elapsed time.
	// go test -count=1 strings -run="^(TestCompareStrings|TestCaseConsistency$)" -json -cover

	expected := map[string]float64{
		"TestCompareStrings":  3.49,
		"TestCaseConsistency": 0.17,
	}

	fileName := "./testdata/elapsed_test.jsonl"
	f, err := os.Open(fileName)
	require.NoError(t, err)
	defer f.Close()

	summary, err := parse.Process(f)
	require.NoError(t, err)
	assert.Len(t, summary.Packages, 1)

	pkg, ok := summary.Packages["strings"]
	if !ok {
		t.Fatalf(`got unexpected pkg: %v\nwant "strings"`, pkg)
	}
	assert.Len(t, pkg.Tests, 2)

	for _, test := range pkg.Tests {
		wantElapsed, ok := expected[test.Name]
		if !ok {
			t.Errorf("got unknown test name %q", test.Name)
		}
		if test.Elapsed() != wantElapsed {
			t.Errorf("got %v elapsed time for test: %q, want %v", test.Elapsed(), test.Name, wantElapsed)
		}
	}
}