Skip to content

Commit da6551b

Browse files
committed
gtr,parser/gotest: Remove gtr.Benchmark and use gtr.Test for all tests
1 parent dc591b8 commit da6551b

File tree

5 files changed

+187
-159
lines changed

5 files changed

+187
-159
lines changed

gtr/gtr.go

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"time"
88
)
99

10-
// Result is the result of a test or benchmark.
10+
// Result is the result of a test.
1111
type Result int
1212

1313
const (
@@ -32,8 +32,7 @@ func (r Result) String() string {
3232
}
3333
}
3434

35-
// Report contains the build, test and/or benchmark results of a collection of
36-
// packages.
35+
// Report contains the build and test results of a collection of packages.
3736
type Report struct {
3837
Packages []Package
3938
}
@@ -54,7 +53,7 @@ func (r *Report) IsSuccessful() bool {
5453
return true
5554
}
5655

57-
// Package contains build, test and/or benchmark results for a single package.
56+
// Package contains build and test results for a single package.
5857
type Package struct {
5958
Name string
6059
Timestamp time.Time
@@ -63,8 +62,7 @@ type Package struct {
6362
Output []string
6463
Properties map[string]string
6564

66-
Tests []Test
67-
Benchmarks []Benchmark
65+
Tests []Test
6866

6967
BuildError Error
7068
RunError Error
@@ -88,25 +86,12 @@ type Test struct {
8886
Result Result
8987
Level int
9088
Output []string
89+
Data map[string]interface{}
9190
}
9291

93-
// Benchmark contains the results of a single benchmark.
94-
type Benchmark struct {
95-
ID int
96-
Name string
97-
Result Result
98-
Output []string
99-
Iterations int64
100-
NsPerOp float64
101-
MBPerSec float64
102-
BytesPerOp int64
103-
AllocsPerOp int64
104-
}
105-
106-
// ApproximateDuration returns the duration calculated by multiplying the
107-
// iterations and average time per iteration (NsPerOp).
108-
func (b Benchmark) ApproximateDuration() time.Duration {
109-
return time.Duration(float64(b.Iterations)*b.NsPerOp) * time.Nanosecond
92+
// NewTest creates a new Test with the given id and name.
93+
func NewTest(id int, name string) Test {
94+
return Test{ID: id, Name: name, Data: make(map[string]interface{})}
11095
}
11196

11297
// Error contains details of a build or runtime error.

parser/gotest/benchmark.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package gotest
2+
3+
import (
4+
"time"
5+
6+
"github.com/jstemmer/go-junit-report/v2/gtr"
7+
)
8+
9+
const (
10+
key = "gotest.benchmark"
11+
)
12+
13+
// Benchmark contains benchmark results and is intended to be used as extra
14+
// data in a gtr.Test.
15+
type Benchmark struct {
16+
Iterations int64
17+
NsPerOp float64
18+
MBPerSec float64
19+
BytesPerOp int64
20+
AllocsPerOp int64
21+
}
22+
23+
// ApproximateDuration returns the duration calculated by multiplying the
24+
// iterations and average time per iteration (NsPerOp).
25+
func (b Benchmark) ApproximateDuration() time.Duration {
26+
return time.Duration(float64(b.Iterations)*b.NsPerOp) * time.Nanosecond
27+
}
28+
29+
// GetBenchmarkData is a helper function that returns the benchmark contained
30+
// in the data field of the given gtr.Test t. If no (valid) benchmark is
31+
// present, ok will be set to false.
32+
func GetBenchmarkData(t gtr.Test) (b Benchmark, ok bool) {
33+
if t.Data != nil {
34+
if data, exists := t.Data[key]; exists {
35+
bm, ok := data.(Benchmark)
36+
return bm, ok
37+
}
38+
}
39+
return Benchmark{}, false
40+
}
41+
42+
// SetBenchmarkData is a helper function that writes the benchmark b to the
43+
// data field of the given gtr.Test t.
44+
func SetBenchmarkData(t *gtr.Test, b Benchmark) {
45+
if t.Data != nil {
46+
t.Data[key] = b
47+
}
48+
}

parser/gotest/gotest_test.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,14 @@ func TestReport(t *testing.T) {
258258
Output: []string{
259259
"\tHello", // TODO: strip tabs?
260260
},
261+
Data: map[string]interface{}{},
261262
},
262263
{
263264
ID: 2,
264265
Name: "TestSkip",
265266
Duration: 1 * time.Millisecond,
266267
Result: gtr.Skip,
268+
Data: map[string]interface{}{},
267269
},
268270
},
269271
},
@@ -280,24 +282,26 @@ func TestReport(t *testing.T) {
280282
Output: []string{
281283
"\tfile_test.go:10: error",
282284
},
285+
Data: map[string]interface{}{},
283286
},
284287
},
285288
},
286289
{
287290
Name: "package/name3",
288291
Duration: 1234 * time.Millisecond,
289292
Timestamp: testTimestamp,
290-
Benchmarks: []gtr.Benchmark{
293+
Tests: []gtr.Test{
291294
{
292-
ID: 4,
293-
Name: "BenchmarkOne",
294-
Result: gtr.Pass,
295-
NsPerOp: 100,
295+
ID: 4,
296+
Name: "BenchmarkOne",
297+
Result: gtr.Pass,
298+
Data: map[string]interface{}{key: Benchmark{NsPerOp: 100}},
296299
},
297300
{
298301
ID: 5,
299302
Name: "BenchmarkTwo",
300303
Result: gtr.Fail,
304+
Data: map[string]interface{}{},
301305
},
302306
},
303307
Output: []string{"goarch: amd64"},
@@ -360,20 +364,23 @@ func TestSubtestModes(t *testing.T) {
360364
Duration: 1 * time.Millisecond,
361365
Result: gtr.Pass,
362366
Output: []string{"TestParent before", "TestParent after"},
367+
Data: map[string]interface{}{},
363368
},
364369
{
365370
ID: 2,
366371
Name: "TestParent/Subtest#1",
367372
Duration: 2 * time.Millisecond,
368373
Result: gtr.Fail,
369374
Output: []string{"Subtest#1 output"},
375+
Data: map[string]interface{}{},
370376
},
371377
{
372378
ID: 3,
373379
Name: "TestParent/Subtest#2",
374380
Duration: 3 * time.Millisecond,
375381
Result: gtr.Pass,
376382
Output: []string{"Subtest#2 output"},
383+
Data: map[string]interface{}{},
377384
},
378385
},
379386
Output: []string{"output"},
@@ -397,13 +404,15 @@ func TestSubtestModes(t *testing.T) {
397404
Duration: 2 * time.Millisecond,
398405
Result: gtr.Fail,
399406
Output: []string{"Subtest#1 output"},
407+
Data: map[string]interface{}{},
400408
},
401409
{
402410
ID: 3,
403411
Name: "TestParent/Subtest#2",
404412
Duration: 3 * time.Millisecond,
405413
Result: gtr.Pass,
406414
Output: []string{"Subtest#2 output"},
415+
Data: map[string]interface{}{},
407416
},
408417
},
409418
Output: []string{"TestParent before", "TestParent after", "output"},

0 commit comments

Comments
 (0)