Categorygithub.com/onsi/ginkgo/v2
modulepackage
2.23.0
Repository: https://github.com/onsi/ginkgo.git
Documentation: pkg.go.dev

# README

Ginkgo

test | Ginkgo Docs


Ginkgo

Ginkgo is a mature testing framework for Go designed to help you write expressive specs. Ginkgo builds on top of Go's testing foundation and is complemented by the Gomega matcher library. Together, Ginkgo and Gomega let you express the intent behind your specs clearly:

import (
    . "github.com/onsi/ginkgo/v2"
    . "github.com/onsi/gomega"
    ...
)

var _ = Describe("Checking books out of the library", Label("library"), func() {
    var library *libraries.Library
    var book *books.Book
    var valjean *users.User
    BeforeEach(func() {
        library = libraries.NewClient()
        book = &books.Book{
            Title: "Les Miserables",
            Author: "Victor Hugo",
        }
        valjean = users.NewUser("Jean Valjean")
    })

    When("the library has the book in question", func() {
        BeforeEach(func(ctx SpecContext) {
            Expect(library.Store(ctx, book)).To(Succeed())
        })

        Context("and the book is available", func() {
            It("lends it to the reader", func(ctx SpecContext) {
                Expect(valjean.Checkout(ctx, library, "Les Miserables")).To(Succeed())
                Expect(valjean.Books()).To(ContainElement(book))
                Expect(library.UserWithBook(ctx, book)).To(Equal(valjean))
            }, SpecTimeout(time.Second * 5))
        })

        Context("but the book has already been checked out", func() {
            var javert *users.User
            BeforeEach(func(ctx SpecContext) {
                javert = users.NewUser("Javert")
                Expect(javert.Checkout(ctx, library, "Les Miserables")).To(Succeed())
            })

            It("tells the user", func(ctx SpecContext) {
                err := valjean.Checkout(ctx, library, "Les Miserables")
                Expect(err).To(MatchError("Les Miserables is currently checked out"))
            }, SpecTimeout(time.Second * 5))

            It("lets the user place a hold and get notified later", func(ctx SpecContext) {
                Expect(valjean.Hold(ctx, library, "Les Miserables")).To(Succeed())
                Expect(valjean.Holds(ctx)).To(ContainElement(book))

                By("when Javert returns the book")
                Expect(javert.Return(ctx, library, book)).To(Succeed())

                By("it eventually informs Valjean")
                notification := "Les Miserables is ready for pick up"
                Eventually(ctx, valjean.Notifications).Should(ContainElement(notification))

                Expect(valjean.Checkout(ctx, library, "Les Miserables")).To(Succeed())
                Expect(valjean.Books(ctx)).To(ContainElement(book))
                Expect(valjean.Holds(ctx)).To(BeEmpty())
            }, SpecTimeout(time.Second * 10))
        })  
    })

    When("the library does not have the book in question", func() {
        It("tells the reader the book is unavailable", func(ctx SpecContext) {
            err := valjean.Checkout(ctx, library, "Les Miserables")
            Expect(err).To(MatchError("Les Miserables is not in the library catalog"))
        }, SpecTimeout(time.Second * 5))
    })
})

Jump to the docs to learn more. It's easy to bootstrap and start writing your first specs.

If you have a question, comment, bug report, feature request, etc. please open a GitHub issue, or visit the Ginkgo Slack channel.

Capabilities

Whether writing basic unit specs, complex integration specs, or even performance specs - Ginkgo gives you an expressive Domain-Specific Language (DSL) that will be familiar to users coming from frameworks such as Quick, RSpec, Jasmine, and Busted. This style of testing is sometimes referred to as "Behavior-Driven Development" (BDD) though Ginkgo's utility extends beyond acceptance-level testing.

With Ginkgo's DSL you can use nestable Describe, Context and When container nodes to help you organize your specs. BeforeEach and AfterEach setup nodes for setup and cleanup. It and Specify subject nodes that hold your assertions. BeforeSuite and AfterSuite nodes to prep for and cleanup after a suite... and much more!.

At runtime, Ginkgo can run your specs in reproducibly random order and has sophisticated support for spec parallelization. In fact, running specs in parallel is as easy as

ginkgo -p

By following established patterns for writing parallel specs you can build even large, complex integration suites that parallelize cleanly and run performantly. And you don't have to worry about your spec suite hanging or leaving a mess behind - Ginkgo provides a per-node context.Context and the capability to interrupt the spec after a set period of time - and then clean up.

As your suites grow Ginkgo helps you keep your specs organized with labels and lets you easily run subsets of specs, either programmatically or on the command line. And Ginkgo's reporting infrastructure generates machine-readable output in a variety of formats and allows you to build your own custom reporting infrastructure.

Ginkgo ships with ginkgo, a command line tool with support for generating, running, filtering, and profiling Ginkgo suites. You can even have Ginkgo automatically run your specs when it detects a change with ginkgo watch, enabling rapid feedback loops during test-driven development.

And that's just Ginkgo! Gomega brings a rich, mature, family of assertions and matchers to your suites. With Gomega you can easily mix synchronous and asynchronous assertions in your specs. You can even build your own set of expressive domain-specific matchers quickly and easily by composing Gomega's existing building blocks.

Happy Testing!

License

Ginkgo is MIT-Licensed

Contributing

See CONTRIBUTING.md

# Packages

Ginkgo's Default Reporter A number of command line flags are available to tweak Ginkgo's default output.

# Functions

AbortSuite instructs Ginkgo to fail the current spec and skip all subsequent specs, thereby aborting the suite.
AddReportEntry generates and adds a new ReportEntry to the current spec's SpecReport.
AfterAll nodes are Setup nodes that can occur inside Ordered containers.
AfterEach nodes are Setup nodes whose closures run after It node closures.
AfterSuite nodes are suite-level Setup nodes run after all specs have finished - regardless of whether specs have passed or failed.
AttachProgressReporter allows you to register a function that will be called whenever Ginkgo generates a Progress Report.
BeforeAll nodes are Setup nodes that can occur inside Ordered containers.
BeforeEach nodes are Setup nodes whose closures run before It node closures.
BeforeSuite nodes are suite-level Setup nodes that run just once before any specs are run.
By allows you to better document complex Specs.
Deprecated: CurrentGinkgoTestDescription has been replaced with CurrentSpecReport.
CurrentSpecReport returns information about the current running spec.
DeferCleanup can be called within any Setup or Subject node to register a cleanup callback that Ginkgo will call at the appropriate time to cleanup after the spec.
Describe nodes are Container nodes that allow you to organize your specs.
DescribeTable describes a table-driven spec.
DescribeTableSubtree describes a table-driven spec that generates a set of tests for each entry.
Entry
Entry constructs a TableEntry.
Fail notifies Ginkgo that the current spec has failed.
FDescribe focuses specs within the Describe block.
You can focus a table with `FDescribeTable`.
You can focus a table with `FDescribeTableSubtree`.
You can focus a particular entry with FEntry.
FIt allows you to focus an individual It.
When is an alias for Describe - it generates the exact same kind of Container node */.
GinkgoConfiguration returns the configuration of the current suite.
GinkgoHelper marks the function it's called in as a test helper.
GinkgoLabelFilter() returns the label filter configured for this suite via `--label-filter`.
Deprecated: GinkgoParallelNode() has been renamed to GinkgoParallelProcess() */.
GinkgoParallelProcess returns the parallel process number for the current ginkgo process The process number is 1-indexed.
GinkgoRandomSeed returns the seed used to randomize spec execution order.
GinkgoRecover should be deferred at the top of any spawned goroutine that (may) call `Fail` Since Gomega assertions call fail, you should throw a `defer GinkgoRecover()` at the top of any goroutine that calls out to Gomega Here's why: Ginkgo's `Fail` method records the failure and then panics to prevent further assertions from running.
GinkgoT() implements an interface that allows third party libraries to integrate with and build on top of Ginkgo.
GinkgoTB() implements a wrapper that exactly matches the testing.TB interface.
It nodes are Subject nodes that contain your spec code and assertions.
JustAfterEach nodes are similar to AfterEach nodes, however they are guaranteed to run *before* all AfterEach node closures - just after the It node closure.
JustBeforeEach nodes are similar to BeforeEach nodes, however they are guaranteed to run *after* all BeforeEach node closures - just before the It node closure.
Label decorates specs with Labels.
Deprecated: Measure() has been removed from Ginkgo 2.0 Use Gomega's gmeasure package instead.
PauseOutputInterception() pauses Ginkgo's output interception.
PDescribe marks specs within the Describe block as pending.
You can mark a table as pending with `PDescribeTable`.
You can mark a table as pending with `PDescribeTableSubtree`.
You can mark a particular entry as pending with PEntry.
PIt allows you to mark an individual It as pending.
PreviewSpecs walks the testing tree and produces a report without actually invoking the specs.
When is an alias for Describe - it generates the exact same kind of Container node */.
ReportAfterEach nodes are run for each spec, even if the spec is skipped or pending.
ReportAfterSuite nodes are run at the end of the suite.
ReportBeforeEach nodes are run for each spec, even if the spec is skipped or pending.
ReportBeforeSuite nodes are run at the beginning of the suite.
ResumeOutputInterception() - see docs for PauseOutputInterception().
RunSpecs is the entry point for the Ginkgo spec runner.
Deprecated: Custom Reporters have been removed in Ginkgo 2.0.
Deprecated: Custom Reporters have been removed in Ginkgo 2.0.
Skip instructs Ginkgo to skip the current spec You can call Skip in any Setup or Subject node closure.
SynchronizedAfterSuite nodes complement the SynchronizedBeforeSuite nodes in solving the problem of splitting clean up into a piece that runs on all processes and a piece that must only run once - on process #1.
SynchronizedBeforeSuite nodes allow you to perform some of the suite setup just once - on parallel process #1 - and then pass information from that setup to the rest of the suite setup on all processes.
When is an alias for Describe - it generates the exact same kind of Container node */.

# Constants

ContinueOnFailure is a decorator that allows you to mark an Ordered container to continue running specs even if failures occur.
Focus is a decorator that allows you to mark a spec or container as focused.
OncePerOrdered is a decorator that allows you to mark outer BeforeEach, AfterEach, JustBeforeEach, and JustAfterEach setup nodes to run once per ordered context.
Ordered is a decorator that allows you to mark a container as ordered.
Pending is a decorator that allows you to mark a spec or container as pending.
Serial is a decorator that allows you to mark a spec or container as serial.
SuppressProgressReporting is a decorator that allows you to disable progress reporting of a particular node.

# Variables

Context is an alias for Describe - it generates the exact same kind of Container node */.
Context is an alias for Describe - it generates the exact same kind of Container node */.
Specify is an alias for It - it can allow for more natural wording in some context.
GinkgoLogr is a logr.Logger that writes to GinkgoWriter */.
GinkgoWriter implements a GinkgoWriterInterface and io.Writer When running in verbose mode (ginkgo -v) any writes to GinkgoWriter will be immediately printed to stdout.
Context is an alias for Describe - it generates the exact same kind of Container node */.
Specify is an alias for It - it can allow for more natural wording in some context.
Specify is an alias for It - it can allow for more natural wording in some context.
Context is an alias for Describe - it generates the exact same kind of Container node */.
XDescribe marks specs within the Describe block as pending.
You can mark a table as pending with `XDescribeTable`.
You can mark a table as pending with `XDescribeTableSubtree`.
You can mark a particular entry as pending with XEntry.
XIt allows you to mark an individual It as pending.
Specify is an alias for It - it can allow for more natural wording in some context.

# Structs

Deprecated: GinkgoTestDescription has been replaced with SpecReport.
TableEntry represents an entry in a table test.

# Interfaces

Deprecated: Benchmarker has been removed from Ginkgo 2.0 Use Gomega's gmeasure package instead.
Additional methods returned by GinkgoT() that provide deeper integration points into Ginkgo */.
The interface by which Ginkgo receives *testing.T.
The portion of the interface returned by GinkgoT() that maps onto methods in the testing package's T.
The interface implemented by GinkgoWriter.

# Type aliases

Deprecated: Done Channel for asynchronous testing The Done channel pattern is no longer supported in Ginkgo 2.0.
The EntryDescription decorator allows you to pass a format string to DescribeTable() and Entry().
FlakeAttempts(uint N) is a decorator that allows you to mark individual specs or spec containers as flaky.
GracePeriod denotes the period of time Ginkgo will wait for an interruptible node to exit once an interruption (whether due to a timeout or a user-invoked signal) has occurred.
Labels are the type for spec Label decorators.
MustPassRepeatedly(uint N) is a decorator that allows you to repeat the execution of individual specs or spec containers.
NodeTimeout allows you to specify a timeout for an indivdiual node.
Offset(uint) is a decorator that allows you to change the stack-frame offset used when computing the line number of the node in question.
PollProgressAfter allows you to override the configured value for --poll-progress-after for a particular node.
PollProgressInterval allows you to override the configured value for --poll-progress-interval for a particular node.
Report represents the report for a Suite.
ReportEntryVisibility governs the visibility of ReportEntries in Ginkgo's console reporter - ReportEntryVisibilityAlways: the default behavior - the ReportEntry is always emitted.
Deprecated: Custom Ginkgo test reporters are deprecated in Ginkgo 2.0.
SpecContext is the context object passed into nodes that are subject to a timeout or need to be notified of an interrupt.
Report represents the report for a Spec.
SpecTimeout allows you to specify a timeout for an indivdiual spec.