当您运行`go test`时,为什么这个函数未定义?

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

Why is this function undefined when I run `go test`?

问题

这是代码:

a.go

package main

import "fmt"

func Haha() {
    fmt.Println("在 Haha 函数中")
}

func main() {
}

a_test.go

package main_test

import "testing"

func TestA(t *testing.T) {
    Haha()
}

当我运行 go build 时,它可以正常工作。但是当我运行 ~/gopath/src/zjk/misc$ go test -v 时,我得到以下错误信息:

# zjk/misc_test
./a_test.go:6: undefined: Haha
FAIL	zjk/misc [build failed]
英文:

Here is the code

a.go

package main

import "fmt"

func Haha() {
	fmt.Println("in Haha")
}

func main() {
}

a_test.go

package main_test

import "testing"

func TestA(t *testing.T) {
	Haha()
}

go build works. But when I run ~/gopath/src/zjk/misc$ go test -v. Here is what I get

# zjk/misc_test
./a_test.go:6: undefined: Haha
FAIL	zjk/misc [build failed]

答案1

得分: 4

你需要在main_test包中导入main并像这样调用它:main.Haha()

为了详细说明为什么有些测试需要放在不同的包中,我应该说有两类测试:

  • 第一类是监督包的实现、内部机制的测试,以确保其完整性、资源使用情况和性能基准等。这些测试应该与包代码本身放在一起。
  • 第二类是测试包的功能和用法的测试。在这些测试中,我们希望确保包能够实现其所提供的服务。这些测试的一个重要方面是确保我们不会暴露任何不必要的细节,以确保私有部分保持私有,公共API清晰明了。因此,这些测试应该放在另一个包中,使用不同的名称,作为被测试包的消费者。

唯一的例外是main包,它不应该从其他包中使用,所以我们只需将所有测试写在该包内即可(如@kostya所评论的)。

英文:

You need to import "main" in main_test package & call it like main.Haha().

Just to elaborate why one might have the tests for a package under a different package, I should say there are two categories of tests:

  • First, those tests that supervise the implementation of a package, the internals, to assure it's integrity, resource usage profile and performance benchmarks and the like. These tests should reside alongside the package code itself.
  • Second are those that test the functionality and usage of the package. In these tests, we want to assure a package stands up to it's claims about the service it provides. An important aspect of these tests is that they assure that we are not exposing any unnecessary details; to ensure private parts remain private and public API is crystal clear. That's why these tests should reside in another package, under a different name, to act as a consumer of the package under the test.

One exception is package main which is not meant to be used from inside other packages, so we just write all tests inside the package itself (as @kostya commented).

答案2

得分: 3

因为你有不同的包,应该是这样的:

a.go

package main

import "fmt"

func Haha() {
    fmt.Println("in Haha")
}

func main() {
}

a_test.go

package main

import "testing"

func TestA(t *testing.T) {
    Haha()
}

输出:

# zjk/misc_test
in Haha
PASS
ok  	github.com/qwertmax/so	0.009s
英文:

because you have different packages

should be:

a.go

package main

import "fmt"

func Haha() {
	fmt.Println("in Haha")
}

func main() {
}

a_test.go

package main

import "testing"

func TestA(t *testing.T) {
    Haha()
}

output:

# zjk/misc_test
in Haha
PASS
ok  	github.com/qwertmax/so	0.009s

答案3

得分: 1

你的测试位于名为main_test的包中,而函数位于名为main的包中。你没有导入main包,所以无法引用它。

如果你的测试不能在main包中,将该函数移动到第三个包中。

英文:

Your test is in a package called main_test and the function is in a package called main. You are not importing main, so there's no way to address it.

If your test can't be in package main, move the function to a third package.

huangapple
  • 本文由 发表于 2016年4月28日 17:53:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/36911120.html
匿名

发表评论

匿名网友

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

确定