@@ -2,6 +2,11 @@ package reports
22
33import (
44 "bytes"
5+ "encoding/json"
6+ "fmt"
7+ "io/ioutil"
8+ "os"
9+ "path/filepath"
510 "strings"
611 "testing"
712)
@@ -109,3 +114,192 @@ func TestPrintTests(t *testing.T) {
109114 }
110115 }
111116}
117+
118+ // Helper function to write temporary JSON files for testing
119+ func writeTempJSONFile (t * testing.T , dir string , filename string , data interface {}) string {
120+ filePath := filepath .Join (dir , filename )
121+ fileData , err := json .Marshal (data )
122+ if err != nil {
123+ t .Fatalf ("Failed to marshal JSON: %v" , err )
124+ }
125+ if err := ioutil .WriteFile (filePath , fileData , 0644 ); err != nil {
126+ t .Fatalf ("Failed to write JSON file: %v" , err )
127+ }
128+ return filePath
129+ }
130+
131+ func TestAggregateTestResults (t * testing.T ) {
132+ // Create a temporary directory for test JSON files
133+ tempDir , err := os .MkdirTemp ("" , "aggregatetestresults" )
134+ if err != nil {
135+ t .Fatalf ("Failed to create temp directory: %v" , err )
136+ }
137+ defer os .RemoveAll (tempDir )
138+
139+ // Test cases
140+ testCases := []struct {
141+ description string
142+ inputFiles []interface {}
143+ expectedOutput []TestResult
144+ }{
145+ {
146+ description : "Unique test results, no aggregation" ,
147+ inputFiles : []interface {}{
148+ []TestResult {
149+ {
150+ TestName : "TestA" ,
151+ TestPackage : "pkgA" ,
152+ PassRatio : 1 ,
153+ PassRatioPercentage : "100%" ,
154+ Skipped : false ,
155+ Runs : 2 ,
156+ Durations : []float64 {0.01 , 0.02 },
157+ },
158+ },
159+ []TestResult {
160+ {
161+ TestName : "TestB" ,
162+ TestPackage : "pkgB" ,
163+ PassRatio : 0.5 ,
164+ PassRatioPercentage : "50%" ,
165+ Skipped : false ,
166+ Runs : 4 ,
167+ Durations : []float64 {0.05 , 0.05 , 0.05 , 0.05 },
168+ },
169+ },
170+ },
171+ expectedOutput : []TestResult {
172+ {
173+ TestName : "TestA" ,
174+ TestPackage : "pkgA" ,
175+ PassRatio : 1 ,
176+ PassRatioPercentage : "100%" ,
177+ Skipped : false ,
178+ Runs : 2 ,
179+ Durations : []float64 {0.01 , 0.02 },
180+ },
181+ {
182+ TestName : "TestB" ,
183+ TestPackage : "pkgB" ,
184+ PassRatio : 0.5 ,
185+ PassRatioPercentage : "50%" ,
186+ Skipped : false ,
187+ Runs : 4 ,
188+ Durations : []float64 {0.05 , 0.05 , 0.05 , 0.05 },
189+ },
190+ },
191+ },
192+ {
193+ description : "Duplicate test results, aggregation of PassRatio and Durations" ,
194+ inputFiles : []interface {}{
195+ []TestResult {
196+ {
197+ TestName : "TestC" ,
198+ TestPackage : "pkgC" ,
199+ PassRatio : 1 ,
200+ PassRatioPercentage : "100%" ,
201+ Skipped : false ,
202+ Runs : 2 ,
203+ Durations : []float64 {0.1 , 0.1 },
204+ },
205+ },
206+ []TestResult {
207+ {
208+ TestName : "TestC" ,
209+ TestPackage : "pkgC" ,
210+ PassRatio : 0.5 ,
211+ PassRatioPercentage : "50%" ,
212+ Skipped : false ,
213+ Runs : 2 ,
214+ Durations : []float64 {0.2 , 0.2 },
215+ },
216+ },
217+ },
218+ expectedOutput : []TestResult {
219+ {
220+ TestName : "TestC" ,
221+ TestPackage : "pkgC" ,
222+ PassRatio : 0.75 , // Calculated as (2*1 + 2*0.5) / 4
223+ PassRatioPercentage : "75%" ,
224+ Skipped : false ,
225+ Runs : 4 ,
226+ Durations : []float64 {0.1 , 0.1 , 0.2 , 0.2 },
227+ },
228+ },
229+ },
230+ {
231+ description : "All Skipped test results" ,
232+ inputFiles : []interface {}{
233+ []TestResult {
234+ {
235+ TestName : "TestD" ,
236+ TestPackage : "pkgD" ,
237+ PassRatio : 1 ,
238+ PassRatioPercentage : "100%" ,
239+ Skipped : true ,
240+ Runs : 3 ,
241+ Durations : []float64 {0.1 , 0.2 , 0.1 },
242+ },
243+ },
244+ []TestResult {
245+ {
246+ TestName : "TestD" ,
247+ TestPackage : "pkgD" ,
248+ PassRatio : 1 ,
249+ PassRatioPercentage : "100%" ,
250+ Skipped : true ,
251+ Runs : 2 ,
252+ Durations : []float64 {0.15 , 0.15 },
253+ },
254+ },
255+ },
256+ expectedOutput : []TestResult {
257+ {
258+ TestName : "TestD" ,
259+ TestPackage : "pkgD" ,
260+ PassRatio : 1 ,
261+ PassRatioPercentage : "100%" ,
262+ Skipped : true , // Should remain true as all runs are skipped
263+ Runs : 5 ,
264+ Durations : []float64 {0.1 , 0.2 , 0.1 , 0.15 , 0.15 },
265+ },
266+ },
267+ },
268+ }
269+
270+ for _ , tc := range testCases {
271+ t .Run (tc .description , func (t * testing.T ) {
272+ // Write input files to the temporary directory
273+ for i , inputData := range tc .inputFiles {
274+ writeTempJSONFile (t , tempDir , fmt .Sprintf ("input%d.json" , i ), inputData )
275+ }
276+
277+ // Run AggregateTestResults
278+ result , err := AggregateTestResults (tempDir )
279+ if err != nil {
280+ t .Fatalf ("AggregateTestResults failed: %v" , err )
281+ }
282+
283+ // Compare the result with the expected output
284+ if len (result ) != len (tc .expectedOutput ) {
285+ t .Fatalf ("Expected %d results, got %d" , len (tc .expectedOutput ), len (result ))
286+ }
287+
288+ for i , expected := range tc .expectedOutput {
289+ got := result [i ]
290+ if got .TestName != expected .TestName || got .TestPackage != expected .TestPackage || got .Runs != expected .Runs || got .Skipped != expected .Skipped {
291+ t .Errorf ("Result %d - expected %+v, got %+v" , i , expected , got )
292+ }
293+ if got .PassRatio != expected .PassRatio {
294+ t .Errorf ("Result %d - expected PassRatio %f, got %f" , i , expected .PassRatio , got .PassRatio )
295+ }
296+ if got .PassRatioPercentage != expected .PassRatioPercentage {
297+ t .Errorf ("Result %d - expected PassRatioPercentage %s, got %s" , i , expected .PassRatioPercentage , got .PassRatioPercentage )
298+ }
299+ if len (got .Durations ) != len (expected .Durations ) {
300+ t .Errorf ("Result %d - expected %d durations, got %d" , i , len (expected .Durations ), len (got .Durations ))
301+ }
302+ }
303+ })
304+ }
305+ }
0 commit comments