-
-
Notifications
You must be signed in to change notification settings - Fork 663
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
extensions/globals
package (#692)
this package can be used to reset the global state of ginkgo
- Loading branch information
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Package `globals` provides an interface to alter the global state of ginkgo suite. | ||
// | ||
// ginkgo currently registers a few singleton global vars that hold all the | ||
// test blocks and failure management. These vars are global per package, which means | ||
// that only one Suite definition can coexist in one package. | ||
// | ||
// However, there can be some use cases where applications using ginkgo may want to | ||
// have a bit more control about this. For instance, a package may be using ginkgo | ||
// to dynamically generate different tests and groups depending on some configuration. | ||
// In this particular case, if the application wants to test how these different groups | ||
// are generated, they will need access to change these global variables, so they | ||
// can re-generate this global state, and ensure that different configuration generate | ||
// indeed different tests. | ||
// | ||
// Note that this package is not intended to be used as part of normal ginkgo setups, and | ||
// usually, you will never need to worry about the global state of ginkgo | ||
package globals | ||
|
||
import "github.com/onsi/ginkgo/internal/global" | ||
|
||
// Reset calls `global.InitializeGlobals()` which will basically create a new instance | ||
// of Suite, and therefore, will effectively reset the global variables to the init state. | ||
// This will effectively remove all groups, tests and blocks that were added to the Suite. | ||
func Reset() { | ||
global.InitializeGlobals() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package globals_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
"github.com/onsi/ginkgo/extensions/globals" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestGlobals(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
|
||
// define some vars to store how many times a test has been run | ||
var ( | ||
testI = 0 | ||
testII = 0 | ||
) | ||
|
||
// Define a simple gingko test I | ||
var _ = Describe("ginkgo test I", func() { | ||
It("build tests I", func() { | ||
testI++ | ||
Ω(testI).Should(Equal(1)) | ||
}) | ||
}) | ||
|
||
RunSpecs(t, "Test Runner Suite I") | ||
|
||
// reset the global state of ginkgo. test I should now be removed, and it | ||
// won't run twice. | ||
globals.Reset() | ||
|
||
// Define a simple gingko test II | ||
var _ = Describe("ginkgo test II", func() { | ||
It("build tests II", func() { | ||
testII++ | ||
Ω(testII).Should(Equal(1)) | ||
}) | ||
}) | ||
|
||
RunSpecs(t, "Test Runner Suite II") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters