How to use CountOfFlakedSpecs method of types Package

Best Ginkgo code snippet using types.CountOfFlakedSpecs

Run Ginkgo automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

types_test.go

Source: types_test.go Github

copy
1package types_test
2
3import (
4	"encoding/json"
5	"time"
6
7	. "github.com/onsi/ginkgo/v2"
8	. "github.com/onsi/gomega"
9
10	"github.com/onsi/ginkgo/v2/types"
11)
12
13var _ = Describe("Types", func() {
14	Describe("Report", func() {
15		Describe("Add", func() {
16			It("concatenates spec reports, combines success, and computes a new RunTime", func() {
17				t := time.Now()
18				reportA := types.Report{
19					SuitePath:                  "foo",
20					SuiteSucceeded:             true,
21					StartTime:                  t.Add(-time.Minute),
22					EndTime:                    t.Add(2 * time.Minute),
23					SpecialSuiteFailureReasons: []string{"blame jim", "blame alice"},
24					SpecReports: types.SpecReports{
25						types.SpecReport{NumAttempts: 3},
26						types.SpecReport{NumAttempts: 4},
27					},
28				}
29
30				reportB := types.Report{
31					SuitePath:                  "bar",
32					SuiteSucceeded:             false,
33					StartTime:                  t.Add(-2 * time.Minute),
34					EndTime:                    t.Add(time.Minute),
35					SpecialSuiteFailureReasons: []string{"blame bob", "blame jim"},
36					SpecReports: types.SpecReports{
37						types.SpecReport{NumAttempts: 5},
38						types.SpecReport{NumAttempts: 6},
39					},
40				}
41
42				composite := reportA.Add(reportB)
43				Ω(composite).Should(Equal(types.Report{
44					SuitePath:                  "foo",
45					SuiteSucceeded:             false,
46					StartTime:                  t.Add(-2 * time.Minute),
47					EndTime:                    t.Add(2 * time.Minute),
48					RunTime:                    4 * time.Minute,
49					SpecialSuiteFailureReasons: []string{"blame jim", "blame alice", "blame bob"},
50					SpecReports: types.SpecReports{
51						types.SpecReport{NumAttempts: 3},
52						types.SpecReport{NumAttempts: 4},
53						types.SpecReport{NumAttempts: 5},
54						types.SpecReport{NumAttempts: 6},
55					},
56				}))
57
58			})
59		})
60	})
61
62	Describe("NodeType", func() {
63		Describe("Is", func() {
64			It("returns true when the NodeType is in the passed-in list", func() {
65				Ω(types.NodeTypeContainer.Is(types.NodeTypeIt | types.NodeTypeContainer)).Should(BeTrue())
66			})
67
68			It("returns false when the NodeType is not in the passed-in list", func() {
69				Ω(types.NodeTypeContainer.Is(types.NodeTypeIt | types.NodeTypeBeforeEach)).Should(BeFalse())
70			})
71		})
72
73		DescribeTable("Representation and Encoding", func(nodeType types.NodeType, expectedString string) {
74			Ω(nodeType.String()).Should(Equal(expectedString))
75
76			marshalled, err := json.Marshal(nodeType)
77			Ω(err).ShouldNot(HaveOccurred())
78			var unmarshalled types.NodeType
79			json.Unmarshal(marshalled, &unmarshalled)
80			Ω(unmarshalled).Should(Equal(nodeType))
81		},
82			func(nodeType types.NodeType, expectedString string) string {
83				return expectedString
84			},
85			Entry(nil, types.NodeTypeContainer, "Container"),
86			Entry(nil, types.NodeTypeIt, "It"),
87			Entry(nil, types.NodeTypeBeforeEach, "BeforeEach"),
88			Entry(nil, types.NodeTypeJustBeforeEach, "JustBeforeEach"),
89			Entry(nil, types.NodeTypeAfterEach, "AfterEach"),
90			Entry(nil, types.NodeTypeJustAfterEach, "JustAfterEach"),
91			Entry(nil, types.NodeTypeBeforeAll, "BeforeAll"),
92			Entry(nil, types.NodeTypeAfterAll, "AfterAll"),
93			Entry(nil, types.NodeTypeBeforeSuite, "BeforeSuite"),
94			Entry(nil, types.NodeTypeSynchronizedBeforeSuite, "SynchronizedBeforeSuite"),
95			Entry(nil, types.NodeTypeAfterSuite, "AfterSuite"),
96			Entry(nil, types.NodeTypeSynchronizedAfterSuite, "SynchronizedAfterSuite"),
97			Entry(nil, types.NodeTypeReportBeforeEach, "ReportBeforeEach"),
98			Entry(nil, types.NodeTypeReportAfterEach, "ReportAfterEach"),
99			Entry(nil, types.NodeTypeReportAfterSuite, "ReportAfterSuite"),
100			Entry(nil, types.NodeTypeCleanupInvalid, "INVALID CLEANUP NODE"),
101			Entry(nil, types.NodeTypeCleanupAfterEach, "DeferCleanup"),
102			Entry(nil, types.NodeTypeCleanupAfterAll, "DeferCleanup (All)"),
103			Entry(nil, types.NodeTypeCleanupAfterSuite, "DeferCleanup (Suite)"),
104			Entry(nil, types.NodeTypeInvalid, "INVALID NODE TYPE"),
105		)
106	})
107
108	Describe("FailureNodeContext", func() {
109		DescribeTable("Representation and Encoding", func(context types.FailureNodeContext) {
110			marshalled, err := json.Marshal(context)
111			Ω(err).ShouldNot(HaveOccurred())
112			var unmarshalled types.FailureNodeContext
113			json.Unmarshal(marshalled, &unmarshalled)
114			Ω(unmarshalled).Should(Equal(context))
115		},
116			Entry("LeafNode", types.FailureNodeIsLeafNode),
117			Entry("TopLevel", types.FailureNodeAtTopLevel),
118			Entry("InContainer", types.FailureNodeInContainer),
119			Entry("Invalid", types.FailureNodeContextInvalid),
120		)
121	})
122
123	Describe("SpecState", func() {
124		DescribeTable("Representation and Encoding", func(specState types.SpecState, expectedString string) {
125			Ω(specState.String()).Should(Equal(expectedString))
126
127			marshalled, err := json.Marshal(specState)
128			Ω(err).ShouldNot(HaveOccurred())
129			var unmarshalled types.SpecState
130			json.Unmarshal(marshalled, &unmarshalled)
131			Ω(unmarshalled).Should(Equal(specState))
132		},
133			Entry("Pending", types.SpecStatePending, "pending"),
134			Entry("Skipped", types.SpecStateSkipped, "skipped"),
135			Entry("Passed", types.SpecStatePassed, "passed"),
136			Entry("Failed", types.SpecStateFailed, "failed"),
137			Entry("Panicked", types.SpecStatePanicked, "panicked"),
138			Entry("Aborted", types.SpecStateAborted, "aborted"),
139			Entry("Interrupted", types.SpecStateInterrupted, "interrupted"),
140			Entry("Invalid", types.SpecStateInvalid, "INVALID SPEC STATE"),
141		)
142	})
143
144	Describe("SpecReport Helper Functions", func() {
145		Describe("CombinedOutput", func() {
146			Context("with no GinkgoWriter or StdOutErr output", func() {
147				It("comes back empty", func() {
148					Ω(types.SpecReport{}.CombinedOutput()).Should(Equal(""))
149				})
150			})
151
152			Context("with only StdOutErr output", func() {
153				It("returns that output", func() {
154					Ω(types.SpecReport{
155						CapturedStdOutErr: "hello",
156					}.CombinedOutput()).Should(Equal("hello"))
157				})
158			})
159
160			Context("with only GinkgoWriter output", func() {
161				It("returns that output", func() {
162					Ω(types.SpecReport{
163						CapturedGinkgoWriterOutput: "hello",
164					}.CombinedOutput()).Should(Equal("hello"))
165				})
166			})
167
168			Context("with both", func() {
169				It("returns both concatenated", func() {
170					Ω(types.SpecReport{
171						CapturedGinkgoWriterOutput: "gw",
172						CapturedStdOutErr:          "std",
173					}.CombinedOutput()).Should(Equal("std\ngw"))
174				})
175			})
176		})
177
178		Describe("Labels", Label("TestA", "TestB"), func() {
179			It("returns a concatenated, deduped, set of labels", Label("TestB", "TestC"), func() {
180				Ω(CurrentSpecReport().Labels()).Should(Equal([]string{"TestA", "TestB", "TestC"}))
181			})
182		})
183
184		Describe("MatchesLabelFilter", Label("dog", "cat"), func() {
185			It("returns an error when passed an invalid filter query", func() {
186				matches, err := CurrentSpecReport().MatchesLabelFilter("(welp")
187				Ω(err).Should(HaveOccurred())
188				Ω(matches).Should(BeFalse())
189			})
190
191			It("returns whether or not the query matches", Label("catfish"), func() {
192				Ω(CurrentSpecReport().MatchesLabelFilter("dog")).Should(BeTrue())
193				Ω(CurrentSpecReport().MatchesLabelFilter("cow || cat")).Should(BeTrue())
194				Ω(CurrentSpecReport().MatchesLabelFilter("/fish/")).Should(BeTrue())
195				Ω(CurrentSpecReport().MatchesLabelFilter("dog && !/fish/")).Should(BeFalse())
196			})
197		})
198
199		It("can report on whether state is a failed state", func() {
200			Ω(types.SpecReport{State: types.SpecStatePending}.Failed()).Should(BeFalse())
201			Ω(types.SpecReport{State: types.SpecStateSkipped}.Failed()).Should(BeFalse())
202			Ω(types.SpecReport{State: types.SpecStatePassed}.Failed()).Should(BeFalse())
203			Ω(types.SpecReport{State: types.SpecStateFailed}.Failed()).Should(BeTrue())
204			Ω(types.SpecReport{State: types.SpecStatePanicked}.Failed()).Should(BeTrue())
205			Ω(types.SpecReport{State: types.SpecStateAborted}.Failed()).Should(BeTrue())
206			Ω(types.SpecReport{State: types.SpecStateInterrupted}.Failed()).Should(BeTrue())
207		})
208
209		It("can return a concatenated set of texts", func() {
210			Ω(CurrentSpecReport().FullText()).Should(Equal("Types SpecReport Helper Functions can return a concatenated set of texts"))
211		})
212
213		It("can return the name of the file it's spec is in", func() {
214			cl := types.NewCodeLocation(0)
215			Ω(CurrentSpecReport().FileName()).Should(Equal(cl.FileName))
216		})
217
218		It("can return the linenumber of the file it's spec is in", func() {
219			cl := types.NewCodeLocation(0)
220			Ω(CurrentSpecReport().LineNumber()).Should(Equal(cl.LineNumber - 1))
221		})
222
223		It("can return it's failure's message", func() {
224			report := types.SpecReport{
225				Failure: types.Failure{Message: "why this failed"},
226			}
227			Ω(report.FailureMessage()).Should(Equal("why this failed"))
228		})
229
230		It("can return it's failure's code location", func() {
231			cl := types.NewCodeLocation(0)
232			report := types.SpecReport{
233				Failure: types.Failure{Location: cl},
234			}
235			Ω(report.FailureLocation()).Should(Equal(cl))
236		})
237	})
238
239	Describe("SpecReports", func() {
240		Describe("Encoding to JSON", func() {
241			var report types.SpecReport
242			BeforeEach(func() {
243				report = types.SpecReport{
244					ContainerHierarchyTexts: []string{"A", "B"},
245					ContainerHierarchyLocations: []types.CodeLocation{
246						types.NewCodeLocation(0),
247						types.NewCodeLocationWithStackTrace(0),
248						types.NewCustomCodeLocation("welp"),
249					},
250					LeafNodeType:               types.NodeTypeIt,
251					LeafNodeLocation:           types.NewCodeLocation(0),
252					LeafNodeText:               "C",
253					State:                      types.SpecStateFailed,
254					StartTime:                  time.Date(2012, 06, 19, 05, 32, 12, 0, time.UTC),
255					EndTime:                    time.Date(2012, 06, 19, 05, 33, 12, 0, time.UTC),
256					RunTime:                    time.Minute,
257					ParallelProcess:            2,
258					NumAttempts:                3,
259					CapturedGinkgoWriterOutput: "gw",
260					CapturedStdOutErr:          "std",
261					Failure: types.Failure{
262						Message:                   "boom",
263						Location:                  types.NewCodeLocation(1),
264						ForwardedPanic:            "bam",
265						FailureNodeContext:        types.FailureNodeInContainer,
266						FailureNodeType:           types.NodeTypeBeforeEach,
267						FailureNodeLocation:       types.NewCodeLocation(0),
268						FailureNodeContainerIndex: 1,
269					},
270				}
271			})
272
273			Context("with a failure", func() {
274				It("round-trips correctly", func() {
275					marshalled, err := json.Marshal(report)
276					Ω(err).ShouldNot(HaveOccurred())
277					unmarshalled := types.SpecReport{}
278					err = json.Unmarshal(marshalled, &unmarshalled)
279					Ω(err).ShouldNot(HaveOccurred())
280					Ω(unmarshalled).Should(Equal(report))
281				})
282			})
283
284			Context("without a failure", func() {
285				BeforeEach(func() {
286					report.Failure = types.Failure{}
287				})
288				It("round-trips correctly and doesn't include the Failure struct", func() {
289					marshalled, err := json.Marshal(report)
290					Ω(string(marshalled)).ShouldNot(ContainSubstring("Failure"))
291					Ω(err).ShouldNot(HaveOccurred())
292					unmarshalled := types.SpecReport{}
293					err = json.Unmarshal(marshalled, &unmarshalled)
294					Ω(err).ShouldNot(HaveOccurred())
295					Ω(unmarshalled).Should(Equal(report))
296				})
297			})
298		})
299
300		Describe("WithLeafNodeType", func() {
301			It("returns reports with the matching LeafNodeTypes", func() {
302				reports := types.SpecReports{
303					{LeafNodeType: types.NodeTypeIt, NumAttempts: 2},
304					{LeafNodeType: types.NodeTypeIt, NumAttempts: 3},
305					{LeafNodeType: types.NodeTypeBeforeSuite, NumAttempts: 4},
306					{LeafNodeType: types.NodeTypeAfterSuite, NumAttempts: 5},
307					{LeafNodeType: types.NodeTypeSynchronizedAfterSuite, NumAttempts: 6},
308				}
309
310				Ω(reports.WithLeafNodeType(types.NodeTypeIt | types.NodeTypeAfterSuite)).Should(Equal(types.SpecReports{
311					{LeafNodeType: types.NodeTypeIt, NumAttempts: 2},
312					{LeafNodeType: types.NodeTypeIt, NumAttempts: 3},
313					{LeafNodeType: types.NodeTypeAfterSuite, NumAttempts: 5},
314				}))
315			})
316		})
317
318		Describe("WithState", func() {
319			It("returns reports with the matching SpecStates", func() {
320				reports := types.SpecReports{
321					{State: types.SpecStatePassed, NumAttempts: 2},
322					{State: types.SpecStatePassed, NumAttempts: 3},
323					{State: types.SpecStateFailed, NumAttempts: 4},
324					{State: types.SpecStatePending, NumAttempts: 5},
325					{State: types.SpecStateSkipped, NumAttempts: 6},
326				}
327
328				Ω(reports.WithState(types.SpecStatePassed | types.SpecStatePending)).Should(Equal(types.SpecReports{
329					{State: types.SpecStatePassed, NumAttempts: 2},
330					{State: types.SpecStatePassed, NumAttempts: 3},
331					{State: types.SpecStatePending, NumAttempts: 5},
332				}))
333			})
334		})
335
336		Describe("CountWithState", func() {
337			It("returns the number with the matching SpecStates", func() {
338				reports := types.SpecReports{
339					{State: types.SpecStatePassed, NumAttempts: 2},
340					{State: types.SpecStatePassed, NumAttempts: 3},
341					{State: types.SpecStateFailed, NumAttempts: 4},
342					{State: types.SpecStatePending, NumAttempts: 5},
343					{State: types.SpecStateSkipped, NumAttempts: 6},
344				}
345
346				Ω(reports.CountWithState(types.SpecStatePassed | types.SpecStatePending)).Should(Equal(3))
347			})
348		})
349
350		Describe("CountOfFlakedSpecs", func() {
351			It("returns the number of passing specs with NumAttempts > 1", func() {
352				reports := types.SpecReports{
353					{State: types.SpecStatePassed, NumAttempts: 2},
354					{State: types.SpecStatePassed, NumAttempts: 2},
355					{State: types.SpecStatePassed, NumAttempts: 1},
356					{State: types.SpecStatePassed, NumAttempts: 1},
357					{State: types.SpecStateFailed, NumAttempts: 2},
358				}
359
360				Ω(reports.CountOfFlakedSpecs()).Should(Equal(2))
361			})
362		})
363	})
364})
365
Full Screen

default_reporter.go

Source: default_reporter.go Github

copy
1/*
2Ginkgo's Default Reporter
3
4A number of command line flags are available to tweak Ginkgo's default output.
5
6These are documented [here](http://onsi.github.io/ginkgo/#running_tests)
7*/
8package reporters
9
10import (
11	"fmt"
12	"io"
13	"runtime"
14	"strings"
15
16	"github.com/onsi/ginkgo/v2/formatter"
17	"github.com/onsi/ginkgo/v2/types"
18)
19
20type DefaultReporter struct {
21	conf   types.ReporterConfig
22	writer io.Writer
23
24	// managing the emission stream
25	lastChar                 string
26	lastEmissionWasDelimiter bool
27
28	// rendering
29	specDenoter  string
30	retryDenoter string
31	formatter    formatter.Formatter
32}
33
34func NewDefaultReporterUnderTest(conf types.ReporterConfig, writer io.Writer) *DefaultReporter {
35	reporter := NewDefaultReporter(conf, writer)
36	reporter.formatter = formatter.New(formatter.ColorModePassthrough)
37
38	return reporter
39}
40
41func NewDefaultReporter(conf types.ReporterConfig, writer io.Writer) *DefaultReporter {
42	reporter := &DefaultReporter{
43		conf:   conf,
44		writer: writer,
45
46		lastChar:                 "\n",
47		lastEmissionWasDelimiter: false,
48
49		specDenoter:  "•",
50		retryDenoter: "↺",
51		formatter:    formatter.NewWithNoColorBool(conf.NoColor),
52	}
53	if runtime.GOOS == "windows" {
54		reporter.specDenoter = "+"
55		reporter.retryDenoter = "R"
56	}
57
58	return reporter
59}
60
61/* The Reporter Interface */
62
63func (r *DefaultReporter) SuiteWillBegin(report types.Report) {
64	if r.conf.Verbosity().Is(types.VerbosityLevelSuccinct) {
65		r.emit(r.f("[%d] {{bold}}%s{{/}} ", report.SuiteConfig.RandomSeed, report.SuiteDescription))
66		if len(report.SuiteLabels) > 0 {
67			r.emit(r.f("{{coral}}[%s]{{/}} ", strings.Join(report.SuiteLabels, ", ")))
68		}
69		r.emit(r.f("- %d/%d specs ", report.PreRunStats.SpecsThatWillRun, report.PreRunStats.TotalSpecs))
70		if report.SuiteConfig.ParallelTotal > 1 {
71			r.emit(r.f("- %d procs ", report.SuiteConfig.ParallelTotal))
72		}
73	} else {
74		banner := r.f("Running Suite: %s - %s", report.SuiteDescription, report.SuitePath)
75		r.emitBlock(banner)
76		bannerWidth := len(banner)
77		if len(report.SuiteLabels) > 0 {
78			labels := strings.Join(report.SuiteLabels, ", ")
79			r.emitBlock(r.f("{{coral}}[%s]{{/}} ", labels))
80			if len(labels)+2 > bannerWidth {
81				bannerWidth = len(labels) + 2
82			}
83		}
84		r.emitBlock(strings.Repeat("=", bannerWidth))
85
86		out := r.f("Random Seed: {{bold}}%d{{/}}", report.SuiteConfig.RandomSeed)
87		if report.SuiteConfig.RandomizeAllSpecs {
88			out += r.f(" - will randomize all specs")
89		}
90		r.emitBlock(out)
91		r.emit("\n")
92		r.emitBlock(r.f("Will run {{bold}}%d{{/}} of {{bold}}%d{{/}} specs", report.PreRunStats.SpecsThatWillRun, report.PreRunStats.TotalSpecs))
93		if report.SuiteConfig.ParallelTotal > 1 {
94			r.emitBlock(r.f("Running in parallel across {{bold}}%d{{/}} processes", report.SuiteConfig.ParallelTotal))
95		}
96	}
97}
98
99func (r *DefaultReporter) WillRun(report types.SpecReport) {
100	if r.conf.Verbosity().LT(types.VerbosityLevelVerbose) || report.State.Is(types.SpecStatePending|types.SpecStateSkipped) {
101		return
102	}
103
104	r.emitDelimiter()
105	indentation := uint(0)
106	if report.LeafNodeType.Is(types.NodeTypesForSuiteLevelNodes) {
107		r.emitBlock(r.f("{{bold}}[%s] %s{{/}}", report.LeafNodeType.String(), report.LeafNodeText))
108	} else {
109		if len(report.ContainerHierarchyTexts) > 0 {
110			r.emitBlock(r.cycleJoin(report.ContainerHierarchyTexts, " "))
111			indentation = 1
112		}
113		line := r.fi(indentation, "{{bold}}%s{{/}}", report.LeafNodeText)
114		labels := report.Labels()
115		if len(labels) > 0 {
116			line += r.f(" {{coral}}[%s]{{/}}", strings.Join(labels, ", "))
117		}
118		r.emitBlock(line)
119	}
120	r.emitBlock(r.fi(indentation, "{{gray}}%s{{/}}", report.LeafNodeLocation))
121}
122
123func (r *DefaultReporter) DidRun(report types.SpecReport) {
124	v := r.conf.Verbosity()
125	var header, highlightColor string
126	includeRuntime, emitGinkgoWriterOutput, stream, denoter := true, true, false, r.specDenoter
127	succinctLocationBlock := v.Is(types.VerbosityLevelSuccinct)
128
129	hasGW := report.CapturedGinkgoWriterOutput != ""
130	hasStd := report.CapturedStdOutErr != ""
131	hasEmittableReports := report.ReportEntries.HasVisibility(types.ReportEntryVisibilityAlways) || (report.ReportEntries.HasVisibility(types.ReportEntryVisibilityFailureOrVerbose) && (!report.Failure.IsZero() || v.GTE(types.VerbosityLevelVerbose)))
132
133	if report.LeafNodeType.Is(types.NodeTypesForSuiteLevelNodes) {
134		denoter = fmt.Sprintf("[%s]", report.LeafNodeType)
135	}
136
137	switch report.State {
138	case types.SpecStatePassed:
139		highlightColor, succinctLocationBlock = "{{green}}", v.LT(types.VerbosityLevelVerbose)
140		emitGinkgoWriterOutput = (r.conf.AlwaysEmitGinkgoWriter || v.GTE(types.VerbosityLevelVerbose)) && hasGW
141		if report.LeafNodeType.Is(types.NodeTypesForSuiteLevelNodes) {
142			if v.GTE(types.VerbosityLevelVerbose) || hasStd || hasEmittableReports {
143				header = fmt.Sprintf("%s PASSED", denoter)
144			} else {
145				return
146			}
147		} else {
148			header, stream = denoter, true
149			if report.NumAttempts > 1 {
150				header, stream = fmt.Sprintf("%s [FLAKEY TEST - TOOK %d ATTEMPTS TO PASS]", r.retryDenoter, report.NumAttempts), false
151			}
152			if report.RunTime > r.conf.SlowSpecThreshold {
153				header, stream = fmt.Sprintf("%s [SLOW TEST]", header), false
154			}
155		}
156		if hasStd || emitGinkgoWriterOutput || hasEmittableReports {
157			stream = false
158		}
159	case types.SpecStatePending:
160		highlightColor = "{{yellow}}"
161		includeRuntime, emitGinkgoWriterOutput = false, false
162		if v.Is(types.VerbosityLevelSuccinct) {
163			header, stream = "P", true
164		} else {
165			header, succinctLocationBlock = "P [PENDING]", v.LT(types.VerbosityLevelVeryVerbose)
166		}
167	case types.SpecStateSkipped:
168		highlightColor = "{{cyan}}"
169		if report.Failure.Message != "" || v.Is(types.VerbosityLevelVeryVerbose) {
170			header = "S [SKIPPED]"
171		} else {
172			header, stream = "S", true
173		}
174	case types.SpecStateFailed:
175		highlightColor, header = "{{red}}", fmt.Sprintf("%s [FAILED]", denoter)
176	case types.SpecStatePanicked:
177		highlightColor, header = "{{magenta}}", fmt.Sprintf("%s! [PANICKED]", denoter)
178	case types.SpecStateInterrupted:
179		highlightColor, header = "{{orange}}", fmt.Sprintf("%s! [INTERRUPTED]", denoter)
180	case types.SpecStateAborted:
181		highlightColor, header = "{{coral}}", fmt.Sprintf("%s! [ABORTED]", denoter)
182	}
183
184	// Emit stream and return
185	if stream {
186		r.emit(r.f(highlightColor + header + "{{/}}"))
187		return
188	}
189
190	// Emit header
191	r.emitDelimiter()
192	if includeRuntime {
193		header = r.f("%s [%.3f seconds]", header, report.RunTime.Seconds())
194	}
195	r.emitBlock(r.f(highlightColor + header + "{{/}}"))
196
197	// Emit Code Location Block
198	r.emitBlock(r.codeLocationBlock(report, highlightColor, succinctLocationBlock, false))
199
200	//Emit Stdout/Stderr Output
201	if hasStd {
202		r.emitBlock("\n")
203		r.emitBlock(r.fi(1, "{{gray}}Begin Captured StdOut/StdErr Output >>{{/}}"))
204		r.emitBlock(r.fi(2, "%s", report.CapturedStdOutErr))
205		r.emitBlock(r.fi(1, "{{gray}}<< End Captured StdOut/StdErr Output{{/}}"))
206	}
207
208	//Emit Captured GinkgoWriter Output
209	if emitGinkgoWriterOutput && hasGW {
210		r.emitBlock("\n")
211		r.emitBlock(r.fi(1, "{{gray}}Begin Captured GinkgoWriter Output >>{{/}}"))
212		r.emitBlock(r.fi(2, "%s", report.CapturedGinkgoWriterOutput))
213		r.emitBlock(r.fi(1, "{{gray}}<< End Captured GinkgoWriter Output{{/}}"))
214	}
215
216	if hasEmittableReports {
217		r.emitBlock("\n")
218		r.emitBlock(r.fi(1, "{{gray}}Begin Report Entries >>{{/}}"))
219		reportEntries := report.ReportEntries.WithVisibility(types.ReportEntryVisibilityAlways)
220		if !report.Failure.IsZero() || v.GTE(types.VerbosityLevelVerbose) {
221			reportEntries = report.ReportEntries.WithVisibility(types.ReportEntryVisibilityAlways, types.ReportEntryVisibilityFailureOrVerbose)
222		}
223		for _, entry := range reportEntries {
224			r.emitBlock(r.fi(2, "{{bold}}"+entry.Name+"{{gray}} - %s @ %s{{/}}", entry.Location, entry.Time.Format(types.GINKGO_TIME_FORMAT)))
225			if representation := entry.StringRepresentation(); representation != "" {
226				r.emitBlock(r.fi(3, representation))
227			}
228		}
229		r.emitBlock(r.fi(1, "{{gray}}<< End Report Entries{{/}}"))
230	}
231
232	// Emit Failure Message
233	if !report.Failure.IsZero() {
234		r.emitBlock("\n")
235		r.emitBlock(r.fi(1, highlightColor+"%s{{/}}", report.Failure.Message))
236		r.emitBlock(r.fi(1, highlightColor+"In {{bold}}[%s]{{/}}"+highlightColor+" at: {{bold}}%s{{/}}\n", report.Failure.FailureNodeType, report.Failure.Location))
237		if report.Failure.ForwardedPanic != "" {
238			r.emitBlock("\n")
239			r.emitBlock(r.fi(1, highlightColor+"%s{{/}}", report.Failure.ForwardedPanic))
240		}
241
242		if r.conf.FullTrace || report.Failure.ForwardedPanic != "" {
243			r.emitBlock("\n")
244			r.emitBlock(r.fi(1, highlightColor+"Full Stack Trace{{/}}"))
245			r.emitBlock(r.fi(2, "%s", report.Failure.Location.FullStackTrace))
246		}
247	}
248
249	r.emitDelimiter()
250}
251
252func (r *DefaultReporter) SuiteDidEnd(report types.Report) {
253	failures := report.SpecReports.WithState(types.SpecStateFailureStates)
254	if len(failures) > 1 {
255		r.emitBlock("\n\n")
256		r.emitBlock(r.f("{{red}}{{bold}}Summarizing %d Failures:{{/}}", len(failures)))
257		for _, specReport := range failures {
258			highlightColor, heading := "{{red}}", "[FAIL]"
259			switch specReport.State {
260			case types.SpecStatePanicked:
261				highlightColor, heading = "{{magenta}}", "[PANICKED!]"
262			case types.SpecStateAborted:
263				highlightColor, heading = "{{coral}}", "[ABORTED]"
264			case types.SpecStateInterrupted:
265				highlightColor, heading = "{{orange}}", "[INTERRUPTED]"
266			}
267			locationBlock := r.codeLocationBlock(specReport, highlightColor, true, true)
268			r.emitBlock(r.fi(1, highlightColor+"%s{{/}} %s", heading, locationBlock))
269		}
270	}
271
272	//summarize the suite
273	if r.conf.Verbosity().Is(types.VerbosityLevelSuccinct) && report.SuiteSucceeded {
274		r.emit(r.f(" {{green}}SUCCESS!{{/}} %s ", report.RunTime))
275		return
276	}
277
278	r.emitBlock("\n")
279	color, status := "{{green}}{{bold}}", "SUCCESS!"
280	if !report.SuiteSucceeded {
281		color, status = "{{red}}{{bold}}", "FAIL!"
282	}
283
284	specs := report.SpecReports.WithLeafNodeType(types.NodeTypeIt) //exclude any suite setup nodes
285	r.emitBlock(r.f(color+"Ran %d of %d Specs in %.3f seconds{{/}}",
286		specs.CountWithState(types.SpecStatePassed)+specs.CountWithState(types.SpecStateFailureStates),
287		report.PreRunStats.TotalSpecs,
288		report.RunTime.Seconds()),
289	)
290
291	switch len(report.SpecialSuiteFailureReasons) {
292	case 0:
293		r.emit(r.f(color+"%s{{/}} -- ", status))
294	case 1:
295		r.emit(r.f(color+"%s - %s{{/}} -- ", status, report.SpecialSuiteFailureReasons[0]))
296	default:
297		r.emitBlock(r.f(color+"%s - %s{{/}}\n", status, strings.Join(report.SpecialSuiteFailureReasons, ", ")))
298	}
299
300	if len(specs) == 0 && report.SpecReports.WithLeafNodeType(types.NodeTypeBeforeSuite|types.NodeTypeSynchronizedBeforeSuite).CountWithState(types.SpecStateFailureStates) > 0 {
301		r.emit(r.f("{{cyan}}{{bold}}A BeforeSuite node failed so all tests were skipped.{{/}}\n"))
302	} else {
303		r.emit(r.f("{{green}}{{bold}}%d Passed{{/}} | ", specs.CountWithState(types.SpecStatePassed)))
304		r.emit(r.f("{{red}}{{bold}}%d Failed{{/}} | ", specs.CountWithState(types.SpecStateFailureStates)))
305		if specs.CountOfFlakedSpecs() > 0 {
306			r.emit(r.f("{{light-yellow}}{{bold}}%d Flaked{{/}} | ", specs.CountOfFlakedSpecs()))
307		}
308		r.emit(r.f("{{yellow}}{{bold}}%d Pending{{/}} | ", specs.CountWithState(types.SpecStatePending)))
309		r.emit(r.f("{{cyan}}{{bold}}%d Skipped{{/}}\n", specs.CountWithState(types.SpecStateSkipped)))
310	}
311}
312
313/* Emitting to the writer */
314func (r *DefaultReporter) emit(s string) {
315	if len(s) > 0 {
316		r.lastChar = s[len(s)-1:]
317		r.lastEmissionWasDelimiter = false
318		r.writer.Write([]byte(s))
319	}
320}
321
322func (r *DefaultReporter) emitBlock(s string) {
323	if len(s) > 0 {
324		if r.lastChar != "\n" {
325			r.emit("\n")
326		}
327		r.emit(s)
328		if r.lastChar != "\n" {
329			r.emit("\n")
330		}
331	}
332}
333
334func (r *DefaultReporter) emitDelimiter() {
335	if r.lastEmissionWasDelimiter {
336		return
337	}
338	r.emitBlock(r.f("{{gray}}%s{{/}}", strings.Repeat("-", 30)))
339	r.lastEmissionWasDelimiter = true
340}
341
342/* Rendering text */
343func (r *DefaultReporter) f(format string, args ...interface{}) string {
344	return r.formatter.F(format, args...)
345}
346
347func (r *DefaultReporter) fi(indentation uint, format string, args ...interface{}) string {
348	return r.formatter.Fi(indentation, format, args...)
349}
350
351func (r *DefaultReporter) cycleJoin(elements []string, joiner string) string {
352	return r.formatter.CycleJoin(elements, joiner, []string{"{{/}}", "{{gray}}"})
353}
354
355func (r *DefaultReporter) codeLocationBlock(report types.SpecReport, highlightColor string, succinct bool, usePreciseFailureLocation bool) string {
356	texts, locations, labels := []string{}, []types.CodeLocation{}, [][]string{}
357	texts, locations, labels = append(texts, report.ContainerHierarchyTexts...), append(locations, report.ContainerHierarchyLocations...), append(labels, report.ContainerHierarchyLabels...)
358	if report.LeafNodeType.Is(types.NodeTypesForSuiteLevelNodes) {
359		texts = append(texts, r.f("[%s] %s", report.LeafNodeType, report.LeafNodeText))
360	} else {
361		texts = append(texts, report.LeafNodeText)
362	}
363	labels = append(labels, report.LeafNodeLabels)
364	locations = append(locations, report.LeafNodeLocation)
365
366	failureLocation := report.Failure.FailureNodeLocation
367	if usePreciseFailureLocation {
368		failureLocation = report.Failure.Location
369	}
370
371	switch report.Failure.FailureNodeContext {
372	case types.FailureNodeAtTopLevel:
373		texts = append([]string{r.f(highlightColor+"{{bold}}TOP-LEVEL [%s]{{/}}", report.Failure.FailureNodeType)}, texts...)
374		locations = append([]types.CodeLocation{failureLocation}, locations...)
375		labels = append([][]string{{}}, labels...)
376	case types.FailureNodeInContainer:
377		i := report.Failure.FailureNodeContainerIndex
378		texts[i] = r.f(highlightColor+"{{bold}}%s [%s]{{/}}", texts[i], report.Failure.FailureNodeType)
379		locations[i] = failureLocation
380	case types.FailureNodeIsLeafNode:
381		i := len(texts) - 1
382		texts[i] = r.f(highlightColor+"{{bold}}[%s] %s{{/}}", report.LeafNodeType, report.LeafNodeText)
383		locations[i] = failureLocation
384	}
385
386	out := ""
387	if succinct {
388		out += r.f("%s", r.cycleJoin(texts, " "))
389		flattenedLabels := report.Labels()
390		if len(flattenedLabels) > 0 {
391			out += r.f(" {{coral}}[%s]{{/}}", strings.Join(flattenedLabels, ", "))
392		}
393		out += "\n"
394		if usePreciseFailureLocation {
395			out += r.f("{{gray}}%s{{/}}", failureLocation)
396		} else {
397			out += r.f("{{gray}}%s{{/}}", locations[len(locations)-1])
398		}
399	} else {
400		for i := range texts {
401			out += r.fi(uint(i), "%s", texts[i])
402			if len(labels[i]) > 0 {
403				out += r.f(" {{coral}}[%s]{{/}}", strings.Join(labels[i], ", "))
404			}
405			out += "\n"
406			out += r.fi(uint(i), "{{gray}}%s{{/}}\n", locations[i])
407		}
408	}
409	return out
410}
411
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in

Trigger CountOfFlakedSpecs code on LambdaTest Cloud Grid

Execute automation tests with CountOfFlakedSpecs on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)