github.com/saucelabs/saucectl@v0.175.1/internal/report/junit/junit_test.go (about) 1 package junit 2 3 import ( 4 "io" 5 "os" 6 "reflect" 7 "testing" 8 "time" 9 10 "github.com/saucelabs/saucectl/internal/job" 11 "github.com/saucelabs/saucectl/internal/report" 12 ) 13 14 func TestReporter_Render(t *testing.T) { 15 type fields struct { 16 TestResults []report.TestResult 17 } 18 tests := []struct { 19 name string 20 fields fields 21 want string 22 }{ 23 { 24 name: "all pass", 25 fields: fields{ 26 TestResults: []report.TestResult{ 27 { 28 Name: "Firefox", 29 Duration: 34479 * time.Millisecond, 30 Status: job.StatePassed, 31 Browser: "Firefox", 32 Platform: "Windows 10", 33 URL: "https://app.saucelabs.com/tests/1234", 34 }, 35 { 36 Name: "Chrome", 37 Duration: 5123 * time.Millisecond, 38 Status: job.StatePassed, 39 Browser: "Chrome", 40 Platform: "Windows 10", 41 }, 42 }, 43 }, 44 want: `<testsuites> 45 <testsuite name="Firefox" tests="0" time="34"> 46 <properties> 47 <property name="url" value="https://app.saucelabs.com/tests/1234"></property> 48 <property name="browser" value="Firefox"></property> 49 <property name="platform" value="Windows 10"></property> 50 </properties> 51 </testsuite> 52 <testsuite name="Chrome" tests="0" time="5"> 53 <properties> 54 <property name="browser" value="Chrome"></property> 55 <property name="platform" value="Windows 10"></property> 56 </properties> 57 </testsuite> 58 </testsuites> 59 `, 60 }, 61 { 62 name: "with failure", 63 fields: fields{ 64 TestResults: []report.TestResult{ 65 { 66 Name: "Firefox", 67 Duration: 34479 * time.Millisecond, 68 Status: job.StatePassed, 69 Browser: "Firefox", 70 Platform: "Windows 10", 71 }, 72 { 73 Name: "Chrome", 74 Duration: 171452 * time.Millisecond, 75 Status: job.StateFailed, 76 Browser: "Chrome", 77 Platform: "Windows 10", 78 }, 79 }, 80 }, 81 want: `<testsuites> 82 <testsuite name="Firefox" tests="0" time="34"> 83 <properties> 84 <property name="browser" value="Firefox"></property> 85 <property name="platform" value="Windows 10"></property> 86 </properties> 87 </testsuite> 88 <testsuite name="Chrome" tests="0" time="171"> 89 <properties> 90 <property name="browser" value="Chrome"></property> 91 <property name="platform" value="Windows 10"></property> 92 </properties> 93 </testsuite> 94 </testsuites> 95 `, 96 }, 97 } 98 for _, tt := range tests { 99 t.Run(tt.name, func(t *testing.T) { 100 f, err := os.CreateTemp("", "saucectl-report.xml") 101 if err != nil { 102 t.Fatalf("Failed to create temp file %s", err) 103 } 104 //f.Close() 105 defer os.Remove(f.Name()) 106 107 r := &Reporter{ 108 TestResults: tt.fields.TestResults, 109 Filename: f.Name(), 110 } 111 r.Render() 112 113 b, err := io.ReadAll(f) 114 if err != nil { 115 t.Fatalf("Failed to read and verify output file %s due to %s", f.Name(), err) 116 } 117 118 bstr := string(b) 119 if !reflect.DeepEqual(bstr, tt.want) { 120 t.Errorf("Render() got = \n%s, want = \n%s", bstr, tt.want) 121 } 122 }) 123 } 124 }