github.com/saucelabs/saucectl@v0.175.1/internal/report/spotlight/spotlight_test.go (about)

     1  package spotlight
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/saucelabs/saucectl/internal/job"
    10  	"github.com/saucelabs/saucectl/internal/junit"
    11  	"github.com/saucelabs/saucectl/internal/report"
    12  )
    13  
    14  func ExampleReporter_Render() {
    15  	startTime := time.Now()
    16  	restResults := []report.TestResult{
    17  		{
    18  			Name:      "Chrome",
    19  			Duration:  171452 * time.Millisecond,
    20  			StartTime: startTime,
    21  			EndTime:   startTime.Add(171452 * time.Millisecond),
    22  			Status:    job.StateFailed,
    23  			Browser:   "Chrome",
    24  			Platform:  "Windows 10",
    25  			URL:       "https://app.saucelabs.com/tests/1234567890abcdef",
    26  			Attempts: []report.Attempt{
    27  				{
    28  					Status: job.StateFailed,
    29  					TestSuites: junit.TestSuites{
    30  						TestSuites: []junit.TestSuite{
    31  							{
    32  								Name: "",
    33  								TestCases: []junit.TestCase{
    34  									{
    35  										Name:      "TestCase1",
    36  										ClassName: "com.saucelabs.examples.SauceTest",
    37  										Error: &junit.Error{
    38  											Message: "Whoops!",
    39  											Type:    "AssertionError",
    40  										},
    41  									},
    42  								},
    43  							},
    44  						},
    45  					},
    46  				},
    47  			},
    48  		},
    49  	}
    50  
    51  	r := Reporter{
    52  		Dst: os.Stdout,
    53  	}
    54  
    55  	for _, tr := range restResults {
    56  		r.Add(tr)
    57  	}
    58  
    59  	r.Render()
    60  	// Output:
    61  	//Spotlight:
    62  	//
    63  	//  ✖ Chrome
    64  	//    ● URL: https://app.saucelabs.com/tests/1234567890abcdef
    65  	//    ● Failed Tests: (showing max. 5)
    66  	//      ✖ com.saucelabs.examples.SauceTest › TestCase1
    67  }
    68  
    69  func TestReporter_Add(t *testing.T) {
    70  	type fields struct {
    71  		TestResults []report.TestResult
    72  		Dst         io.Writer
    73  	}
    74  	type args struct {
    75  		t report.TestResult
    76  	}
    77  	tests := []struct {
    78  		name   string
    79  		fields fields
    80  		args   args
    81  		want   int
    82  	}{
    83  		{
    84  			name: "skip passed tests",
    85  			args: args{
    86  				t: report.TestResult{
    87  					Status: job.StatePassed,
    88  				},
    89  			},
    90  			want: 0,
    91  		},
    92  		{
    93  			name: "skipped in-progress tests",
    94  			args: args{
    95  				t: report.TestResult{
    96  					Status: job.StateInProgress,
    97  				},
    98  			},
    99  			want: 0,
   100  		},
   101  		{
   102  			name: "include failed tests",
   103  			args: args{
   104  				t: report.TestResult{
   105  					Status: job.StateFailed,
   106  				},
   107  			},
   108  			want: 1,
   109  		},
   110  		{
   111  			name: "include errored tests",
   112  			args: args{
   113  				t: report.TestResult{
   114  					Status: job.StateError,
   115  				},
   116  			},
   117  			want: 1,
   118  		},
   119  		{
   120  			name: "include timed out tests",
   121  			args: args{
   122  				t: report.TestResult{
   123  					TimedOut: true,
   124  				},
   125  			},
   126  			want: 1,
   127  		},
   128  	}
   129  	for _, tt := range tests {
   130  		t.Run(tt.name, func(t *testing.T) {
   131  			r := &Reporter{}
   132  			r.Add(tt.args.t)
   133  			if added := len(r.TestResults); added != tt.want {
   134  				t.Errorf("Reporter.Add() added %d results, want %d", added, tt.want)
   135  			}
   136  		})
   137  	}
   138  }