Giter Club home page Giter Club logo

Comments (6)

onsi avatar onsi commented on May 18, 2024 3

I ran into this myself and also thought "does Ginkgo need a beforeAll block"? Then I realized:

func TestBooks(t *testing.T) {
    RegisterFailHandler(Fail)

    //BEFORE ALL
    RunSpecs(t, "Books Suite")
    //AFTER ALL
}

Here's how I share global state:

var dbSession *DBSession

func TestBooks(t *testing.T) {
    RegisterFailHandler(Fail)

    dbSession = DBSession.New(...)
    err := dbSession.Connect()
    Ω(err).ShouldNot(HaveOccured())
    RunSpecs(t, "Books Suite")
    dbSession.Disconnect()
}

Now your Its have access to the global dbSession and, because Ginkgo will only call your Befores and Its during RunSpecs you can trust that the dbSession is initialized.

There's one more trick here. If you want to reset the DB between each test you can do this in your bootstrap file:

var dbSession *DBSession

func TestBooks(t *testing.T) {
    RegisterFailHandler(Fail)

    dbSession = DBSession.New(...)
    err := dbSession.Connect()
    Ω(err).ShouldNot(HaveOccured())
    RunSpecs(t, "Books Suite")
    dbSession.Disconnect()
}

var _ = BeforeEach(func() {
    err := dbSession.Reset()
    Ω(err).ShouldNot(HaveOccured())
})

This is a top-level BeforeEach that will run before every test.

One last pattern: when I have a lot of infrastructure for a test suite (e.g. an integration test suite that manages multiple external processes) I wrap all that stuff into a separate mothership/provider object and initialize/reset that object in the boostrap but then pull things off of that object throughout my various tests. This keeps the bootstrap file small and centralizes all the infrastructure management to a separate object/package (which can now be shared among tests).

from ginkgo.

onsi avatar onsi commented on May 18, 2024

oops.. didn't mean to close the issue -- i'll let you do that @sporto if you think the approach I outlined above is sufficient.

from ginkgo.

sporto avatar sporto commented on May 18, 2024

Cool this works, thanks.

There is a subtle mistake I ran into and I thought this was not working (just writing it here for future reference):

var dbSession *DBSession

func TestBooks(t *testing.T) {
        RegisterFailHandler(Fail)

    dbSession, err := DBSession.New(...)
    if err != nil {
        ...
    }
    RunSpecs(t, "Books Suite")
}

In this case I shadowed the variable dbSession without thinking about it because I needed to get err in the same line, so then dbSession was nil in my tests.

from ginkgo.

onsi avatar onsi commented on May 18, 2024

Yep... I think I run into this one three times a day ;)

Sent from my iPad

On Nov 22, 2013, at 5:52 PM, Sebastian Porto [email protected] wrote:

Cool this works, thanks.

There is a subtle mistake I ran into and I thought this was not working (just writing it here for future reference):

var dbSession *DBSession

func TestBooks(t *testing.T) {
RegisterFailHandler(Fail)

dbSession, err := DBSession.New(...)
if err != nil {
    ...
}
RunSpecs(t, "Books Suite")

}
In this case I shadowed the variable dbSession without thinking about it because I needed to get err in the same line, so then dbSession was nil in my tests.


Reply to this email directly or view it on GitHub.

from ginkgo.

tzvatot avatar tzvatot commented on May 18, 2024

I would really like to have beforeAll() that will run once before all the It it cover.
The reason is that you sometime group together several specs and they all can share a common state, but I don't want that state to be global to the suite: it's not serving all the specs.

from ginkgo.

williammartin avatar williammartin commented on May 18, 2024

Please add your thoughts to #70 @tzvatot.

from ginkgo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.