Best Ginkgo code snippet using ginkgo.Describe
ginkgo_dsl.go
Source:ginkgo_dsl.go
...112//to tell Ginkgo that your async test is done.113type Done chan<- interface{}114//GinkgoTestDescription represents the information about the current running test returned by CurrentGinkgoTestDescription115// FullTestText: a concatenation of ComponentTexts and the TestText116// ComponentTexts: a list of all texts for the Describes & Contexts leading up to the current test117// TestText: the text in the actual It or Measure node118// IsMeasurement: true if the current test is a measurement119// FileName: the name of the file containing the current test120// LineNumber: the line number for the current test121// Failed: if the current test has failed, this will be true (useful in an AfterEach)122type GinkgoTestDescription struct {123 FullTestText string124 ComponentTexts []string125 TestText string126 IsMeasurement bool127 FileName string128 LineNumber int129 Failed bool130 Duration time.Duration131}132//CurrentGinkgoTestDescripton returns information about the current running test.133func CurrentGinkgoTestDescription() GinkgoTestDescription {134 summary, ok := globalSuite.CurrentRunningSpecSummary()135 if !ok {136 return GinkgoTestDescription{}137 }138 subjectCodeLocation := summary.ComponentCodeLocations[len(summary.ComponentCodeLocations)-1]139 return GinkgoTestDescription{140 ComponentTexts: summary.ComponentTexts[1:],141 FullTestText: strings.Join(summary.ComponentTexts[1:], " "),142 TestText: summary.ComponentTexts[len(summary.ComponentTexts)-1],143 IsMeasurement: summary.IsMeasurement,144 FileName: subjectCodeLocation.FileName,145 LineNumber: subjectCodeLocation.LineNumber,146 Failed: summary.HasFailureState(),147 Duration: summary.RunTime,148 }149}150//Measurement tests receive a Benchmarker.151//152//You use the Time() function to time how long the passed in body function takes to run153//You use the RecordValue() function to track arbitrary numerical measurements.154//The RecordValueWithPrecision() function can be used alternatively to provide the unit155//and resolution of the numeric measurement.156//The optional info argument is passed to the test reporter and can be used to157// provide the measurement data to a custom reporter with context.158//159//See http://onsi.github.io/ginkgo/#benchmark_tests for more details160type Benchmarker interface {161 Time(name string, body func(), info ...interface{}) (elapsedTime time.Duration)162 RecordValue(name string, value float64, info ...interface{})163 RecordValueWithPrecision(name string, value float64, units string, precision int, info ...interface{})164}165//RunSpecs is the entry point for the Ginkgo test runner.166//You must call this within a Golang testing TestX(t *testing.T) function.167//168//To bootstrap a test suite you can use the Ginkgo CLI:169//170// ginkgo bootstrap171func RunSpecs(t GinkgoTestingT, description string) bool {172 specReporters := []Reporter{buildDefaultReporter()}173 return RunSpecsWithCustomReporters(t, description, specReporters)174}175//To run your tests with Ginkgo's default reporter and your custom reporter(s), replace176//RunSpecs() with this method.177func RunSpecsWithDefaultAndCustomReporters(t GinkgoTestingT, description string, specReporters []Reporter) bool {178 specReporters = append(specReporters, buildDefaultReporter())179 return RunSpecsWithCustomReporters(t, description, specReporters)180}181//To run your tests with your custom reporter(s) (and *not* Ginkgo's default reporter), replace182//RunSpecs() with this method. Note that parallel tests will not work correctly without the default reporter183func RunSpecsWithCustomReporters(t GinkgoTestingT, description string, specReporters []Reporter) bool {184 writer := GinkgoWriter.(*writer.Writer)185 writer.SetStream(config.DefaultReporterConfig.Verbose)186 reporters := make([]reporters.Reporter, len(specReporters))187 for i, reporter := range specReporters {188 reporters[i] = reporter189 }190 passed, hasFocusedTests := globalSuite.Run(t, description, reporters, writer, config.GinkgoConfig)191 if passed && hasFocusedTests && strings.TrimSpace(os.Getenv("GINKGO_EDITOR_INTEGRATION")) == "" {192 fmt.Println("PASS | FOCUSED")193 os.Exit(types.GINKGO_FOCUS_EXIT_CODE)194 }195 return passed196}197func buildDefaultReporter() Reporter {198 remoteReportingServer := config.GinkgoConfig.StreamHost199 if remoteReportingServer == "" {200 stenographer := stenographer.New(!config.DefaultReporterConfig.NoColor, config.GinkgoConfig.FlakeAttempts > 1, colorable.NewColorableStdout())201 return reporters.NewDefaultReporter(config.DefaultReporterConfig, stenographer)202 } else {203 debugFile := ""204 if config.GinkgoConfig.DebugParallel {205 debugFile = fmt.Sprintf("ginkgo-node-%d.log", config.GinkgoConfig.ParallelNode)206 }207 return remote.NewForwardingReporter(config.DefaultReporterConfig, remoteReportingServer, &http.Client{}, remote.NewOutputInterceptor(), GinkgoWriter.(*writer.Writer), debugFile)208 }209}210//Skip notifies Ginkgo that the current spec was skipped.211func Skip(message string, callerSkip ...int) {212 skip := 0213 if len(callerSkip) > 0 {214 skip = callerSkip[0]215 }216 globalFailer.Skip(message, codelocation.New(skip+1))217 panic(GINKGO_PANIC)218}219//Fail notifies Ginkgo that the current spec has failed. (Gomega will call Fail for you automatically when an assertion fails.)220func Fail(message string, callerSkip ...int) {221 skip := 0222 if len(callerSkip) > 0 {223 skip = callerSkip[0]224 }225 globalFailer.Fail(message, codelocation.New(skip+1))226 panic(GINKGO_PANIC)227}228//GinkgoRecover should be deferred at the top of any spawned goroutine that (may) call `Fail`229//Since Gomega assertions call fail, you should throw a `defer GinkgoRecover()` at the top of any goroutine that230//calls out to Gomega231//232//Here's why: Ginkgo's `Fail` method records the failure and then panics to prevent233//further assertions from running. This panic must be recovered. Ginkgo does this for you234//if the panic originates in a Ginkgo node (an It, BeforeEach, etc...)235//236//Unfortunately, if a panic originates on a goroutine *launched* from one of these nodes there's no237//way for Ginkgo to rescue the panic. To do this, you must remember to `defer GinkgoRecover()` at the top of such a goroutine.238func GinkgoRecover() {239 e := recover()240 if e != nil {241 globalFailer.Panic(codelocation.New(1), e)242 }243}244//Describe blocks allow you to organize your specs. A Describe block can contain any number of245//BeforeEach, AfterEach, JustBeforeEach, It, and Measurement blocks.246//247//In addition you can nest Describe, Context and When blocks. Describe, Context and When blocks are functionally248//equivalent. The difference is purely semantic -- you typical Describe the behavior of an object249//or method and, within that Describe, outline a number of Contexts and Whens.250func Describe(text string, body func()) bool {251 globalSuite.PushContainerNode(text, body, types.FlagTypeNone, codelocation.New(1))252 return true253}254//You can focus the tests within a describe block using FDescribe255func FDescribe(text string, body func()) bool {256 globalSuite.PushContainerNode(text, body, types.FlagTypeFocused, codelocation.New(1))257 return true258}259//You can mark the tests within a describe block as pending using PDescribe260func PDescribe(text string, body func()) bool {261 globalSuite.PushContainerNode(text, body, types.FlagTypePending, codelocation.New(1))262 return true263}264//You can mark the tests within a describe block as pending using XDescribe265func XDescribe(text string, body func()) bool {266 globalSuite.PushContainerNode(text, body, types.FlagTypePending, codelocation.New(1))267 return true268}269//Context blocks allow you to organize your specs. A Context block can contain any number of270//BeforeEach, AfterEach, JustBeforeEach, It, and Measurement blocks.271//272//In addition you can nest Describe, Context and When blocks. Describe, Context and When blocks are functionally273//equivalent. The difference is purely semantic -- you typical Describe the behavior of an object274//or method and, within that Describe, outline a number of Contexts and Whens.275func Context(text string, body func()) bool {276 globalSuite.PushContainerNode(text, body, types.FlagTypeNone, codelocation.New(1))277 return true278}279//You can focus the tests within a describe block using FContext280func FContext(text string, body func()) bool {281 globalSuite.PushContainerNode(text, body, types.FlagTypeFocused, codelocation.New(1))282 return true283}284//You can mark the tests within a describe block as pending using PContext285func PContext(text string, body func()) bool {286 globalSuite.PushContainerNode(text, body, types.FlagTypePending, codelocation.New(1))287 return true288}289//You can mark the tests within a describe block as pending using XContext290func XContext(text string, body func()) bool {291 globalSuite.PushContainerNode(text, body, types.FlagTypePending, codelocation.New(1))292 return true293}294//When blocks allow you to organize your specs. A When block can contain any number of295//BeforeEach, AfterEach, JustBeforeEach, It, and Measurement blocks.296//297//In addition you can nest Describe, Context and When blocks. Describe, Context and When blocks are functionally298//equivalent. The difference is purely semantic -- you typical Describe the behavior of an object299//or method and, within that Describe, outline a number of Contexts and Whens.300func When(text string, body func()) bool {301 globalSuite.PushContainerNode("when "+text, body, types.FlagTypeNone, codelocation.New(1))302 return true303}304//You can focus the tests within a describe block using FWhen305func FWhen(text string, body func()) bool {306 globalSuite.PushContainerNode("when "+text, body, types.FlagTypeFocused, codelocation.New(1))307 return true308}309//You can mark the tests within a describe block as pending using PWhen310func PWhen(text string, body func()) bool {311 globalSuite.PushContainerNode("when "+text, body, types.FlagTypePending, codelocation.New(1))312 return true313}314//You can mark the tests within a describe block as pending using XWhen315func XWhen(text string, body func()) bool {316 globalSuite.PushContainerNode("when "+text, body, types.FlagTypePending, codelocation.New(1))317 return true318}319//It blocks contain your test code and assertions. You cannot nest any other Ginkgo blocks320//within an It block.321//322//Ginkgo will normally run It blocks synchronously. To perform asynchronous tests, pass a323//function that accepts a Done channel. When you do this, you can also provide an optional timeout.324func It(text string, body interface{}, timeout ...float64) bool {325 globalSuite.PushItNode(text, body, types.FlagTypeNone, codelocation.New(1), parseTimeout(timeout...))326 return true327}328//You can focus individual Its using FIt329func FIt(text string, body interface{}, timeout ...float64) bool {330 globalSuite.PushItNode(text, body, types.FlagTypeFocused, codelocation.New(1), parseTimeout(timeout...))331 return true332}333//You can mark Its as pending using PIt334func PIt(text string, _ ...interface{}) bool {335 globalSuite.PushItNode(text, func() {}, types.FlagTypePending, codelocation.New(1), 0)336 return true337}338//You can mark Its as pending using XIt339func XIt(text string, _ ...interface{}) bool {340 globalSuite.PushItNode(text, func() {}, types.FlagTypePending, codelocation.New(1), 0)341 return true342}343//Specify blocks are aliases for It blocks and allow for more natural wording in situations344//which "It" does not fit into a natural sentence flow. All the same protocols apply for Specify blocks345//which apply to It blocks.346func Specify(text string, body interface{}, timeout ...float64) bool {347 globalSuite.PushItNode(text, body, types.FlagTypeNone, codelocation.New(1), parseTimeout(timeout...))348 return true349}350//You can focus individual Specifys using FSpecify351func FSpecify(text string, body interface{}, timeout ...float64) bool {352 globalSuite.PushItNode(text, body, types.FlagTypeFocused, codelocation.New(1), parseTimeout(timeout...))353 return true354}355//You can mark Specifys as pending using PSpecify356func PSpecify(text string, is ...interface{}) bool {357 globalSuite.PushItNode(text, func() {}, types.FlagTypePending, codelocation.New(1), 0)358 return true359}360//You can mark Specifys as pending using XSpecify361func XSpecify(text string, is ...interface{}) bool {362 globalSuite.PushItNode(text, func() {}, types.FlagTypePending, codelocation.New(1), 0)363 return true364}365//By allows you to better document large Its.366//367//Generally you should try to keep your Its short and to the point. This is not always possible, however,368//especially in the context of integration tests that capture a particular workflow.369//370//By allows you to document such flows. By must be called within a runnable node (It, BeforeEach, Measure, etc...)371//By will simply log the passed in text to the GinkgoWriter. If By is handed a function it will immediately run the function.372func By(text string, callbacks ...func()) {373 preamble := "\x1b[1mSTEP\x1b[0m"374 if config.DefaultReporterConfig.NoColor {375 preamble = "STEP"376 }377 fmt.Fprintln(GinkgoWriter, preamble+": "+text)378 if len(callbacks) == 1 {379 callbacks[0]()380 }381 if len(callbacks) > 1 {382 panic("just one callback per By, please")383 }384}385//Measure blocks run the passed in body function repeatedly (determined by the samples argument)386//and accumulate metrics provided to the Benchmarker by the body function.387//388//The body function must have the signature:389// func(b Benchmarker)390func Measure(text string, body interface{}, samples int) bool {391 globalSuite.PushMeasureNode(text, body, types.FlagTypeNone, codelocation.New(1), samples)392 return true393}394//You can focus individual Measures using FMeasure395func FMeasure(text string, body interface{}, samples int) bool {396 globalSuite.PushMeasureNode(text, body, types.FlagTypeFocused, codelocation.New(1), samples)397 return true398}399//You can mark Maeasurements as pending using PMeasure400func PMeasure(text string, _ ...interface{}) bool {401 globalSuite.PushMeasureNode(text, func(b Benchmarker) {}, types.FlagTypePending, codelocation.New(1), 0)402 return true403}404//You can mark Maeasurements as pending using XMeasure405func XMeasure(text string, _ ...interface{}) bool {406 globalSuite.PushMeasureNode(text, func(b Benchmarker) {}, types.FlagTypePending, codelocation.New(1), 0)407 return true408}409//BeforeSuite blocks are run just once before any specs are run. When running in parallel, each410//parallel node process will call BeforeSuite.411//412//BeforeSuite blocks can be made asynchronous by providing a body function that accepts a Done channel413//414//You may only register *one* BeforeSuite handler per test suite. You typically do so in your bootstrap file at the top level.415func BeforeSuite(body interface{}, timeout ...float64) bool {416 globalSuite.SetBeforeSuiteNode(body, codelocation.New(1), parseTimeout(timeout...))417 return true418}419//AfterSuite blocks are *always* run after all the specs regardless of whether specs have passed or failed.420//Moreover, if Ginkgo receives an interrupt signal (^C) it will attempt to run the AfterSuite before exiting.421//422//When running in parallel, each parallel node process will call AfterSuite.423//424//AfterSuite blocks can be made asynchronous by providing a body function that accepts a Done channel425//426//You may only register *one* AfterSuite handler per test suite. You typically do so in your bootstrap file at the top level.427func AfterSuite(body interface{}, timeout ...float64) bool {428 globalSuite.SetAfterSuiteNode(body, codelocation.New(1), parseTimeout(timeout...))429 return true430}431//SynchronizedBeforeSuite blocks are primarily meant to solve the problem of setting up singleton external resources shared across432//nodes when running tests in parallel. For example, say you have a shared database that you can only start one instance of that433//must be used in your tests. When running in parallel, only one node should set up the database and all other nodes should wait434//until that node is done before running.435//436//SynchronizedBeforeSuite accomplishes this by taking *two* function arguments. The first is only run on parallel node #1. The second is437//run on all nodes, but *only* after the first function completes succesfully. Ginkgo also makes it possible to send data from the first function (on Node 1)438//to the second function (on all the other nodes).439//440//The functions have the following signatures. The first function (which only runs on node 1) has the signature:441//442// func() []byte443//444//or, to run asynchronously:445//446// func(done Done) []byte447//448//The byte array returned by the first function is then passed to the second function, which has the signature:449//450// func(data []byte)451//452//or, to run asynchronously:453//454// func(data []byte, done Done)455//456//Here's a simple pseudo-code example that starts a shared database on Node 1 and shares the database's address with the other nodes:457//458// var dbClient db.Client459// var dbRunner db.Runner460//461// var _ = SynchronizedBeforeSuite(func() []byte {462// dbRunner = db.NewRunner()463// err := dbRunner.Start()464// Ω(err).ShouldNot(HaveOccurred())465// return []byte(dbRunner.URL)466// }, func(data []byte) {467// dbClient = db.NewClient()468// err := dbClient.Connect(string(data))469// Ω(err).ShouldNot(HaveOccurred())470// })471func SynchronizedBeforeSuite(node1Body interface{}, allNodesBody interface{}, timeout ...float64) bool {472 globalSuite.SetSynchronizedBeforeSuiteNode(473 node1Body,474 allNodesBody,475 codelocation.New(1),476 parseTimeout(timeout...),477 )478 return true479}480//SynchronizedAfterSuite blocks complement the SynchronizedBeforeSuite blocks in solving the problem of setting up481//external singleton resources shared across nodes when running tests in parallel.482//483//SynchronizedAfterSuite accomplishes this by taking *two* function arguments. The first runs on all nodes. The second runs only on parallel node #1484//and *only* after all other nodes have finished and exited. This ensures that node 1, and any resources it is running, remain alive until485//all other nodes are finished.486//487//Both functions have the same signature: either func() or func(done Done) to run asynchronously.488//489//Here's a pseudo-code example that complements that given in SynchronizedBeforeSuite. Here, SynchronizedAfterSuite is used to tear down the shared database490//only after all nodes have finished:491//492// var _ = SynchronizedAfterSuite(func() {493// dbClient.Cleanup()494// }, func() {495// dbRunner.Stop()496// })497func SynchronizedAfterSuite(allNodesBody interface{}, node1Body interface{}, timeout ...float64) bool {498 globalSuite.SetSynchronizedAfterSuiteNode(499 allNodesBody,500 node1Body,501 codelocation.New(1),502 parseTimeout(timeout...),503 )504 return true505}506//BeforeEach blocks are run before It blocks. When multiple BeforeEach blocks are defined in nested507//Describe and Context blocks the outermost BeforeEach blocks are run first.508//509//Like It blocks, BeforeEach blocks can be made asynchronous by providing a body function that accepts510//a Done channel511func BeforeEach(body interface{}, timeout ...float64) bool {512 globalSuite.PushBeforeEachNode(body, codelocation.New(1), parseTimeout(timeout...))513 return true514}515//JustBeforeEach blocks are run before It blocks but *after* all BeforeEach blocks. For more details,516//read the [documentation](http://onsi.github.io/ginkgo/#separating_creation_and_configuration_)517//518//Like It blocks, BeforeEach blocks can be made asynchronous by providing a body function that accepts519//a Done channel520func JustBeforeEach(body interface{}, timeout ...float64) bool {521 globalSuite.PushJustBeforeEachNode(body, codelocation.New(1), parseTimeout(timeout...))522 return true523}524//JustAfterEach blocks are run after It blocks but *before* all AfterEach blocks. For more details,525//read the [documentation](http://onsi.github.io/ginkgo/#separating_creation_and_configuration_)526//527//Like It blocks, JustAfterEach blocks can be made asynchronous by providing a body function that accepts528//a Done channel529func JustAfterEach(body interface{}, timeout ...float64) bool {530 globalSuite.PushJustAfterEachNode(body, codelocation.New(1), parseTimeout(timeout...))531 return true532}533//AfterEach blocks are run after It blocks. When multiple AfterEach blocks are defined in nested534//Describe and Context blocks the innermost AfterEach blocks are run first.535//536//Like It blocks, AfterEach blocks can be made asynchronous by providing a body function that accepts537//a Done channel538func AfterEach(body interface{}, timeout ...float64) bool {539 globalSuite.PushAfterEachNode(body, codelocation.New(1), parseTimeout(timeout...))540 return true541}542func parseTimeout(timeout ...float64) time.Duration {543 if len(timeout) == 0 {544 return time.Duration(defaultTimeout * int64(time.Second))545 } else {546 return time.Duration(timeout[0] * float64(time.Second))547 }548}...
session_test.go
Source:session_test.go
...7 . "github.com/onsi/gomega/gexec"8 . "github.com/onsi/ginkgo"9 . "github.com/onsi/gomega"10)11var _ = Describe("Session", func() {12 var command *exec.Cmd13 var session *Session14 var outWriter, errWriter *Buffer15 BeforeEach(func() {16 outWriter = nil17 errWriter = nil18 })19 JustBeforeEach(func() {20 command = exec.Command(fireflyPath)21 var err error22 session, err = Start(command, outWriter, errWriter)23 Ω(err).ShouldNot(HaveOccurred())24 })25 Context("running a command", func() {26 It("should start the process", func() {27 Ω(command.Process).ShouldNot(BeNil())28 })29 It("should wrap the process's stdout and stderr with gbytes buffers", func(done Done) {30 Eventually(session.Out).Should(Say("We've done the impossible, and that makes us mighty"))31 Eventually(session.Err).Should(Say("Ah, curse your sudden but inevitable betrayal!"))32 defer session.Out.CancelDetects()33 select {34 case <-session.Out.Detect("Can we maybe vote on the whole murdering people issue"):35 Eventually(session).Should(Exit(0))36 case <-session.Out.Detect("I swear by my pretty floral bonnet, I will end you."):37 Eventually(session).Should(Exit(1))38 case <-session.Out.Detect("My work's illegal, but at least it's honest."):39 Eventually(session).Should(Exit(2))40 }41 close(done)42 })43 It("should satisfy the gbytes.BufferProvider interface, passing Stdout", func() {44 Eventually(session).Should(Say("We've done the impossible, and that makes us mighty"))45 Eventually(session).Should(Exit())46 })47 })48 Describe("providing the exit code", func() {49 It("should provide the app's exit code", func() {50 Ω(session.ExitCode()).Should(Equal(-1))51 Eventually(session).Should(Exit())52 Ω(session.ExitCode()).Should(BeNumerically(">=", 0))53 Ω(session.ExitCode()).Should(BeNumerically("<", 3))54 })55 })56 Describe("wait", func() {57 It("should wait till the command exits", func() {58 Ω(session.ExitCode()).Should(Equal(-1))59 Ω(session.Wait().ExitCode()).Should(BeNumerically(">=", 0))60 Ω(session.Wait().ExitCode()).Should(BeNumerically("<", 3))61 })62 })63 Describe("exited", func() {64 It("should close when the command exits", func() {65 Eventually(session.Exited).Should(BeClosed())66 Ω(session.ExitCode()).ShouldNot(Equal(-1))67 })68 })69 Describe("kill", func() {70 It("should kill the command and don't wait for it to exit", func() {71 session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)72 Ω(err).ShouldNot(HaveOccurred())73 session.Kill()74 Ω(session).ShouldNot(Exit(), "Should not exit immediately...")75 Eventually(session).Should(Exit(128 + 9))76 })77 })78 Describe("interrupt", func() {79 It("should interrupt the command", func() {80 session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)81 Ω(err).ShouldNot(HaveOccurred())82 session.Interrupt()83 Ω(session).ShouldNot(Exit(), "Should not exit immediately...")84 Eventually(session).Should(Exit(128 + 2))85 })86 })87 Describe("terminate", func() {88 It("should terminate the command", func() {89 session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)90 Ω(err).ShouldNot(HaveOccurred())91 session.Terminate()92 Ω(session).ShouldNot(Exit(), "Should not exit immediately...")93 Eventually(session).Should(Exit(128 + 15))94 })95 })96 Describe("signal", func() {97 It("should send the signal to the command", func() {98 session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)99 Ω(err).ShouldNot(HaveOccurred())100 session.Signal(syscall.SIGABRT)101 Ω(session).ShouldNot(Exit(), "Should not exit immediately...")102 Eventually(session).Should(Exit(128 + 6))103 })104 })105 Context("tracking sessions", func() {106 BeforeEach(func() {107 KillAndWait()108 })109 Describe("kill", func() {110 It("should kill all the started sessions", func() {111 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)112 Ω(err).ShouldNot(HaveOccurred())113 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)114 Ω(err).ShouldNot(HaveOccurred())115 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)116 Ω(err).ShouldNot(HaveOccurred())117 Kill()118 Eventually(session1).Should(Exit(128 + 9))119 Eventually(session2).Should(Exit(128 + 9))120 Eventually(session3).Should(Exit(128 + 9))121 })122 It("should not wait for exit", func() {123 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)124 Ω(err).ShouldNot(HaveOccurred())125 Kill()126 Ω(session1).ShouldNot(Exit(), "Should not exit immediately...")127 Eventually(session1).Should(Exit(128 + 9))128 })129 It("should not track unstarted sessions", func() {130 _, err := Start(exec.Command("does not exist", "10000000"), GinkgoWriter, GinkgoWriter)131 Ω(err).Should(HaveOccurred())132 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)133 Ω(err).ShouldNot(HaveOccurred())134 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)135 Ω(err).ShouldNot(HaveOccurred())136 Kill()137 Eventually(session2).Should(Exit(128 + 9))138 Eventually(session3).Should(Exit(128 + 9))139 })140 })141 Describe("killAndWait", func() {142 It("should kill all the started sessions and wait for them to finish", func() {143 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)144 Ω(err).ShouldNot(HaveOccurred())145 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)146 Ω(err).ShouldNot(HaveOccurred())147 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)148 Ω(err).ShouldNot(HaveOccurred())149 KillAndWait()150 Ω(session1).Should(Exit(128+9), "Should have exited")151 Ω(session2).Should(Exit(128+9), "Should have exited")152 Ω(session3).Should(Exit(128+9), "Should have exited")153 })154 })155 Describe("terminate", func() {156 It("should terminate all the started sessions", func() {157 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)158 Ω(err).ShouldNot(HaveOccurred())159 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)160 Ω(err).ShouldNot(HaveOccurred())161 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)162 Ω(err).ShouldNot(HaveOccurred())163 Terminate()164 Eventually(session1).Should(Exit(128 + 15))165 Eventually(session2).Should(Exit(128 + 15))166 Eventually(session3).Should(Exit(128 + 15))167 })168 It("should not wait for exit", func() {169 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)170 Ω(err).ShouldNot(HaveOccurred())171 Terminate()172 Ω(session1).ShouldNot(Exit(), "Should not exit immediately...")173 })174 })175 Describe("terminateAndWait", func() {176 It("should terminate all the started sessions, and wait for them to exit", func() {177 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)178 Ω(err).ShouldNot(HaveOccurred())179 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)180 Ω(err).ShouldNot(HaveOccurred())181 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)182 Ω(err).ShouldNot(HaveOccurred())183 TerminateAndWait()184 Ω(session1).Should(Exit(128+15), "Should have exited")185 Ω(session2).Should(Exit(128+15), "Should have exited")186 Ω(session3).Should(Exit(128+15), "Should have exited")187 })188 })189 Describe("signal", func() {190 It("should signal all the started sessions", func() {191 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)192 Ω(err).ShouldNot(HaveOccurred())193 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)194 Ω(err).ShouldNot(HaveOccurred())195 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)196 Ω(err).ShouldNot(HaveOccurred())197 Signal(syscall.SIGABRT)198 Eventually(session1).Should(Exit(128 + 6))199 Eventually(session2).Should(Exit(128 + 6))200 Eventually(session3).Should(Exit(128 + 6))201 })202 It("should not wait", func() {203 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)204 Ω(err).ShouldNot(HaveOccurred())205 Signal(syscall.SIGABRT)206 Ω(session1).ShouldNot(Exit(), "Should not exit immediately...")207 })208 })209 Describe("interrupt", func() {210 It("should interrupt all the started sessions, and not wait", func() {211 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)212 Ω(err).ShouldNot(HaveOccurred())213 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)214 Ω(err).ShouldNot(HaveOccurred())215 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)216 Ω(err).ShouldNot(HaveOccurred())217 Interrupt()218 Eventually(session1).Should(Exit(128 + 2))219 Eventually(session2).Should(Exit(128 + 2))220 Eventually(session3).Should(Exit(128 + 2))221 })222 It("should not wait", func() {223 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)224 Ω(err).ShouldNot(HaveOccurred())225 Interrupt()226 Ω(session1).ShouldNot(Exit(), "Should not exit immediately...")227 })228 })229 })230 Context("when the command exits", func() {231 It("should close the buffers", func() {232 Eventually(session).Should(Exit())233 Ω(session.Out.Closed()).Should(BeTrue())234 Ω(session.Err.Closed()).Should(BeTrue())235 Ω(session.Out).Should(Say("We've done the impossible, and that makes us mighty"))236 })237 var So = It238 So("this means that eventually should short circuit", func() {239 t := time.Now()240 failures := InterceptGomegaFailures(func() {241 Eventually(session).Should(Say("blah blah blah blah blah"))242 })243 Ω(time.Since(t)).Should(BeNumerically("<=", 500*time.Millisecond))244 Ω(failures).Should(HaveLen(1))245 })246 })247 Context("when wrapping out and err", func() {248 BeforeEach(func() {249 outWriter = NewBuffer()250 errWriter = NewBuffer()251 })252 It("should route to both the provided writers and the gbytes buffers", func() {253 Eventually(session.Out).Should(Say("We've done the impossible, and that makes us mighty"))254 Eventually(session.Err).Should(Say("Ah, curse your sudden but inevitable betrayal!"))255 Ω(outWriter.Contents()).Should(ContainSubstring("We've done the impossible, and that makes us mighty"))256 Ω(errWriter.Contents()).Should(ContainSubstring("Ah, curse your sudden but inevitable betrayal!"))257 Eventually(session).Should(Exit())258 Ω(outWriter.Contents()).Should(Equal(session.Out.Contents()))259 Ω(errWriter.Contents()).Should(Equal(session.Err.Contents()))260 })261 })262 Describe("when the command fails to start", func() {263 It("should return an error", func() {264 _, err := Start(exec.Command("agklsjdfas"), nil, nil)265 Ω(err).Should(HaveOccurred())266 })267 })268})...
builder_test.go
Source:builder_test.go
...5 . "github.com/onsi/gomega"6)7// Since focus would make the exit code to be non-zero,8// following testing is uneffective by default.9var _ = XDescribe("Focused(modify code to run effective testing)", func() {10 Context("FDescribe()", func() {11 var fdescribe = 012 NewGinkgoBuilder("ToFDescribe()").13 It("Sample It", func() {14 fdescribe = 115 }).16 ToFDescribe()17 It("Flag for FDescribe() should be 1", func() {18 Expect(fdescribe).To(Equal(1))19 })20 })21 /**22 * FContext()23 */24 var fcontext = 025 NewGinkgoBuilder("ToFContext()").26 It("Sample It", func() {27 fcontext = 128 }).29 ToFContext()30 It("Flag for FContext() should be 1", func() {31 Expect(fcontext).To(Equal(1))32 })33 // :~)34 Context("FIt()", func() {35 fit := 036 NewGinkgoBuilder("").37 FIt("For FIt", func() {38 fit = 139 }).40 Expose()41 It("Flag for FIt() should be set to 1", func() {42 Expect(fit).To(Equal(1))43 })44 })45 Context("FSpecify()", func() {46 fspecify := 047 NewGinkgoBuilder("").48 FSpecify("For FSpecify", func() {49 fspecify = 150 }).51 Expose()52 It("Flag for FSpecify() should be set to 1", func() {53 Expect(fspecify).To(Equal(1))54 })55 })56 Context("FMeasure()", func() {57 fmeasure := 058 NewGinkgoBuilder("").59 FMeasure("For FMeasure", func(b Benchmarker) {60 fmeasure = 161 }, 1).62 Expose()63 It("Flag for FMeasure() should be set to 1", func() {64 Expect(fmeasure).To(Equal(1))65 })66 })67})68var _ = Describe("GinkgoBuilder: For To?Describe()", func() {69 var describe = 070 NewGinkgoBuilder("ToDescribe()").71 It("Sample It", func() {72 describe = 173 }).74 ToDescribe()75 It("Flag for Describe() should be 1", func() {76 Expect(describe).To(Equal(1))77 })78 NewGinkgoBuilder("ToPDescribe()").79 // This should be pending80 It("Sample It", func() {81 Expect(1).To(Equal(2))82 }).83 ToPDescribe()84 NewGinkgoBuilder("ToXDescribe()").85 // This should be pending86 It("Sample It", func() {87 Expect(1).To(Equal(2))88 }).89 ToXDescribe()90})91var _ = Describe("GinkgoBuilder: For To?Context()", func() {92 var context = 093 NewGinkgoBuilder("ToContext()").94 It("Sample It", func() {95 context = 196 }).97 ToContext()98 It("Flag for Context() should be 1", func() {99 Expect(context).To(Equal(1))100 })101 NewGinkgoBuilder("ToPContext()").102 It("Sample It", func() {103 Expect(1).To(Equal(2))104 }).105 ToPContext()106 NewGinkgoBuilder("ToXContext()").107 It("Sample It", func() {108 Expect(1).To(Equal(2))109 }).110 ToXContext()111})112var _ = Describe("GinkgoBuilder: For building of testing function", func() {113 Context("?It related functions",114 func() {115 var (116 it = 0117 )118 NewGinkgoBuilder("").119 It("For It", func() {120 it = 1121 }).122 PIt("For PIt", func() {123 // This should be pending124 Expect(1).To(Equal(2))125 }).126 XIt("For XIt", func() {127 // This should be pending128 Expect(1).To(Equal(2))129 }).130 Expose()131 It("Flag for It() should be set to 1", func() {132 Expect(it).To(Equal(1))133 })134 },135 )136 Context("?Specify related functions",137 func() {138 var (139 specify = 0140 )141 NewGinkgoBuilder("").142 Specify("For Specify", func() {143 specify = 1144 }).145 PSpecify("For PSpecify", func() {146 // This should be pending147 Expect(1).To(Equal(2))148 }).149 XSpecify("For XSpecify", func() {150 // This should be pending151 Expect(1).To(Equal(2))152 }).153 Expose()154 It("Flag for Specify() should be set to 1", func() {155 Expect(specify).To(Equal(1))156 })157 },158 )159 Context("?Measure related functions",160 func() {161 var (162 measure = 0163 )164 NewGinkgoBuilder("").165 Measure("For Measure", func(b Benchmarker) {166 measure = 1167 }, 1).168 PMeasure("For PMeasure", func(b Benchmarker) {169 // This should be pending170 Expect(1).To(Equal(2))171 }, 1).172 XMeasure("For XMeasure", func(b Benchmarker) {173 // This should be pending174 Expect(1).To(Equal(2))175 }, 1).176 Expose()177 It("Flag for Measure() should be set to 1", func() {178 Expect(measure).To(Equal(1))179 })180 },181 )182})183var _ = Describe("GinkgoBuilder: For BeforeEach/AfterEach/JustBeforeEach", func() {184 var (185 justBeforeEach = 0186 beforeEach = 0187 afterEach = 0188 )189 NewGinkgoBuilder("").190 BeforeEach(func() {191 beforeEach++192 }).193 AfterEach(func() {194 afterEach++195 }).196 JustBeforeEach(func() {197 justBeforeEach++198 }).199 Expose()200 It("1st testing. BeforeEach()/JustBeforeEach() get called 1 time", func() {201 Expect(beforeEach).To(Equal(1))202 Expect(justBeforeEach).To(Equal(1))203 Expect(afterEach).To(Equal(0))204 })205 It("2nd testing. BeforeEach()/JustBeforeEach() get called 2 times. AfterEach() get called once", func() {206 Expect(beforeEach).To(Equal(2))207 Expect(justBeforeEach).To(Equal(2))208 Expect(afterEach).To(Equal(1))209 })210})211var _ = Describe("GinkgoBuilder: For BeforeFirst/AfterLast", func() {212 var (213 beforeCalled = 0214 afterCalled = 0215 )216 NewGinkgoBuilder("").217 BeforeFirst(func() {218 beforeCalled++219 }).220 AfterLast(func() {221 afterCalled++222 }).223 It("1st testing", func() {224 Expect(beforeCalled).To(Equal(1))225 Expect(afterCalled).To(Equal(0))226 }).227 It("2nd testing", func() {228 Expect(beforeCalled).To(Equal(1))229 Expect(afterCalled).To(Equal(0))230 }).231 Expose()232 It("BeforeFirst should get called only once", func() {233 Expect(beforeCalled).To(Equal(1))234 })235 It("AfterLast should get called only once", func() {236 Expect(afterCalled).To(Equal(1))237 })238})239var _ = Describe("GinkgoTable", func() {240 Context("Simple case", func() {241 counter := 0242 NewGinkgoTable().243 Exec(func(v int) {244 counter += v245 }).246 Exec(func(v int) {247 counter += v248 }).249 Case("case 1 for value 2", 2).250 Case(251 func(v int) string {252 return fmt.Sprintf("case 2 for value %d", v)253 }, 3,254 ).255 Expose()256 It("The final counter should be #case * #exec_body", func() {257 Expect(counter).To(Equal(10))258 })259 })260})261var _ = Describe("Use GinkgoTable with GinkgoBuilder", func() {262 var (263 beforeFirst = 0264 beforeEach = 0265 tableExec = 0266 externalExec = 0267 )268 NewGinkgoBuilder("Sample Context 1").269 BeforeFirst(func() {270 beforeFirst++271 }).272 BeforeEach(func() {273 beforeEach++274 }).275 Table(NewGinkgoTable()....
Describe
Using AI Code Generation
1import (2func main() {3 gomega.RegisterFailHandler(ginkgo.Fail)4 ginkgo.RunSpecs(t, "My Suite")5}6import (7func main() {8 gomega.RegisterFailHandler(ginkgo.Fail)9 ginkgo.RunSpecs(t, "My Suite")10}11import (12func main() {13 gomega.RegisterFailHandler(ginkgo.Fail)14 ginkgo.RunSpecs(t, "My Suite")15}16import (17func main() {18 gomega.RegisterFailHandler(ginkgo.Fail)19 ginkgo.RunSpecs(t, "My Suite")20}21import (22func main() {23 gomega.RegisterFailHandler(ginkgo.Fail)24 ginkgo.RunSpecs(t, "My Suite")25}26import (27func main() {28 gomega.RegisterFailHandler(ginkgo.Fail)29 ginkgo.RunSpecs(t, "My Suite")30}31import (32func main() {33 gomega.RegisterFailHandler(ginkgo.Fail)
Describe
Using AI Code Generation
1import (2func Test1(t *testing.T) {3 RegisterFailHandler(Fail)4 RunSpecs(t, "Test suite")5}6var _ = Describe("Test1", func() {7 Context("Test1", func() {8 It("Test1", func() {9 Expect(1).To(Equal(1))10 })11 })12})13import (14func Test2(t *testing.T) {15 RegisterFailHandler(Fail)16 RunSpecs(t, "Test suite")17}18var _ = Describe("Test2", func() {19 Context("Test2", func() {20 It("Test2", func() {21 Expect(1).To(Equal(1))22 })23 })24})25import (26func Test3(t *testing.T) {27 RegisterFailHandler(Fail)28 RunSpecs(t, "Test suite")29}30var _ = Describe("Test3", func() {31 Context("Test3", func() {32 It("Test3", func() {33 Expect(1).To(Equal(1))34 })35 })36})37import (38func Test4(t *testing.T) {39 RegisterFailHandler(Fail)40 RunSpecs(t, "Test suite")41}42var _ = Describe("Test4", func() {43 Context("Test4", func() {44 It("Test4", func() {45 Expect(1).To(Equal(1))46 })47 })48})49import (
Describe
Using AI Code Generation
1import (2func main() {3 ginkgo.Describe("Test", func() {4 fmt.Println("Test")5 })6}7import (8func main() {9 ginkgo.Describe("Test", func() {10 fmt.Println("Test")11 })12}13import (14func main() {15 ginkgo.Describe("Test", func() {16 ginkgo.It("Test2", func() {17 fmt.Println("Test2")18 })19 })20}21import (22func main() {23 ginkgo.Describe("Test", func() {24 ginkgo.It("Test2", func() {25 fmt.Println("Test2")26 })27 ginkgo.It("Test3", func() {28 fmt.Println("Test3")29 })30 })31}32import (33func main() {34 ginkgo.Describe("Test", func() {35 ginkgo.It("Test2", func() {36 fmt.Println("Test2")37 })38 ginkgo.It("Test3", func() {39 fmt.Println("Test3")40 })41 ginkgo.It("Test4", func() {42 fmt.Println("Test4")43 })44 })45}46import (47func main() {48 ginkgo.Describe("Test", func() {49 ginkgo.It("Test2", func() {50 fmt.Println("Test2")51 })52 ginkgo.It("Test3", func() {53 fmt.Println("Test3")54 })55 ginkgo.It("Test4", func() {
Describe
Using AI Code Generation
1import (2func main() {3ginkgo.Describe("Test", func() {4ginkgo.It("Should print hello world", func() {5gomega.Expect("Hello World").To(gomega.Equal("Hello World"))6})7})8}9import (10func main() {11ginkgo.Describe("Test", func() {12ginkgo.It("Should print hello world", func() {13gomega.Expect("Hello World").To(gomega.Equal("Hello World"))14})15})16}17import (18func main() {19ginkgo.Describe("Test", func() {20ginkgo.It("Should print hello world", func() {21gomega.Expect("Hello World").To(gomega.Equal("Hello World"))22})23})24}25import (26func main() {27ginkgo.Describe("Test", func() {28ginkgo.It("Should print hello world", func() {29gomega.Expect("Hello World").To(gomega.Equal("Hello World"))30})31})32}33import (34func main() {35ginkgo.Describe("Test", func() {36ginkgo.It("Should print hello world", func() {37gomega.Expect("Hello World").To(gomega.Equal("Hello World"))38})39})40}41import (42func main() {43ginkgo.Describe("Test", func() {44ginkgo.It("Should print hello world", func() {45gomega.Expect("Hello World").To(gomega.Equal("Hello World"))46})47})48}49import (50func main() {51ginkgo.Describe("Test", func() {
Describe
Using AI Code Generation
1Describe("Test", func() {2 It("Should pass", func() {3 Expect("Hello").To(Equal("Hello"))4 })5})6Describe("Test", func() {7 It("Should pass", func() {8 Expect("Hello").To(Equal("Hello"))9 })10})11Describe("Test", func() {12 It("Should pass", func() {13 Expect("Hello").To(Equal("Hello"))14 })15})16Describe("Test", func() {17 It("Should pass", func() {18 Expect("Hello").To(Equal("Hello"))19 })20})21Describe("Test", func() {22 It("Should pass", func() {23 Expect("Hello").To(Equal("Hello"))24 })25})26Describe("Test", func() {27 It("Should pass", func() {28 Expect("Hello").To(Equal("Hello"))29 })30})31Describe("Test", func() {32 It("Should pass", func() {33 Expect("Hello").To(Equal("Hello"))34 })35})36Describe("Test", func() {37 It("Should pass", func() {38 Expect("Hello").To(Equal("Hello"))39 })40})41Describe("Test", func() {42 It("Should pass", func() {43 Expect("Hello").To(Equal("Hello"))44 })45})46Describe("Test", func() {47 It("Should pass", func() {48 Expect("Hello").To(Equal("Hello"))49 })50})51Describe("Test", func() {52 It("Should pass", func() {53 Expect("Hello").To(Equal("Hello"))54 })55})
Describe
Using AI Code Generation
1Describe("Describe method of ginkgo", func() {2 It("It method of ginkgo", func() {3 Expect(1).Should(Equal(1))4 })5})6It("It method of ginkgo", func() {7 Expect(1).Should(Equal(1))8})9Expect(1).Should(Equal(1))10Expect(1).Should(Equal(1))11Expect(1).Should(Equal(1))12Expect(1).Should(Equal(1))13Expect(true).Should(BeTrue())14Expect(false).Should(BeFalse())15Expect(nil).Should(BeNil())16Expect([]int{}).Should(BeEmpty())17Expect(0).Should(BeZero())18Expect([]int{1,2,3}).Should(HaveLen(3))19Expect([]int{1,2,3}).Should(ContainElement(1))20Expect([]int{1,2,3}).Should(ContainElements(1,2))21Expect("Hello World").Should(ContainSubstring("Hello"))22Expect("Hello World").Should(MatchRegexp("Hello"))23Expect(1).Should(BeNumerically(">", 0))24Expect(make(chan int)).Should(BeClosed())25Expect(make(chan int)).Should(BeSent(1))26Expect(make(chan int)).Should(Receive())
Describe
Using AI Code Generation
1Describe("Test suite name", func() {2 It("Test case name", func() {3 Expect("actual value").To(Equal("expected value"))4 })5})6Describe("Test suite name", func() {7 It("Test case name", func() {8 Expect("actual value").To(Equal("expected value"))9 })10})11Describe("Test suite name", func() {12 It("Test case name", func() {13 Expect("actual value").To(Equal("expected value"))14 })15})16Describe("Test suite name", func() {17 It("Test case name", func() {18 Expect("actual value").To(Equal("expected value"))19 })20})21Describe("Test suite name", func() {22 It("Test case name", func() {23 Expect("actual value").To(Equal("expected value"))24 })25})26Describe("Test suite name", func() {27 It("Test case name", func() {28 Expect("actual value").To(Equal("expected value"))29 })30})31Describe("Test suite name", func() {32 It("Test case name", func() {
Describe
Using AI Code Generation
1Describe("Test for ginkgo", func() {2 It("should be able to describe the test", func() {3 Expect(1).To(Equal(1))4 })5})6Describe("Test for ginkgo", func() {7 It("should be able to describe the test", func() {8 Expect(1).To(Equal(1))9 })10})11Describe("Test for ginkgo", func() {12 It("should be able to describe the test", func() {13 Expect(1).To(Equal(1))14 })15})16Describe("Test for ginkgo", func() {17 It("should be able to describe the test", func() {18 Expect(1).To(Equal(1))19 })20})21Describe("Test for ginkgo", func() {22 It("should be able to describe the test", func() {23 Expect(1).To(Equal(1))24 })25})26Describe("Test for ginkgo", func() {27 It("should be able to describe the test", func() {28 Expect(1).To(Equal(1))29 })30})31Describe("Test for ginkgo", func() {32 It("should be able to describe the test", func() {33 Expect(1).To(Equal(1))34 })35})36Describe("Test for ginkgo", func() {37 It("should be able to describe the test", func() {38 Expect(1).To(Equal(1))39 })40})41Describe("Test for ginkgo", func() {42 It("should be able to describe the test", func() {43 Expect(1).To(Equal(1))44 })45})
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!