Ginkgo tests not being found?

6.8k views Asked by At

I do not understand why 'go' cannot find my Ginkgo test files

Here's how my structure looks:

events
├── button_not_shown_event.go
├── events_test
│   └── button_not_shown_event_test.go

And here how my button_not_shown_event_test.go look like

package events_test

import (
    "fmt"
    . "github.com/onsi/ginkgo"
    . "github.com/onsi/gomega"
)

var _ = Describe("ButtonNotShownEvent", func() {
  BeforeEach(func() {
    Expect(false).To(BeTrue())
  })
  
  Context("ButtonNotShownEvent.GET()", func() {
        It("should not return a JSONify string", func() {
           Expect(true).To(BeFalse())
        })
    })
})

Notice I have specifically written a test so that it will fail.

But every time I run the Ginkgo test I get the following error

go test ./app/events/events_test/button_not_shown_event_test.go  -v

testing: warning: no tests to run
PASS
ok      command-line-arguments  1.027s

So clearly there is something I'm missing over here.

Any clue?

3

There are 3 answers

7
ahillman3 On BEST ANSWER

You have a few issues.

  1. You aren't importing the testing package. This should be in the bootstrap file generated by Ginkgo.
  2. The bootstrap file should also include as a parameter the testing.T function. e.g. (t *testing.T).
  3. It looks like you skipped a step or two in the Ginkgo process, resulting in a prior dependency not existing. e.g. the bootstrap/stub.

Additionally, after a lot of comments by several people. You likely need to read the Ginkgo docs, to be sure you are following their process properly to get your tests setup properly.

0
ggorlen On

I found the docs a bit confusing to follow, and they don't use a go mod at the time of writing, so I'll share the minimal setup that I'm using. For simplicity, all files are in the root project directory.

adder.go:

package adder

func Add(a, b int) int {
    return a + b
}

adder_test.go:

package adder_test

import (
    . "github.com/onsi/ginkgo"
    . "github.com/onsi/gomega"
    . "example.com/adder"
)

var _ = Describe("Adder", func() {
    It("should add", func() {
        Expect(Add(1, 2)).To(Equal(3))
    })
})

adder_suite_test.go:

package adder_test

import (
    . "github.com/onsi/ginkgo"
    . "github.com/onsi/gomega"
    "testing"
)

func TestAdder(t *testing.T) {
    RegisterFailHandler(Fail)
    RunSpecs(t, "Adder Suite")
}

Now run go mod init example.com/adder; go mod tidy:

PS > go version
go version go1.17.1 windows/amd64
PS > go mod init example.com/adder
go: creating new go.mod: module example.com/adder
go: to add module requirements and sums:
        go mod tidy
PS > go mod tidy
go: finding module for package github.com/onsi/gomega
go: finding module for package github.com/onsi/ginkgo
go: found github.com/onsi/ginkgo in github.com/onsi/ginkgo v1.16.4
go: found github.com/onsi/gomega in github.com/onsi/gomega v1.16.0

Finally, run go test:

Running Suite: Adder Suite
==========================
Random Seed: 1631413901
Will run 1 of 1 specs

+
Ran 1 of 1 Specs in 0.042 seconds
SUCCESS! -- 1 Passed | 0 Failed | 0 Pending | 0 Skipped
PASS
ok      example.com/adder       0.310s

Everything is the same for Linux.

0
Abe Miessler On

Go the the events_test directory and run:

ginkgo bootstrap

This is from Ginkgo's writing your first test docs:

To write Ginkgo tests for a package you must first bootstrap a Ginkgo test suite. Say you have a package named books:

$ cd path/to/books
$ ginkgo bootstrap

ahillman3's advice is valid for normal testing but if you are testing with Ginkgo it does not apply.