Skip to content

Question/Feature Request: Ability to run a BeforeEach for ALL specs in a project #1629

Open

Description

@pablocalvo-zh

We have multiple Describe suites, that live in separate files in the same project. For each one of them we have added a BeforeEach that helps us skip tests if they have previously run and were successful (a way to speed up our testing pipelines).

// this is mandatory when checking skipped test
// the spec hash is helpful to debug and manually skip tests
BeforeEach(func(specContext SpecContext) {
	e2eSuite.SetupSpec(specContext, specContext.SpecReport(), true)
})

We have the collaboration problem that some engineers write new files and forget to include this block, and we have multiple projects like these. So I am wondering if there is a way to run one BEFORE EACH for all Its within the same project, so we can write it only once..

Activity

  1. onsi commented on Dec 29, 2025

    @onsi
    Owner

    Hey there - yes, if you put the BeforeEach at the top level of any of the files in the testing package (or any library imported by the testing package) it will register as a BeforeEach that runs below any descendant specs (which will be all the specs):

    package foo_test
    
    import . "github.com/onsi/ginkgo/v2"
    
    
    var _ = BeforeEach(func(specContext SpecContext) {
    	e2eSuite.SetupSpec(specContext, specContext.SpecReport(), true)
    })
    
    ...etc
    

    Note the var _ = .... Go does not allow bare function invocations at the top level but it does allow invocations that lead to assignments. In this case we simply ignore the assignment with var _.

  2. pablocalvo-zh commented on Jan 13, 2026

    @pablocalvo-zh
    Author

    Yeah but we have about 20 files per suite. I wanted to avoid re-writing it everytime. I wanted to see if there was a 1 time thing..

  3. onsi commented on Jan 13, 2026

    @onsi
    Owner

    You only need to call it once. In one file (typically the X_suite_test.go file - it will run at the top level before every spec in the suite

  4. csirmazbendeguz commented on Mar 23, 2026

    @csirmazbendeguz

    Hi! I found this via Google as I'm trying to do something similar - instead of skipping tests, I am setting up a gRPC server for integration tests. I'm storing the server in a global variable like this:

    var (
    	server     *grpc.Server
    	clientConn *grpc.ClientConn
    )
    
    var _ = ginkgo.BeforeEach(func() {
    	server, clientConn = startServer()
    })
    
    var _ = ginkgo.AfterEach(func() {
    	err := clientConn.Close()
    	if err != nil {
    		ginkgo.GinkgoWriter.Print(err)
    	}
    
    	server.Stop()
    })

    I don't think this is thread-safe though and I would like to run the suite with parallel execution. What's the best way to achieve this in Ginkgo? I am aware of BeforeSuite / SynchronizedBeforeSuite but they run once per suite while I'm trying to run once per spec

  5. onsi commented on Mar 23, 2026

    @onsi
    Owner

    hey @csirmazbendeguz what you've got will work just fine. When you invoke ginkgo -p to run on parallel ginkgo spins up and orchestrates multiple processes. no shared memory - no thread safety issues:

    https://onsi.github.io/ginkgo/#spec-parallelization

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions