How to use WithState method of test_helpers Package

Best Ginkgo code snippet using test_helpers.WithState

Run Ginkgo automation tests on LambdaTest cloud grid

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

skip_test.go

Source: skip_test.go Github

copy
1package internal_integration_test
2
3import (
4	. "github.com/onsi/ginkgo/v2"
5	"github.com/onsi/ginkgo/v2/types"
6	. "github.com/onsi/gomega"
7
8	. "github.com/onsi/ginkgo/v2/internal/test_helpers"
9)
10
11var _ = Describe("Skip", func() {
12	Context("When Skip() is called in individual subject and setup nodes", func() {
13		BeforeEach(func() {
14			success, _ := RunFixture("Skip() tests", func() {
15				Describe("container to ensure order", func() {
16					It("A", rt.T("A"))
17					Describe("container", func() {
18						BeforeEach(rt.T("bef", func() {
19							failer.Skip("skip in Bef", cl)
20							panic("boom") //simulates what Ginkgo DSL does
21						}))
22						It("B", rt.T("B"))
23						It("C", rt.T("C"))
24						AfterEach(rt.T("aft"))
25					})
26					It("D", rt.T("D", func() {
27						failer.Skip("skip D", cl)
28						panic("boom") //simulates what Ginkgo DSL does
29					}))
30				})
31			})
32
33			Ω(success).Should(BeTrue())
34		})
35
36		It("skips the tests that are Skipped()", func() {
37			Ω(rt).Should(HaveTracked("A", "bef", "aft", "bef", "aft", "D"))
38			Ω(reporter.Did.WithState(types.SpecStatePassed).Names()).Should(ConsistOf("A"))
39			Ω(reporter.Did.WithState(types.SpecStateSkipped).Names()).Should(ConsistOf("B", "C", "D"))
40
41			Ω(reporter.Did.Find("B").Failure.Message).Should(Equal("skip in Bef"))
42			Ω(reporter.Did.Find("B").Failure.Location).Should(Equal(cl))
43
44			Ω(reporter.Did.Find("D").Failure.Message).Should(Equal("skip D"))
45			Ω(reporter.Did.Find("D").Failure.Location).Should(Equal(cl))
46		})
47
48		It("report on the suite with accurate numbers", func() {
49			Ω(reporter.End).Should(BeASuiteSummary(true, NPassed(1), NSkipped(3), NPending(0), NSpecs(4), NWillRun(4)))
50		})
51	})
52
53	Context("when Skip() is called in BeforeSuite", func() {
54		BeforeEach(func() {
55			success, _ := RunFixture("Skip() BeforeSuite", func() {
56				BeforeSuite(func() {
57					rt.Run("befs")
58					Skip("skip please")
59				})
60				Describe("container to ensure order", func() {
61					It("A", rt.T("A"))
62					It("B", rt.T("B"))
63					It("C", rt.T("C"))
64				})
65			})
66
67			Ω(success).Should(BeTrue())
68		})
69
70		It("skips all the tsts", func() {
71			Ω(rt).Should(HaveTracked("befs"))
72			Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeBeforeSuite)).Should(HaveBeenSkippedWithMessage("skip please"))
73			Ω(reporter.Did.Find("A")).Should(HaveBeenSkipped())
74			Ω(reporter.Did.Find("B")).Should(HaveBeenSkipped())
75			Ω(reporter.Did.Find("C")).Should(HaveBeenSkipped())
76		})
77
78		It("report on the suite with accurate numbers", func() {
79			Ω(reporter.End).Should(BeASuiteSummary(true, NPassed(0), NSkipped(3), NPending(0), NSpecs(3), NWillRun(3)))
80			Ω(reporter.End.SpecialSuiteFailureReasons).Should(ContainElement("Suite skipped in BeforeSuite"))
81		})
82	})
83})
84
Full Screen

focus_test.go

Source: focus_test.go Github

copy
1package internal_integration_test
2
3import (
4	. "github.com/onsi/ginkgo/v2"
5	"github.com/onsi/ginkgo/v2/types"
6	. "github.com/onsi/gomega"
7
8	. "github.com/onsi/ginkgo/v2/internal/test_helpers"
9)
10
11var _ = Describe("Focus", func() {
12	Describe("when a suite has pending tests", func() {
13		fixture := func() {
14			It("A", rt.T("A"))
15			It("B", rt.T("B"))
16			It("C", rt.T("C"), Pending)
17			Describe("container", func() {
18				It("D", rt.T("D"))
19			})
20			PDescribe("pending container", func() {
21				It("E", rt.T("E"))
22				It("F", rt.T("F"))
23			})
24		}
25		Context("without config.FailOnPending", func() {
26			BeforeEach(func() {
27				success, hPF := RunFixture("pending tests", fixture)
28				Ω(success).Should(BeTrue())
29				Ω(hPF).Should(BeFalse())
30			})
31
32			It("should not report that the suite hasProgrammaticFocus", func() {
33				Ω(reporter.Begin.SuiteHasProgrammaticFocus).Should(BeFalse())
34				Ω(reporter.End.SuiteHasProgrammaticFocus).Should(BeFalse())
35			})
36
37			It("does not run the pending tests", func() {
38				Ω(rt.TrackedRuns()).Should(ConsistOf("A", "B", "D"))
39			})
40
41			It("reports on the pending tests", func() {
42				Ω(reporter.Did.WithState(types.SpecStatePassed).Names()).Should(ConsistOf("A", "B", "D"))
43				Ω(reporter.Did.WithState(types.SpecStatePending).Names()).Should(ConsistOf("C", "E", "F"))
44			})
45
46			It("reports on the suite with accurate numbers", func() {
47				Ω(reporter.End).Should(BeASuiteSummary(true, NSpecs(6), NPassed(3), NPending(3), NWillRun(3), NSkipped(0)))
48			})
49
50			It("does not include a special suite failure reason", func() {
51				Ω(reporter.End.SpecialSuiteFailureReasons).Should(BeEmpty())
52			})
53		})
54
55		Context("with config.FailOnPending", func() {
56			BeforeEach(func() {
57				conf.FailOnPending = true
58				success, hPF := RunFixture("pending tests", fixture)
59				Ω(success).Should(BeFalse())
60				Ω(hPF).Should(BeFalse())
61			})
62
63			It("reports on the suite with accurate numbers", func() {
64				Ω(reporter.End).Should(BeASuiteSummary(false, NPassed(3), NSpecs(6), NPending(3), NWillRun(3), NSkipped(0)))
65			})
66
67			It("includes a special suite failure reason", func() {
68				Ω(reporter.End.SpecialSuiteFailureReasons).Should(ContainElement("Detected pending specs and --fail-on-pending is set"))
69			})
70		})
71	})
72
73	Describe("with programmatic focus", func() {
74		var success bool
75		var hasProgrammaticFocus bool
76		BeforeEach(func() {
77			success, hasProgrammaticFocus = RunFixture("focused tests", func() {
78				It("A", rt.T("A"))
79				It("B", rt.T("B"))
80				FDescribe("focused container", func() {
81					It("C", rt.T("C"))
82					It("D", rt.T("D"))
83					PIt("E", rt.T("E"))
84				})
85				FDescribe("focused container with focused child", func() {
86					It("F", rt.T("F"))
87					It("G", Focus, rt.T("G"))
88				})
89				Describe("container", func() {
90					It("H", rt.T("H"))
91				})
92				FIt("I", rt.T("I"))
93			})
94			Ω(success).Should(BeTrue())
95		})
96
97		It("should return true for hasProgrammaticFocus", func() {
98			Ω(hasProgrammaticFocus).Should(BeTrue())
99		})
100
101		It("should report that the suite hasProgrammaticFocus", func() {
102			Ω(reporter.Begin.SuiteHasProgrammaticFocus).Should(BeTrue())
103			Ω(reporter.End.SuiteHasProgrammaticFocus).Should(BeTrue())
104		})
105
106		It("should run the focused tests, honoring the nested focus policy", func() {
107			Ω(rt.TrackedRuns()).Should(ConsistOf("C", "D", "G", "I"))
108		})
109
110		It("should report on the tests correctly", func() {
111			Ω(reporter.Did.WithState(types.SpecStateSkipped).Names()).Should(ConsistOf("A", "B", "F", "H"))
112			Ω(reporter.Did.WithState(types.SpecStatePending).Names()).Should(ConsistOf("E"))
113			Ω(reporter.Did.WithState(types.SpecStatePassed).Names()).Should(ConsistOf("C", "D", "G", "I"))
114		})
115
116		It("report on the suite with accurate numbers", func() {
117			Ω(reporter.End).Should(BeASuiteSummary(true, NPassed(4), NSkipped(4), NPending(1), NSpecs(9), NWillRun(4)))
118		})
119	})
120
121	Describe("with config.FocusStrings and config.SkipStrings", func() {
122		BeforeEach(func() {
123			conf.FocusStrings = []string{"blue", "green"}
124			conf.SkipStrings = []string{"red"}
125			success, _ := RunFixture("cli focus tests", func() {
126				It("blue.1", rt.T("blue.1"))
127				It("blue.2", rt.T("blue.2"))
128				Describe("blue.container", func() {
129					It("yellow.1", rt.T("yellow.1"))
130					It("red.1", rt.T("red.1"))
131					PIt("blue.3", rt.T("blue.3"))
132				})
133				Describe("green.container", func() {
134					It("yellow.2", rt.T("yellow.2"))
135					It("green.1", rt.T("green.1"))
136				})
137				Describe("red.2", func() {
138					It("green.2", rt.T("green.2"))
139				})
140				FIt("red.3", rt.T("red.3"))
141			})
142			Ω(success).Should(BeTrue())
143		})
144
145		It("should run tests that match", func() {
146			Ω(rt.TrackedRuns()).Should(ConsistOf("blue.1", "blue.2", "yellow.1", "yellow.2", "green.1"))
147		})
148
149		It("should report on the tests correctly", func() {
150			Ω(reporter.Did.WithState(types.SpecStateSkipped).Names()).Should(ConsistOf("red.1", "green.2", "red.3"))
151			Ω(reporter.Did.WithState(types.SpecStatePending).Names()).Should(ConsistOf("blue.3"))
152			Ω(reporter.Did.WithState(types.SpecStatePassed).Names()).Should(ConsistOf("blue.1", "blue.2", "yellow.1", "yellow.2", "green.1"))
153		})
154
155		It("report on the suite with accurate numbers", func() {
156			Ω(reporter.End).Should(BeASuiteSummary(true, NPassed(5), NSkipped(3), NPending(1), NSpecs(9), NWillRun(5)))
157		})
158	})
159
160	Describe("when no tests will end up running", func() {
161		BeforeEach(func() {
162			conf.FocusStrings = []string{"red"}
163			success, _ := RunFixture("cli focus tests", func() {
164				BeforeSuite(rt.T("bef-suite"))
165				AfterSuite(rt.T("aft-suite"))
166				It("blue.1", rt.T("blue.1"))
167				It("blue.2", rt.T("blue.2"))
168			})
169			Ω(success).Should(BeTrue())
170		})
171
172		It("does not run the BeforeSuite or the AfterSuite", func() {
173			Ω(rt).Should(HaveTrackedNothing())
174		})
175	})
176
177	Describe("Skip()", func() {
178		BeforeEach(func() {
179			success, _ := RunFixture("Skip() tests", func() {
180				Describe("container to ensure order", func() {
181					It("A", rt.T("A"))
182					Describe("container", func() {
183						BeforeEach(rt.T("bef", func() {
184							failer.Skip("skip in Bef", cl)
185							panic("boom") //simulates what Ginkgo DSL does
186						}))
187						It("B", rt.T("B"))
188						It("C", rt.T("C"))
189						AfterEach(rt.T("aft"))
190					})
191					It("D", rt.T("D", func() {
192						failer.Skip("skip D", cl)
193						panic("boom") //simulates what Ginkgo DSL does
194					}))
195				})
196			})
197
198			Ω(success).Should(BeTrue())
199		})
200
201		It("skips the tests that are Skipped()", func() {
202			Ω(rt).Should(HaveTracked("A", "bef", "aft", "bef", "aft", "D"))
203			Ω(reporter.Did.WithState(types.SpecStatePassed).Names()).Should(ConsistOf("A"))
204			Ω(reporter.Did.WithState(types.SpecStateSkipped).Names()).Should(ConsistOf("B", "C", "D"))
205
206			Ω(reporter.Did.Find("B").Failure.Message).Should(Equal("skip in Bef"))
207			Ω(reporter.Did.Find("B").Failure.Location).Should(Equal(cl))
208
209			Ω(reporter.Did.Find("D").Failure.Message).Should(Equal("skip D"))
210			Ω(reporter.Did.Find("D").Failure.Location).Should(Equal(cl))
211		})
212
213		It("report on the suite with accurate numbers", func() {
214			Ω(reporter.End).Should(BeASuiteSummary(true, NPassed(1), NSkipped(3), NPending(0), NSpecs(4), NWillRun(4)))
215		})
216	})
217})
218
Full Screen

