4

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?

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
Viren
  • 5,812
  • 6
  • 45
  • 98
  • 3
    The output says you have no tests to run, so there isn't anything to fail. Reasoning: [Why does `go test -run NotExist` pass?](https://stackoverflow.com/questions/38463609/why-does-go-test-run-notexist-pass/38466246#38466246) Tests should be in the same package being tested. Read package doc of [testing](https://golang.org/pkg/testing/). – icza Jun 27 '17 at 14:26
  • 1
    Have you ran ginkgo bootstrap? Can you see the generated function similar to: `func TestEvents(t *testing.T)`? – dev.bmax Jun 27 '17 at 14:35
  • put `button_not_shown_event_test.go` in the same dir as `button_not_shown_event.go` and give it the same package name. – RickyA Jun 27 '17 at 14:37
  • and on top of that put an actual test in the file like dev.bmax explains. – RickyA Jun 27 '17 at 14:39
  • @Viren have you done the `ginkgo bootstrap` according to the Ginkgo docs? – Adrian Jun 27 '17 at 15:21

3 Answers3

3

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.

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
3

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.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
1

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.

ahillman3
  • 976
  • 7
  • 18
  • OP is using Ginkgo which deviates drastically from the normal go testing usage - and normal Go syntax and idioms in general, for that matter. – Adrian Jun 27 '17 at 14:54
  • True. But, I went and looked at the Ginkgo docs, and they still use the testing package. – ahillman3 Jun 27 '17 at 15:09
  • They do use the testing package, but only in a stub generated by a tool they provide. You don't import the testing package into your ginkgo BDD test files. – Adrian Jun 27 '17 at 15:11
  • They also recommend *against* putting your tests in the same package with the code under test; they recommend black-box tests using `_test` packages, as the OP has done. – Adrian Jun 27 '17 at 15:12
  • I'll edit my answer to remove the same package item. But, the OP is using the go test command, and according to what I'm reading in the Ginkgo docs, it won't work without the testing package. – ahillman3 Jun 27 '17 at 15:16
  • It's right [on their homepage](http://onsi.github.io/ginkgo/): you use `ginkgo bootstrap` to create the stub file, which imports `testing`. You write your actual specs in a separate file that does not. – Adrian Jun 27 '17 at 15:19
  • Edited. I'm going to go read the Ginkgo docs and see if there is anything there I can use in my tests. – ahillman3 Jun 27 '17 at 15:26