11package run
22
33import (
4+ "path/filepath"
45 "slices"
56 "time"
67
8+ "github.com/samber/lo"
9+
710 "github.com/gdt-dev/core/api"
811 "github.com/gdt-dev/core/testunit"
9- "github.com/samber/lo"
1012)
1113
1214// Run stores state of a test run when tests are executed with the `gdt` CLI
1315// tool.
1416type Run struct {
1517 // scenarioResults is a map, keyed by the Scenario path, of slices of
16- // TestUnitResult structs corresponding to the test specs in the scenario.
17- // There is guaranteed to be exactly the same number of TestUnitResults in
18+ // testunit.Result structs corresponding to the test specs in the scenario.
19+ // There is guaranteed to be exactly the same number of testunit.Results in
1820 // the slice as scenarios in the scenario.
19- scenarioResults map [string ][]TestUnitResult
21+ scenarioResults map [string ][]testunit. Result
2022}
2123
2224// OK returns true if all Scenarios in the Run had all successful test units.
2325func (r * Run ) OK () bool {
24- return lo .EveryBy (lo .Values (r .scenarioResults ), func (results []TestUnitResult ) bool {
25- return lo .EveryBy (results , func (tur TestUnitResult ) bool {
26- return tur .OK ()
27- })
28- })
26+ return lo .EveryBy (
27+ lo .Values (r .scenarioResults ),
28+ func (results []testunit.Result ) bool {
29+ return lo .EveryBy (results , func (tur testunit.Result ) bool {
30+ return tur .OK ()
31+ })
32+ },
33+ )
2934}
3035
3136// ScenarioPaths returns a sorted list of Scenario Paths.
@@ -35,9 +40,9 @@ func (r *Run) ScenarioPaths() []string {
3540 return paths
3641}
3742
38- // ScenarioResults returns the set of TestUnitResults for a Scenario with the
43+ // ScenarioResults returns the set of testunit.Results for a Scenario with the
3944// supplied path.
40- func (r * Run ) ScenarioResults (path string ) []TestUnitResult {
45+ func (r * Run ) ScenarioResults (path string ) []testunit. Result {
4146 return r .scenarioResults [path ]
4247}
4348
@@ -49,63 +54,52 @@ func (r *Run) StoreResult(
4954 res * api.Result ,
5055) {
5156 if _ , ok := r .scenarioResults [path ]; ! ok {
52- r .scenarioResults [path ] = []TestUnitResult {}
57+ r .scenarioResults [path ] = []testunit. Result {}
5358 }
5459 r .scenarioResults [path ] = append (
5560 r .scenarioResults [path ],
56- TestUnitResult {
57- index : index ,
58- name : tu .Name (),
59- elapsed : tu .Elapsed (),
60- skipped : tu .Skipped (),
61- failures : res .Failures (),
62- detail : tu .Detail (),
63- },
61+ testunit .NewResult (index , tu , res ),
6462 )
6563}
6664
67- // TestUnitResult stores a summary of the test execution of a single test unit.
68- type TestUnitResult struct {
69- // index is the 0-based index of the test unit within the test scenario.
70- index int
71- // name is the short name of the test unit
72- name string
73- // skipped is true if the test unit was skipped
74- skipped bool
75- // failures is the collection of assertion failures for the test spec that
76- // occurred during the run. this will NOT include RuntimeErrors.
77- failures [] error
78- // elapsed is the time take to execute the test unit
79- elapsed time. Duration
80- // detail is a buffer holding any log entries made during the run of the
81- // test spec.
82- detail string
83- }
65+ // XUnit returns the Run's scenario results as a slice of structs that can be
66+ // serialized to either XML (JUnit/XUnit-style) or JSON.
67+ func ( r * Run ) XUnit () []api. XUnitTestSuite {
68+ suites := []api. XUnitTestSuite {}
69+ paths := r . ScenarioPaths ()
70+ for _ , path := range paths {
71+ shortPath := filepath . Base ( path )
72+ suite := api. XUnitTestSuite {
73+ Name : shortPath ,
74+ Properties : []api. XUnitProperty {
75+ {
76+ Name : "path" ,
77+ Value : path ,
78+ },
79+ },
80+ Timestamp : time . Now (),
81+ }
8482
85- func (u TestUnitResult ) OK () bool {
86- return len (u .failures ) == 0
87- }
83+ var scenElapsed time.Duration
8884
89- func (u TestUnitResult ) Name () string {
90- return u .name
91- }
85+ unitResults := r .ScenarioResults (path )
9286
93- func (u TestUnitResult ) Index () int {
94- return u .index
95- }
96-
97- func (u TestUnitResult ) Failures () []error {
98- return u .failures
99- }
100-
101- func (u TestUnitResult ) Skipped () bool {
102- return u .skipped
103- }
104-
105- func (u TestUnitResult ) Detail () string {
106- return u .detail
107- }
87+ testcases := make ([]api.XUnitTestCase , len (unitResults ))
88+ tcFails := 0
10889
109- func (u TestUnitResult ) Elapsed () time.Duration {
110- return u .elapsed
90+ for x , res := range unitResults {
91+ tc := res .XUnit ()
92+ if res .Failed () {
93+ tcFails ++
94+ }
95+ scenElapsed += res .Elapsed ()
96+ testcases [x ] = tc
97+ }
98+ suite .Failures = tcFails
99+ suite .Tests = len (testcases )
100+ suite .Time = scenElapsed .String ()
101+ suite .TestCases = testcases
102+ suites = append (suites , suite )
103+ }
104+ return suites
111105}
0 commit comments