interrupt_test.go

Source: interrupt_test.go Github

copy
1package internal_integration_test
2
3import (
4	"time"
5
6	. "github.com/onsi/ginkgo/v2"
7	"github.com/onsi/ginkgo/v2/internal/interrupt_handler"
8	. "github.com/onsi/ginkgo/v2/internal/test_helpers"
9	"github.com/onsi/ginkgo/v2/types"
10	. "github.com/onsi/gomega"
11)
12
13var _ = Describe("When a test suite is interrupted", func() {
14	Describe("when it is interrupted in a BeforeSuite", func() {
15		BeforeEach(func() {
16			success, _ := RunFixture("interrupted test", func() {
17				BeforeSuite(rt.T("before-suite", func() {
18					interruptHandler.Interrupt(interrupt_handler.InterruptCauseTimeout)
19					time.Sleep(time.Hour)
20				}))
21				AfterSuite(rt.T("after-suite"))
22				It("A", rt.T("A"))
23				It("B", rt.T("B"))
24			})
25			Ω(success).Should(Equal(false))
26		})
27
28		It("runs the AfterSuite and skips all the tests", func() {
29			Ω(rt).Should(HaveTracked("before-suite", "after-suite"))
30			Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeIt)).Should(BeZero())
31		})
32
33		It("reports the correct failure", func() {
34			summary := reporter.Did.FindByLeafNodeType(types.NodeTypeBeforeSuite)
35			Ω(summary.State).Should(Equal(types.SpecStateInterrupted))
36			Ω(summary.Failure.Message).Should(ContainSubstring("Interrupted by Timeout\nstack trace"))
37		})
38
39		It("reports the correct statistics", func() {
40			Ω(reporter.End).Should(BeASuiteSummary(false, NSpecs(2), NWillRun(2), NPassed(0), NSkipped(0), NFailed(0)))
41		})
42
43		It("reports the correct special failure reason", func() {
44			Ω(reporter.End.SpecialSuiteFailureReasons).Should(ContainElement("Interrupted by Timeout"))
45		})
46	})
47
48	Describe("when it is interrupted in a test", func() {
49		BeforeEach(func() {
50			conf.FlakeAttempts = 3
51			success, _ := RunFixture("interrupted test", func() {
52				BeforeSuite(rt.T("before-suite"))
53				AfterSuite(rt.T("after-suite"))
54				BeforeEach(rt.T("bef.1"))
55				AfterEach(rt.T("aft.1"))
56				Describe("container", func() {
57					BeforeEach(rt.T("bef.2"))
58					AfterEach(rt.T("aft.2"))
59					It("runs", rt.T("runs"))
60					Describe("nested-container", func() {
61						BeforeEach(rt.T("bef.3-interrupt!", func() {
62							interruptHandler.Interrupt(interrupt_handler.InterruptCauseTimeout)
63							time.Sleep(time.Hour)
64						}))
65						AfterEach(rt.T("aft.3a"))
66						AfterEach(rt.T("aft.3b", func() {
67							interruptHandler.Interrupt(interrupt_handler.InterruptCauseTimeout)
68							time.Sleep(time.Hour)
69						}))
70						Describe("deeply-nested-container", func() {
71							BeforeEach(rt.T("bef.4"))
72							AfterEach(rt.T("aft.4"))
73							It("the interrupted test", rt.T("the interrupted test"))
74							It("skipped.1", rt.T("skipped.1"))
75						})
76					})
77					It("skipped.2", rt.T("skipped.2"))
78				})
79			})
80			Ω(success).Should(Equal(false))
81		})
82
83		It("unwinds the after eaches at the appropriate nesting level, allowing additional interrupts of after eaches as it goes", func() {
84			Ω(rt).Should(HaveTracked("before-suite",
85				"bef.1", "bef.2", "runs", "aft.2", "aft.1",
86				"bef.1", "bef.2", "bef.3-interrupt!", "aft.3a", "aft.3b", "aft.2", "aft.1",
87				"after-suite"))
88		})
89
90		It("skips subsequent tests", func() {
91			Ω(reporter.Did.WithState(types.SpecStatePassed).Names()).Should(ConsistOf("runs"))
92			Ω(reporter.Did.WithState(types.SpecStateInterrupted).Names()).Should(ConsistOf("the interrupted test"))
93			Ω(reporter.Did.WithState(types.SpecStateSkipped).Names()).Should(ConsistOf("skipped.1", "skipped.2"))
94		})
95
96		It("reports the interrupted test as interrupted and emits a stack trace", func() {
97			message := reporter.Did.Find("the interrupted test").Failure.Message
98			Ω(message).Should(ContainSubstring("Interrupted by Timeout\nstack trace"))
99		})
100
101		It("reports the correct statistics", func() {
102			Ω(reporter.End).Should(BeASuiteSummary(false, NSpecs(4), NWillRun(4), NPassed(1), NSkipped(2), NFailed(1)))
103			Ω(reporter.End.SpecialSuiteFailureReasons).Should(ContainElement("Interrupted by Timeout"))
104		})
105	})
106})
107
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
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)