parser/gotest: Set unique Test, Benchmark and Error ID's

This will be used later to collect and store output for the right tests.
This commit is contained in:
Joël Stemmer 2022-06-07 00:47:31 +01:00
parent 5d691c7896
commit 2f7bf7c3bc
3 changed files with 32 additions and 18 deletions

View File

@ -251,6 +251,7 @@ func TestReport(t *testing.T) {
Timestamp: testTimestamp, Timestamp: testTimestamp,
Tests: []gtr.Test{ Tests: []gtr.Test{
{ {
ID: 1,
Name: "TestOne", Name: "TestOne",
Duration: 1 * time.Millisecond, Duration: 1 * time.Millisecond,
Result: gtr.Pass, Result: gtr.Pass,
@ -259,6 +260,7 @@ func TestReport(t *testing.T) {
}, },
}, },
{ {
ID: 2,
Name: "TestSkip", Name: "TestSkip",
Duration: 1 * time.Millisecond, Duration: 1 * time.Millisecond,
Result: gtr.Skip, Result: gtr.Skip,
@ -271,6 +273,7 @@ func TestReport(t *testing.T) {
Timestamp: testTimestamp, Timestamp: testTimestamp,
Tests: []gtr.Test{ Tests: []gtr.Test{
{ {
ID: 3,
Name: "TestOne", Name: "TestOne",
Duration: 1 * time.Millisecond, Duration: 1 * time.Millisecond,
Result: gtr.Fail, Result: gtr.Fail,
@ -286,11 +289,13 @@ func TestReport(t *testing.T) {
Timestamp: testTimestamp, Timestamp: testTimestamp,
Benchmarks: []gtr.Benchmark{ Benchmarks: []gtr.Benchmark{
{ {
ID: 4,
Name: "BenchmarkOne", Name: "BenchmarkOne",
Result: gtr.Pass, Result: gtr.Pass,
NsPerOp: 100, NsPerOp: 100,
}, },
{ {
ID: 5,
Name: "BenchmarkTwo", Name: "BenchmarkTwo",
Result: gtr.Fail, Result: gtr.Fail,
}, },
@ -301,6 +306,7 @@ func TestReport(t *testing.T) {
Name: "package/failing1", Name: "package/failing1",
Timestamp: testTimestamp, Timestamp: testTimestamp,
BuildError: gtr.Error{ BuildError: gtr.Error{
ID: 6,
Name: "package/failing1", Name: "package/failing1",
Cause: "[build failed]", Cause: "[build failed]",
Output: []string{"error message"}, Output: []string{"error message"},
@ -346,18 +352,21 @@ func TestSubtestModes(t *testing.T) {
Timestamp: testTimestamp, Timestamp: testTimestamp,
Tests: []gtr.Test{ Tests: []gtr.Test{
{ {
ID: 1,
Name: "TestParent", Name: "TestParent",
Duration: 1 * time.Millisecond, Duration: 1 * time.Millisecond,
Result: gtr.Pass, Result: gtr.Pass,
Output: []string{"TestParent output"}, Output: []string{"TestParent output"},
}, },
{ {
ID: 2,
Name: "TestParent/Subtest#1", Name: "TestParent/Subtest#1",
Duration: 2 * time.Millisecond, Duration: 2 * time.Millisecond,
Result: gtr.Fail, Result: gtr.Fail,
Output: []string{"Subtest#1 output"}, Output: []string{"Subtest#1 output"},
}, },
{ {
ID: 3,
Name: "TestParent/Subtest#2", Name: "TestParent/Subtest#2",
Duration: 3 * time.Millisecond, Duration: 3 * time.Millisecond,
Result: gtr.Pass, Result: gtr.Pass,
@ -379,12 +388,14 @@ func TestSubtestModes(t *testing.T) {
Timestamp: testTimestamp, Timestamp: testTimestamp,
Tests: []gtr.Test{ Tests: []gtr.Test{
{ {
ID: 2,
Name: "TestParent/Subtest#1", Name: "TestParent/Subtest#1",
Duration: 2 * time.Millisecond, Duration: 2 * time.Millisecond,
Result: gtr.Fail, Result: gtr.Fail,
Output: []string{"Subtest#1 output"}, Output: []string{"Subtest#1 output"},
}, },
{ {
ID: 3,
Name: "TestParent/Subtest#2", Name: "TestParent/Subtest#2",
Duration: 3 * time.Millisecond, Duration: 3 * time.Millisecond,
Result: gtr.Pass, Result: gtr.Pass,

View File

@ -78,7 +78,8 @@ func (b *reportBuilder) CreateTest(name string) {
if parentID, ok := b.findTestParentID(name); ok { if parentID, ok := b.findTestParentID(name); ok {
b.parentIDs[parentID] = struct{}{} b.parentIDs[parentID] = struct{}{}
} }
b.tests[b.newID()] = gtr.Test{Name: name} id := b.newID()
b.tests[id] = gtr.Test{ID: id, Name: name}
} }
// PauseTest marks the active context as no longer active. Any results or // PauseTest marks the active context as no longer active. Any results or
@ -127,9 +128,8 @@ func (b *reportBuilder) End() {
// most recently created benchmark will be updated. If no benchmark exists with // most recently created benchmark will be updated. If no benchmark exists with
// this name, a new benchmark is created. // this name, a new benchmark is created.
func (b *reportBuilder) CreateBenchmark(name string) { func (b *reportBuilder) CreateBenchmark(name string) {
b.benchmarks[b.newID()] = gtr.Benchmark{ id := b.newID()
Name: name, b.benchmarks[id] = gtr.Benchmark{ID: id, Name: name}
}
} }
// BenchmarkResult updates an existing or adds a new benchmark with the given // BenchmarkResult updates an existing or adds a new benchmark with the given
@ -144,6 +144,7 @@ func (b *reportBuilder) BenchmarkResult(name string, iterations int64, nsPerOp,
} }
b.benchmarks[id] = gtr.Benchmark{ b.benchmarks[id] = gtr.Benchmark{
ID: id,
Name: name, Name: name,
Result: gtr.Pass, Result: gtr.Pass,
Iterations: iterations, Iterations: iterations,
@ -173,7 +174,8 @@ func (b *reportBuilder) EndBenchmark(name, result string) {
// CreateBuildError creates a new build error and marks it as active. // CreateBuildError creates a new build error and marks it as active.
func (b *reportBuilder) CreateBuildError(packageName string) { func (b *reportBuilder) CreateBuildError(packageName string) {
b.buildErrors[b.newID()] = gtr.Error{Name: packageName} id := b.newID()
b.buildErrors[id] = gtr.Error{ID: id, Name: packageName}
} }
// CreatePackage adds a new package with the given name to the Report. This // CreatePackage adds a new package with the given name to the Report. This
@ -197,6 +199,7 @@ func (b *reportBuilder) CreatePackage(name, result string, duration time.Duratio
if len(b.tests) > 0 || len(b.benchmarks) > 0 { if len(b.tests) > 0 || len(b.benchmarks) > 0 {
panic("unexpected tests and/or benchmarks found in build error package") panic("unexpected tests and/or benchmarks found in build error package")
} }
buildErr.ID = id
buildErr.Duration = duration buildErr.Duration = duration
buildErr.Cause = data buildErr.Cause = data
pkg.BuildError = buildErr pkg.BuildError = buildErr
@ -388,7 +391,7 @@ func groupBenchmarksByName(benchmarks []gtr.Benchmark) []gtr.Benchmark {
byName := make(map[string][]gtr.Benchmark) byName := make(map[string][]gtr.Benchmark)
for _, bm := range benchmarks { for _, bm := range benchmarks {
if _, ok := byName[bm.Name]; !ok { if _, ok := byName[bm.Name]; !ok {
grouped = append(grouped, gtr.Benchmark{Name: bm.Name}) grouped = append(grouped, gtr.Benchmark{ID: bm.ID, Name: bm.Name})
} }
byName[bm.Name] = append(byName[bm.Name], bm) byName[bm.Name] = append(byName[bm.Name], bm)
} }

View File

@ -15,29 +15,29 @@ func TestGroupBenchmarksByName(t *testing.T) {
}{ }{
{nil, nil}, {nil, nil},
{ {
[]gtr.Benchmark{{Name: "BenchmarkFailed", Result: gtr.Fail}}, []gtr.Benchmark{{ID: 1, Name: "BenchmarkFailed", Result: gtr.Fail}},
[]gtr.Benchmark{{Name: "BenchmarkFailed", Result: gtr.Fail}}, []gtr.Benchmark{{ID: 1, Name: "BenchmarkFailed", Result: gtr.Fail}},
}, },
{ {
[]gtr.Benchmark{ []gtr.Benchmark{
{Name: "BenchmarkOne", Result: gtr.Pass, NsPerOp: 10, MBPerSec: 400, BytesPerOp: 1, AllocsPerOp: 2}, {ID: 1, Name: "BenchmarkOne", Result: gtr.Pass, NsPerOp: 10, MBPerSec: 400, BytesPerOp: 1, AllocsPerOp: 2},
{Name: "BenchmarkOne", Result: gtr.Pass, NsPerOp: 20, MBPerSec: 300, BytesPerOp: 1, AllocsPerOp: 4}, {ID: 2, Name: "BenchmarkOne", Result: gtr.Pass, NsPerOp: 20, MBPerSec: 300, BytesPerOp: 1, AllocsPerOp: 4},
{Name: "BenchmarkOne", Result: gtr.Pass, NsPerOp: 30, MBPerSec: 200, BytesPerOp: 1, AllocsPerOp: 8}, {ID: 3, Name: "BenchmarkOne", Result: gtr.Pass, NsPerOp: 30, MBPerSec: 200, BytesPerOp: 1, AllocsPerOp: 8},
{Name: "BenchmarkOne", Result: gtr.Pass, NsPerOp: 40, MBPerSec: 100, BytesPerOp: 5, AllocsPerOp: 2}, {ID: 4, Name: "BenchmarkOne", Result: gtr.Pass, NsPerOp: 40, MBPerSec: 100, BytesPerOp: 5, AllocsPerOp: 2},
}, },
[]gtr.Benchmark{ []gtr.Benchmark{
{Name: "BenchmarkOne", Result: gtr.Pass, NsPerOp: 25, MBPerSec: 250, BytesPerOp: 2, AllocsPerOp: 4}, {ID: 1, Name: "BenchmarkOne", Result: gtr.Pass, NsPerOp: 25, MBPerSec: 250, BytesPerOp: 2, AllocsPerOp: 4},
}, },
}, },
{ {
[]gtr.Benchmark{ []gtr.Benchmark{
{Name: "BenchmarkMixed", Result: gtr.Unknown}, {ID: 1, Name: "BenchmarkMixed", Result: gtr.Unknown},
{Name: "BenchmarkMixed", Result: gtr.Pass, NsPerOp: 10, MBPerSec: 400, BytesPerOp: 1, AllocsPerOp: 2}, {ID: 2, Name: "BenchmarkMixed", Result: gtr.Pass, NsPerOp: 10, MBPerSec: 400, BytesPerOp: 1, AllocsPerOp: 2},
{Name: "BenchmarkMixed", Result: gtr.Pass, NsPerOp: 40, MBPerSec: 100, BytesPerOp: 3, AllocsPerOp: 4}, {ID: 3, Name: "BenchmarkMixed", Result: gtr.Pass, NsPerOp: 40, MBPerSec: 100, BytesPerOp: 3, AllocsPerOp: 4},
{Name: "BenchmarkMixed", Result: gtr.Fail}, {ID: 4, Name: "BenchmarkMixed", Result: gtr.Fail},
}, },
[]gtr.Benchmark{ []gtr.Benchmark{
{Name: "BenchmarkMixed", Result: gtr.Fail, NsPerOp: 25, MBPerSec: 250, BytesPerOp: 2, AllocsPerOp: 3}, {ID: 1, Name: "BenchmarkMixed", Result: gtr.Fail, NsPerOp: 25, MBPerSec: 250, BytesPerOp: 2, AllocsPerOp: 3},
}, },
}, },
} }