onsi / ginkgo

A Modern Testing Framework for Go
http://onsi.github.io/ginkgo/
MIT License
8.12k stars 644 forks source link

Behavior difference between calling the test using "ginkgo" and go test #1294

Closed bjeevan-ib closed 8 months ago

bjeevan-ib commented 8 months ago

ginkgo/v2 newbie question:

I am calling Describe from within a TestAdd function and it seems to behave differently when I call it with ginkgo and go test.

Here is the code:

package controllers
import (
    "testing"

    . "github.com/onsi/ginkgo/v2"
    . "github.com/onsi/gomega"
)
func Add(a, b int) int {
    return a + b
}
func TestAdd(t *testing.T) {
    Describe("Add()", func() {
        It("should add two positive numbers correctly", func() {
            result := Add(1, 1)
            Expect(result).To(Equal(3))
        })

    })
}

Running it with "ginkgo" fails as expected:

ginkgo
Running Suite: IntegrationTest Suite - /Users/bjeevan/git/github.com/Infobloxopen/db-controller/controllers/integration-test
============================================================================================================================
Random Seed: 1698951041

Will run 1 of 1 specs
------------------------------
• [FAILED] [0.000 seconds]
Add() [It] should add two positive numbers correctly
/Users/bjeevan/git/github.com/Infobloxopen/db-controller/controllers/integration-test/dbc_test.go:16

  [FAILED] Expected
      <int>: 2
  to equal
      <int>: 3
  In [It] at: /Users/bjeevan/git/github.com/Infobloxopen/db-controller/controllers/integration-test/dbc_test.go:18 @ 11/02/23 11:50:42.449
------------------------------

Summarizing 1 Failure:
  [FAIL] Add() [It] should add two positive numbers correctly
  /Users/bjeevan/git/github.com/Infobloxopen/db-controller/controllers/integration-test/dbc_test.go:18

Ran 1 of 1 Specs in 0.001 seconds
FAIL! -- 0 Passed | 1 Failed | 0 Pending | 0 Skipped
--- FAIL: TestIntegrationTest (0.00s)
FAIL

Ginkgo ran 1 suite in 1.285063101s

Test Suite Failed

While running it using go test passes (without actually executing the describe funtion)

go test ./ -run TestAdd -count 1
ok      github.com/infobloxopen/db-controller/controllers/integration-test  0.188s

What is wrong in the way I am using go test? (please let me know if there is a better place to ask this question)

onsi commented 8 months ago

hey there,

You shouldn't put anything other than RunSpecs in a TestX function. The Describe should be at the top level, no in a TestX function. It will be registered in Ginkgo's suite when the test program begins and then run by Ginkgo.

~I suspect the difference you saw between ginkgo and go test is spurious and a red herring (in one case the TestAdd function was called fore RunSpecs, in the other it was called after).~ (sorry i just looked at your post more closely)

The issue you are seeing is that by invoking go test and targeting the specific add test you are only registering the Describe but not also invoking RunSpecs. If you'd like to focus a particular test Ginkgo has lots of mechanisms for that.

Happy to add more context if you'd like, but if you drop the surrounding TestAdd your tests should behave as expected. I'd also suggest skimming the first sections of the Ginkgo docs to learn more about how Ginkgo tests are structured.

bjeevan-ib commented 8 months ago

hey there,

You shouldn't put anything other than RunSpecs in a TestX function. The Describe should be at the top level, no in a TestX function. It will be registered in Ginkgo's suite when the test program begins and then run by Ginkgo.

~I suspect the difference you saw between ginkgo and go test is spurious and a red herring (in one case the TestAdd function was called fore RunSpecs, in the other it was called after).~ (sorry i just looked at your post more closely)

The issue you are seeing is that by invoking go test and targeting the specific add test you are only registering the Describe but not also invoking RunSpecs. If you'd like to focus a particular test Ginkgo has lots of mechanisms for that.

Happy to add more context if you'd like, but if you drop the surrounding TestAdd your tests should behave as expected. I'd also suggest skimming the first sections of the Ginkgo docs to learn more about how Ginkgo tests are structured.

Thank you for the quick response.