Comments (6)
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 Before
s and It
s 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.
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.
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.
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.
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.
Please add your thoughts to #70 @tzvatot.
from ginkgo.
Related Issues (20)
- ginkgo refuses to run pre-compiled test binary on Windows HOT 10
- Release Binaries HOT 1
- Doesn't show failed test case when -p option is set. HOT 1
- Grace period timeout occurred in ReportAfterSuite() HOT 4
- [Bug] File Handler Leak HOT 3
- WASM fails to compile due to ginkgo dsl
- Using label-filter to execute non labeled tests HOT 2
- Proposal: new decorador FlakeBackoff for more fine-grained control over flaked tests HOT 3
- How to build ginkgo in an intranet environment
- Tests that have not run end up in SpecStateSkipped in after suite report HOT 6
- Ginkgohelper does not work well with Eventually HOT 2
- Could not process Coverprofile for multi-module project HOT 1
- feature proposal: WhenTable HOT 2
- ginkgo outline doesn't honor --nodot option HOT 2
- explain cause of context cancelation HOT 2
- Don't skip tests that failed to compile HOT 1
- Merging coverage profiles leads lots of duplicated lines and uses lots of memory HOT 2
- GinkgoT: does not skip internal code by default HOT 1
- GinkoT().Error[f]: record error instead of aborting the test HOT 2
- Support for KeyValue atttributes in specs HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ginkgo.