【问题标题】:Ginkgo tests not being found?银杏测试没有被发现?
【发布时间】:2021-09-12 10:31:05
【问题描述】:

我不明白为什么 '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

很明显,我在这里遗漏了一些东西。

有什么线索吗?

【问题讨论】:

  • 输出表明您没有要运行的测试,因此没有任何失败。推理:Why does go test -run NotExist pass? 测试应该在被测试的同一个包中。阅读testing的包文档。
  • 你运行过银杏引导程序吗?你能看到生成的函数类似于:func TestEvents(t *testing.T)吗?
  • button_not_shown_event_test.gobutton_not_shown_event.go 放在同一个目录中,并赋予它相同的包名。
  • 然后像 dev.bmax 解释的那样在文件中进行实际测试。
  • @Viren 你根据 Ginkgo 文档完成了ginkgo bootstrap 吗?

标签: go ginkgo gomega


【解决方案1】:

转到events_test 目录并运行:

ginkgo bootstrap

这是银杏的writing your first test docs

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

$ cd path/to/books
$ ginkgo bootstrap

ahillman3 的建议适用于正常测试,但如果您使用 Ginkgo 进行测试,则不适用。

【讨论】:

    【解决方案2】:

    你有一些问题。

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

    另外,经过几个人的很多cmet。您可能需要阅读 Ginkgo 文档,以确保您正确遵循他们的流程以正确设置测试。

    【讨论】:

    • OP 正在使用 Ginkgo,它与正常的 Go 测试用法和一般的 Go 语法和习语大相径庭。
    • 是的。但是,我去看了 Ginkgo 文档,他们仍然使用测试包。
    • 他们确实使用了测试包,但仅在他们提供的工具生成的存根中。您不会将测试包导入到您的 ginkgo BDD 测试文件中。
    • 他们还建议反对将您的测试与被测代码放在同一个包中;他们建议使用 _test 包进行黑盒测试,就像 OP 所做的那样。
    • 我将编辑我的答案以删除相同的包裹项目。但是,OP 正在使用 go test 命令,根据我在 Ginkgo 文档中阅读的内容,如果没有测试包,它将无法工作。
    【解决方案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,一切都是一样的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-29
      • 2022-11-29
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      相关资源
      最近更新 更多