Skip to content

Commit eff4dcd

Browse files
committed
Merge pull request #21 from tatsuhiro-t/add-test-summary
Print test summary including failed test's result
2 parents 1e302ca + 346e912 commit eff4dcd

File tree

1 file changed

+111
-14
lines changed

1 file changed

+111
-14
lines changed

h2spec.go

Lines changed: 111 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,72 @@ type Test interface {
5959
}
6060

6161
type TestGroup struct {
62-
Section string
63-
Name string
64-
testGroups []TestGroup
65-
testCases []TestCase
62+
Section string
63+
Name string
64+
testGroups []*TestGroup
65+
testCases []*TestCase
66+
numTestCases int // the number of test cases under this group
67+
numSkipped int // the number of skipped test cases under this group
68+
numFailed int // the number of failed test cases under this group
6669
}
6770

6871
func (tg *TestGroup) Run(ctx *Context, level int) {
6972
runMode := ctx.GetRunMode(tg.Section)
7073

7174
if runMode == ModeSkip {
75+
tg.numSkipped += tg.numTestCases
7276
return
7377
}
7478

7579
tg.PrintHeader(level)
7680
if runMode == ModeAll {
7781
for _, testCase := range tg.testCases {
78-
testCase.Run(ctx, level+1)
82+
switch testCase.Run(ctx, level+1) {
83+
case Failed:
84+
tg.numFailed += 1
85+
case Skipped:
86+
tg.numSkipped += 1
87+
}
7988
}
8089
tg.PrintFooter(level)
8190
}
8291

8392
for _, testGroup := range tg.testGroups {
8493
testGroup.Run(ctx, level+1)
94+
tg.numSkipped += testGroup.numSkipped
95+
tg.numFailed += testGroup.numFailed
96+
}
97+
}
98+
99+
// PrintFailedTestCase prints failed TestCase results under this
100+
// TestGroup.
101+
func (tg *TestGroup) PrintFailedTestCase(level int) {
102+
if tg.numFailed == 0 {
103+
return
104+
}
105+
106+
tg.PrintHeader(level)
107+
108+
for _, tc := range tg.testCases {
109+
if tc.verdict {
110+
continue
111+
}
112+
tc.PrintError(tc.expected, tc.actual, level+1)
113+
}
114+
115+
for _, testGroup := range tg.testGroups {
116+
testGroup.PrintFailedTestCase(level + 1)
85117
}
86118
}
87119

88120
func (tg *TestGroup) AddTestCase(testCase *TestCase) {
89-
tg.testCases = append(tg.testCases, *testCase)
121+
tg.testCases = append(tg.testCases, testCase)
122+
tg.numTestCases += 1
90123
}
91124

92125
func (tg *TestGroup) AddTestGroup(testGroup *TestGroup) {
93-
tg.testGroups = append(tg.testGroups, *testGroup)
126+
tg.testGroups = append(tg.testGroups, testGroup)
127+
tg.numTestCases += testGroup.numTestCases
94128
}
95129

96130
func (tg *TestGroup) PrintHeader(level int) {
@@ -106,24 +140,44 @@ func (tg *TestGroup) PrintFooter(level int) {
106140
}
107141

108142
type TestCase struct {
109-
Desc string
110-
Spec string
111-
handler func(*Context) ([]Result, Result)
143+
Desc string
144+
Spec string
145+
handler func(*Context) ([]Result, Result)
146+
verdict bool // true if test passed
147+
expected []Result // expected result
148+
actual Result // actual result
112149
}
113150

114-
func (tc *TestCase) Run(ctx *Context, level int) {
151+
type TestResult int
152+
153+
// TestResult indicates the result of test case
154+
const (
155+
Failed TestResult = iota
156+
Skipped
157+
Passed
158+
)
159+
160+
func (tc *TestCase) Run(ctx *Context, level int) TestResult {
115161
expected, actual := tc.handler(ctx)
116162

117163
_, ok := actual.(*ResultSkipped)
118164
if ok {
119165
tc.PrintSkipped(actual, level)
120-
return
166+
return Skipped
121167
}
122168

169+
// keep expected and actual so that we can report the failed
170+
// test cases in summary.
171+
tc.expected = expected
172+
tc.actual = actual
173+
123174
if tc.evaluateResult(expected, actual) {
175+
tc.verdict = true
124176
tc.PrintResult(level)
177+
return Passed
125178
} else {
126179
tc.PrintError(expected, actual, level)
180+
return Failed
127181
}
128182
}
129183

@@ -181,11 +235,18 @@ func (tc *TestCase) PrintSkipped(actual Result, level int) {
181235
}
182236

183237
func NewTestGroup(section, name string) *TestGroup {
184-
return &TestGroup{section, name, nil, nil}
238+
return &TestGroup{
239+
Section: section,
240+
Name: name,
241+
}
185242
}
186243

187244
func NewTestCase(desc, spec string, handler func(*Context) ([]Result, Result)) *TestCase {
188-
return &TestCase{desc, spec, handler}
245+
return &TestCase{
246+
Desc: desc,
247+
Spec: spec,
248+
handler: handler,
249+
}
189250
}
190251

191252
var FlagDefault http2.Flags = 0x0
@@ -551,6 +612,33 @@ func pair(name, value string) hpack.HeaderField {
551612
return hpack.HeaderField{Name: name, Value: value}
552613
}
553614

615+
// printSummary prints out the test summary of all tests performed.
616+
func printSummary(groups []*TestGroup, numTestCases, numSkipped, numFailed int) {
617+
fmt.Printf("\x1b[35m")
618+
fmt.Println(`
619+
*******************************************************************************
620+
* *
621+
* Test Summary *
622+
* *
623+
*******************************************************************************`)
624+
fmt.Println("\x1b[0m")
625+
if numFailed > 0 {
626+
fmt.Println("Failed tests:")
627+
for _, tg := range groups {
628+
tg.PrintFailedTestCase(1)
629+
}
630+
}
631+
632+
numPassed := numTestCases - numSkipped - numFailed
633+
634+
fmt.Printf("\n%v tests, %v passed, %v skipped, %v failed\n", numTestCases, numPassed, numSkipped, numFailed)
635+
if numFailed == 0 {
636+
fmt.Printf("\x1b[32m")
637+
fmt.Printf("All tests passed\n")
638+
fmt.Printf("\x1b[0m")
639+
}
640+
}
641+
554642
func Run(ctx *Context) {
555643
groups := []*TestGroup{
556644
Http2ConnectionPrefaceTestGroup(),
@@ -572,7 +660,16 @@ func Run(ctx *Context) {
572660
ServerPushTestGroup(),
573661
}
574662

663+
numTestCases := 0
664+
numSkipped := 0
665+
numFailed := 0
666+
575667
for _, group := range groups {
576668
group.Run(ctx, 1)
669+
numTestCases += group.numTestCases
670+
numSkipped += group.numSkipped
671+
numFailed += group.numFailed
577672
}
673+
674+
printSummary(groups, numTestCases, numSkipped, numFailed)
578675
}

0 commit comments

Comments
 (0)