银杏测试找不到?

huangapple go评论77阅读模式
英文:

Ginkgo tests not being found?

问题

我不明白为什么 'go' 找不到我的 Ginkgo 测试文件。

这是我的目录结构:

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

这是我的 button_not_shown_event_test.go 文件的内容:

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())
        })
    })
})

请注意,我特意编写了一个测试用例,以便它会失败。

但是每次运行 Ginkgo 测试时,我都会得到以下错误:

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

所以显然我在这里漏掉了什么。

有什么线索吗?

英文:

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?

答案1

得分: 3

我发现文档有点难以理解,并且在撰写时没有使用go mod,所以我将分享我正在使用的最简设置。为了简单起见,所有文件都在根项目目录中。

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")
}

现在运行 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

最后,运行 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

Linux 环境下一切都是相同的。

英文:

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.

答案2

得分: 2

请执行以下操作:

  1. 进入events_test目录。

  2. 运行以下命令:

    ginkgo bootstrap
    

这段内容来自Ginkgo的编写第一个测试文档

要为一个包编写Ginkgo测试,首先必须引导一个Ginkgo测试套件。假设你有一个名为books的包:

$ cd path/to/books
$ ginkgo bootstrap

ahillman3的建议适用于普通测试,但如果你正在使用Ginkgo进行测试,那么不适用。

英文:

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.

答案3

得分: 1

你有几个问题。

  1. 你没有导入 testing 包。这应该在 Ginkgo 生成的引导文件中。
  2. 引导文件还应该将 testing.T 函数作为参数包含进去。例如 (t *testing.T)
  3. 看起来你在 Ginkgo 过程中跳过了一两个步骤,导致之前的依赖项不存在。例如引导/存根。

此外,根据几个人的评论,你可能需要阅读 Ginkgo 文档,确保你正确地按照他们的流程设置测试。

英文:

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.

huangapple
  • 本文由 发表于 2017年6月27日 22:24:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/44782807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定