Created
July 22, 2026 15:48
-
-
Save palaniraja/2038c34e3a7a2de3ddbaea6fa446cecf to your computer and use it in GitHub Desktop.
mini-ginkgo - go test -v
This file contains hidden or 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
| module example.com/mini-ginkgo | |
| go 1.26.1 |
This file contains hidden or 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
| package miniginkgo | |
| import ( | |
| "fmt" | |
| "testing" | |
| ) | |
| // TestFunc represents setup or test code. | |
| type TestFunc func() | |
| // Spec is one registered test case. | |
| type Spec struct { | |
| description string | |
| body TestFunc | |
| beforeEach []TestFunc | |
| } | |
| // These act as Ginkgo's internal registry/state. | |
| var ( | |
| registeredSpecs []Spec | |
| activeBeforeEaches []TestFunc | |
| ) | |
| // Describe groups specs. | |
| // | |
| // It immediately calls body() during Go package initialization. | |
| // The body registers BeforeEach and It definitions. | |
| func Describe(description string, body TestFunc) bool { | |
| fmt.Printf("[REGISTER] Describe: %q\n", description) | |
| // Save the parent Describe scope, allowing nested Describes. | |
| parentBeforeEaches := activeBeforeEaches | |
| // This executes NOW, during registration. | |
| body() | |
| // Restore parent state after leaving this Describe block. | |
| activeBeforeEaches = parentBeforeEaches | |
| return true | |
| } | |
| // BeforeEach registers setup code. | |
| // | |
| // It does NOT execute the setup now. | |
| func BeforeEach(body TestFunc) { | |
| fmt.Println("[REGISTER] BeforeEach") | |
| activeBeforeEaches = append(activeBeforeEaches, body) | |
| } | |
| // It registers one executable spec. | |
| // | |
| // It does NOT execute the test body now. | |
| func It(description string, body TestFunc) { | |
| fmt.Printf("[REGISTER] It: %q\n", description) | |
| // Copy current setup steps into this individual spec. | |
| // This is how an It receives the BeforeEach functions that apply to it. | |
| spec := Spec{ | |
| description: description, | |
| body: body, | |
| beforeEach: append([]TestFunc(nil), activeBeforeEaches...), | |
| } | |
| registeredSpecs = append(registeredSpecs, spec) | |
| } | |
| // RunSpecs is the simplified equivalent of Ginkgo's RunSpecs. | |
| // | |
| // It runs every registered It. For each one: | |
| // 1. Run BeforeEach functions | |
| // 2. Run the It body | |
| func RunSpecs(t *testing.T) { | |
| t.Helper() | |
| for _, spec := range registeredSpecs { | |
| spec := spec // safe closure capture | |
| t.Run(spec.description, func(t *testing.T) { | |
| fmt.Printf("\n[RUN] It: %q\n", spec.description) | |
| for index, setup := range spec.beforeEach { | |
| fmt.Printf("[RUN] BeforeEach %d\n", index+1) | |
| setup() | |
| } | |
| fmt.Println("[RUN] It body") | |
| spec.body() | |
| }) | |
| } | |
| } | |
| // ------------------------------------------------------------ | |
| // SPEC REGISTRATION | |
| // ------------------------------------------------------------ | |
| // | |
| // This is the key Ginkgo-style pattern: | |
| // | |
| // var _ = Describe(...) | |
| // | |
| // Go evaluates package-level variable initializers BEFORE it calls | |
| // TestMiniGinkgo. Describe therefore registers all It specs first. | |
| var _ = Describe("Calculator", func() { | |
| fmt.Println("[DESCRIBE BODY] This runs during registration") | |
| var calculatorName string | |
| BeforeEach(func() { | |
| fmt.Println(" [SETUP] Create a fresh calculator") | |
| calculatorName = "calculator-created-by-BeforeEach" | |
| }) | |
| It("adds two numbers", func() { | |
| fmt.Printf(" [TEST] Using: %s\n", calculatorName) | |
| actual := 2 + 3 | |
| expected := 5 | |
| if actual != expected { | |
| panic(fmt.Sprintf("expected %d, got %d", expected, actual)) | |
| } | |
| fmt.Printf(" [TEST] 2 + 3 = %d\n", actual) | |
| }) | |
| It("multiplies two numbers", func() { | |
| fmt.Printf(" [TEST] Using: %s\n", calculatorName) | |
| actual := 4 * 5 | |
| expected := 20 | |
| if actual != expected { | |
| panic(fmt.Sprintf("expected %d, got %d", expected, actual)) | |
| } | |
| fmt.Printf(" [TEST] 4 * 5 = %d\n", actual) | |
| }) | |
| Describe("when special mode is enabled", func() { | |
| BeforeEach(func() { | |
| fmt.Println(" [SETUP] Enable special mode") | |
| }) | |
| It("has both outer and inner setup", func() { | |
| fmt.Println(" [TEST] Run test with calculator and special mode") | |
| }) | |
| }) | |
| }) | |
| // ------------------------------------------------------------ | |
| // GO TEST ENTRY POINT | |
| // ------------------------------------------------------------ | |
| // | |
| // This is the only function Go itself discovers and calls. | |
| // Ginkgo uses this same pattern: | |
| // | |
| // func TestXxx(t *testing.T) { | |
| // RegisterFailHandler(Fail) | |
| // RunSpecs(t, "My Suite") | |
| // } | |
| func TestMiniGinkgo(t *testing.T) { | |
| fmt.Println("\n========== GO TEST EXECUTION ==========") | |
| fmt.Println("[GO TEST] TestMiniGinkgo was called by go test") | |
| RunSpecs(t) | |
| } |
This file contains hidden or 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
| go test -v | |
| [REGISTER] Describe: "Calculator" | |
| [DESCRIBE BODY] This runs during registration | |
| [REGISTER] BeforeEach | |
| [REGISTER] It: "adds two numbers" | |
| [REGISTER] It: "multiplies two numbers" | |
| [REGISTER] Describe: "when special mode is enabled" | |
| [REGISTER] BeforeEach | |
| [REGISTER] It: "has both outer and inner setup" | |
| === RUN TestMiniGinkgo | |
| ========== GO TEST EXECUTION ========== | |
| [GO TEST] TestMiniGinkgo was called by go test | |
| === RUN TestMiniGinkgo/adds_two_numbers | |
| [RUN] It: "adds two numbers" | |
| [RUN] BeforeEach 1 | |
| [SETUP] Create a fresh calculator | |
| [RUN] It body | |
| [TEST] Using: calculator-created-by-BeforeEach | |
| [TEST] 2 + 3 = 5 | |
| === RUN TestMiniGinkgo/multiplies_two_numbers | |
| [RUN] It: "multiplies two numbers" | |
| [RUN] BeforeEach 1 | |
| [SETUP] Create a fresh calculator | |
| [RUN] It body | |
| [TEST] Using: calculator-created-by-BeforeEach | |
| [TEST] 4 * 5 = 20 | |
| === RUN TestMiniGinkgo/has_both_outer_and_inner_setup | |
| [RUN] It: "has both outer and inner setup" | |
| [RUN] BeforeEach 1 | |
| [SETUP] Create a fresh calculator | |
| [RUN] BeforeEach 2 | |
| [SETUP] Enable special mode | |
| [RUN] It body | |
| [TEST] Run test with calculator and special mode | |
| --- PASS: TestMiniGinkgo (0.00s) | |
| --- PASS: TestMiniGinkgo/adds_two_numbers (0.00s) | |
| --- PASS: TestMiniGinkgo/multiplies_two_numbers (0.00s) | |
| --- PASS: TestMiniGinkgo/has_both_outer_and_inner_setup (0.00s) | |
| PASS | |
| ok example.com/mini-ginkgo 0.186s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment