Skip to content

Instantly share code, notes, and snippets.

@smoser
Created June 27, 2022 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smoser/1ba00536f9b06d9ae544c7d8f62f205a to your computer and use it in GitHub Desktop.
Save smoser/1ba00536f9b06d9ae544c7d8f62f205a to your computer and use it in GitHub Desktop.
Simple test of using ginkgo test suite with go

Example ginkgo test

Setup

Setup for this was like:

$ mkdir superdude
$ cd superdude
$ go mod init example.com/superdude
$ go get -u github.com/onsi/ginkgo/v2
$ go get -u "github.com/onsi/gomega"

# now put that in go bin
$ go install -mod=mod github.com/onsi/ginkgo/v2/ginkgo

Running tests

You can run tests with go tests just like:

$ go test -v

Or, with the ginkgo binary:

$ ginkgo 

Notes

  • Interesting that superdude_test.go is package superdude, while the superdude_suite_test.go is package superdude_test. I'm not sure how the TestSuperdude method in superdude_suite_test.go has gets a list of the Describeed tests. It probably has something to do with the fact that Describe is being run at initialization time (to set the _ variable).
module example.com/superdude
go 1.18
require (
github.com/onsi/ginkgo/v2 v2.1.4 // indirect
github.com/onsi/gomega v1.19.0 // indirect
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/onsi/ginkgo/v2 v2.1.4 h1:GNapqRSid3zijZ9H77KrgVG4/8KqiyRsxcSxe+7ApXY=
github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU=
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e h1:TsQ7F31D3bUCLeqPT0u+yjp1guoArKaNKmCr22PYgTQ=
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 h1:rHZQSjJdAI4Xf5Qzeh2bBc5YJIkPFVM6oDtMFYmgws0=
golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
package superdude_test
import (
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestSuperdude(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Adder Suite")
}
package superdude
import (
"fmt"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("String Length", func() {
DescribeTable("Check Len works",
func(s string, num int) {
Expect(len(s)).To(Equal(num))
},
Entry("my name", "smoser", 6),
Entry("your name", "tristan", 7),
)
})
var _ = Describe("Greetings", func() {
DescribeTable("Say Hi",
func(s string) {
Expect(fmt.Sprintf("Hi, %s", s)).To(Equal("Hi, " + s))
},
Entry("my name", "smoser"),
Entry("your name", "tristan"),
)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment