我不明白为什么'go'找不到我的银杏测试文件
这是我的结构的外观:
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())
})
})
})
请注意,我已经特别写了一个测试,以使其失败。
但是每次我运行银杏测试时,我都会收到以下错误
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
很明显,我在这里缺少一些东西。
有任何线索?
进行events_test
目录并运行:
ginkgo bootstrap
这是银杏撰写的第一个测试文档:
要为包装编写银杏测试,您必须首先引导银杏测试套件。说您有一个名为书籍的包裹:
$ cd path/to/books $ ginkgo bootstrap
ahillman3的建议对于正常测试有效,但是如果您使用银杏进行测试,则不适用。
我发现文档有点令人困惑,并且在撰写本文时不会使用GO Mod,因此我将分享我正在使用的最小设置。为简单起见,所有文件都在root项目目录中。
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而言,一切都是相同的。
您有一些问题。
- 您不是导入测试软件包。这应该在Ginkgo生成的Bootstrap文件中。
- Bootstrap文件也应作为参数包括testing.t函数。例如
(t *testing.T)
。 - 看起来您在银杏过程中跳过了一两个步骤,导致先前的依赖性未存在。例如bootstrap/stub。
此外,经过几个人的很多评论。您可能需要阅读Ginkgo文档,以确保您正确遵循其过程以正确设置测试。