44 "bytes"
55 "encoding/json"
66 "fmt"
7- "io/ioutil"
87 "os"
98 "path/filepath"
9+ "sort"
1010 "strings"
1111 "testing"
1212)
@@ -115,14 +115,24 @@ func TestPrintTests(t *testing.T) {
115115 }
116116}
117117
118- // Helper function to write temporary JSON files for testing
118+ // Sorts TestResult slice by TestName and TestPackage for consistent comparison
119+ func sortTestResults (results []TestResult ) {
120+ sort .Slice (results , func (i , j int ) bool {
121+ if results [i ].TestName == results [j ].TestName {
122+ return results [i ].TestPackage < results [j ].TestPackage
123+ }
124+ return results [i ].TestName < results [j ].TestName
125+ })
126+ }
127+
128+ // Helper function to write a JSON file for testing
119129func writeTempJSONFile (t * testing.T , dir string , filename string , data interface {}) string {
120130 filePath := filepath .Join (dir , filename )
121131 fileData , err := json .Marshal (data )
122132 if err != nil {
123133 t .Fatalf ("Failed to marshal JSON: %v" , err )
124134 }
125- if err := ioutil .WriteFile (filePath , fileData , 0644 ); err != nil {
135+ if err := os .WriteFile (filePath , fileData , 0644 ); err != nil {
126136 t .Fatalf ("Failed to write JSON file: %v" , err )
127137 }
128138 return filePath
@@ -143,7 +153,7 @@ func TestAggregateTestResults(t *testing.T) {
143153 expectedOutput []TestResult
144154 }{
145155 {
146- description : "Unique test results, no aggregation" ,
156+ description : "Unique test results without aggregation" ,
147157 inputFiles : []interface {}{
148158 []TestResult {
149159 {
@@ -154,6 +164,7 @@ func TestAggregateTestResults(t *testing.T) {
154164 Skipped : false ,
155165 Runs : 2 ,
156166 Durations : []float64 {0.01 , 0.02 },
167+ Outputs : []string {"Output1" , "Output2" },
157168 },
158169 },
159170 []TestResult {
@@ -165,6 +176,7 @@ func TestAggregateTestResults(t *testing.T) {
165176 Skipped : false ,
166177 Runs : 4 ,
167178 Durations : []float64 {0.05 , 0.05 , 0.05 , 0.05 },
179+ Outputs : []string {"Output3" , "Output4" , "Output5" , "Output6" },
168180 },
169181 },
170182 },
@@ -177,6 +189,7 @@ func TestAggregateTestResults(t *testing.T) {
177189 Skipped : false ,
178190 Runs : 2 ,
179191 Durations : []float64 {0.01 , 0.02 },
192+ Outputs : []string {"Output1" , "Output2" },
180193 },
181194 {
182195 TestName : "TestB" ,
@@ -186,11 +199,12 @@ func TestAggregateTestResults(t *testing.T) {
186199 Skipped : false ,
187200 Runs : 4 ,
188201 Durations : []float64 {0.05 , 0.05 , 0.05 , 0.05 },
202+ Outputs : []string {"Output3" , "Output4" , "Output5" , "Output6" },
189203 },
190204 },
191205 },
192206 {
193- description : "Duplicate test results, aggregation of PassRatio and Durations " ,
207+ description : "Duplicate test results with aggregation " ,
194208 inputFiles : []interface {}{
195209 []TestResult {
196210 {
@@ -201,6 +215,7 @@ func TestAggregateTestResults(t *testing.T) {
201215 Skipped : false ,
202216 Runs : 2 ,
203217 Durations : []float64 {0.1 , 0.1 },
218+ Outputs : []string {"Output7" , "Output8" },
204219 },
205220 },
206221 []TestResult {
@@ -212,6 +227,7 @@ func TestAggregateTestResults(t *testing.T) {
212227 Skipped : false ,
213228 Runs : 2 ,
214229 Durations : []float64 {0.2 , 0.2 },
230+ Outputs : []string {"Output9" , "Output10" },
215231 },
216232 },
217233 },
@@ -224,6 +240,7 @@ func TestAggregateTestResults(t *testing.T) {
224240 Skipped : false ,
225241 Runs : 4 ,
226242 Durations : []float64 {0.1 , 0.1 , 0.2 , 0.2 },
243+ Outputs : []string {"Output7" , "Output8" , "Output9" , "Output10" },
227244 },
228245 },
229246 },
@@ -239,6 +256,7 @@ func TestAggregateTestResults(t *testing.T) {
239256 Skipped : true ,
240257 Runs : 3 ,
241258 Durations : []float64 {0.1 , 0.2 , 0.1 },
259+ Outputs : []string {"Output11" , "Output12" , "Output13" },
242260 },
243261 },
244262 []TestResult {
@@ -250,6 +268,7 @@ func TestAggregateTestResults(t *testing.T) {
250268 Skipped : true ,
251269 Runs : 2 ,
252270 Durations : []float64 {0.15 , 0.15 },
271+ Outputs : []string {"Output14" , "Output15" },
253272 },
254273 },
255274 },
@@ -262,6 +281,7 @@ func TestAggregateTestResults(t *testing.T) {
262281 Skipped : true , // Should remain true as all runs are skipped
263282 Runs : 5 ,
264283 Durations : []float64 {0.1 , 0.2 , 0.1 , 0.15 , 0.15 },
284+ Outputs : []string {"Output11" , "Output12" , "Output13" , "Output14" , "Output15" },
265285 },
266286 },
267287 },
@@ -280,6 +300,10 @@ func TestAggregateTestResults(t *testing.T) {
280300 t .Fatalf ("AggregateTestResults failed: %v" , err )
281301 }
282302
303+ // Sort both result and expectedOutput for consistent comparison
304+ sortTestResults (result )
305+ sortTestResults (tc .expectedOutput )
306+
283307 // Compare the result with the expected output
284308 if len (result ) != len (tc .expectedOutput ) {
285309 t .Fatalf ("Expected %d results, got %d" , len (tc .expectedOutput ), len (result ))
@@ -299,6 +323,9 @@ func TestAggregateTestResults(t *testing.T) {
299323 if len (got .Durations ) != len (expected .Durations ) {
300324 t .Errorf ("Result %d - expected %d durations, got %d" , i , len (expected .Durations ), len (got .Durations ))
301325 }
326+ if len (got .Outputs ) != len (expected .Outputs ) {
327+ t .Errorf ("Result %d - expected %d outputs, got %d" , i , len (expected .Outputs ), len (got .Outputs ))
328+ }
302329 }
303330 })
304331 }
0 commit comments