go test无法在同一个包中找到函数。

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

go test can't find function in a same package

问题

目录结构如下:

src
src/pkg
src/pkg/t1.go
src/pkg/t1_test.go

t1.go

package pkg

import (
"fmt"
)

func SayHI(){
    fmt.Println("this is t1")
}

t1_test.go

package pkg

import (
    "testing"
)

func TestXYZ(t *testing.T) {
    SayHI()
}

在命令行中从src/pkg目录下调用go test

go test t1_test.go


错误信息:

./t1_test.go:8: undefined: SayHI
FAIL	command-line-arguments [build failed]

但是函数是存在的

感谢任何提示

英文:

The directory structure is :

src
src/pkg
src/pkg/t1.go
src/pkg/t1_test.go

t1.go

package pkg

import (
"fmt"
)

func SayHI(){
	fmt.Println("this is t1")
}

t1_test.go

package pkg

import (
    "testing"
)

func TestXYZ(t *testing.T) {
	SayHI()
}

Invoke go test from command line at dir src/pkg

go test t1_test.go


error:

./t1_test.go:8: undefined: SayHI
FAIL	command-line-arguments [build failed]

but the function is there

thanks for any hints

答案1

得分: 79

它按预期工作。

jnml@fsc-r630:~/src/pkg$ go help test
用法:go test [-c] [-i] [构建标志] [包] [测试二进制文件的标志]

'Go test'自动化测试由导入路径指定的包。
它以以下格式打印测试结果的摘要:

    ok   archive/tar   0.011s
    FAIL archive/zip   0.022s
    ok   compress/gzip 0.033s
    ...

然后是每个失败包的详细输出。

'Go test'重新编译每个包以及与文件模式“*_test.go”匹配的任何文件。这些附加文件可以包含测试函数、基准函数和示例函数。有关更多信息,请参阅'go help testfunc'。

默认情况下,go test不需要参数。它编译并测试当前目录中包含测试的源代码,并运行测试。

该包在临时目录中构建,因此不会干扰非测试安装。

除了构建标志外,'go test'本身处理的标志还有:

    -c  将测试二进制文件编译为pkg.test,但不运行它。

    -i
        安装测试的依赖包。
        不运行测试。

测试二进制文件还接受控制测试执行的标志;这些标志也可以通过'go test'访问。有关详细信息,请参阅'go help testflag'。

有关构建标志的更多信息,请参阅'go help build'。
有关指定包的更多信息,请参阅'go help packages'。

另请参阅:go build,go vet。
jnml@fsc-r630:~/src/pkg$ 

换句话说:

  • go test 是可以的。
  • go test pkg(假设$GOPATH是~,包在~/src/pkg中)是可以的。
  • go test whatever_test.go 不可以,因为它在上面的文档中没有被支持。

要选择要运行的测试,请使用-run <regular_expression>标志(其中<regular_expression>被解释为两端都有通配符的正则表达式,如.*<regular_expression>.*)。例如

$ go test -run Say # 在包的目录中运行

或者

$ go test -run Say my/package/import/path # 从任何地方运行
英文:

It is working as intended.

jnml@fsc-r630:~/src/pkg$ go help test
usage: go test [-c] [-i] [build flags] [packages] [flags for test binary]

&#39;Go test&#39; automates testing the packages named by the import paths.
It prints a summary of the test results in the format:

    ok   archive/tar   0.011s
    FAIL archive/zip   0.022s
    ok   compress/gzip 0.033s
    ...

followed by detailed output for each failed package.

&#39;Go test&#39; recompiles each package along with any files with names matching
the file pattern &quot;*_test.go&quot;.  These additional files can contain test functions,
benchmark functions, and example functions.  See &#39;go help testfunc&#39; for more.

By default, go test needs no arguments.  It compiles and tests the package
with source in the current directory, including tests, and runs the tests.

The package is built in a temporary directory so it does not interfere with the
non-test installation.

In addition to the build flags, the flags handled by &#39;go test&#39; itself are:

    -c  Compile the test binary to pkg.test but do not run it.

    -i
        Install packages that are dependencies of the test.
        Do not run the test.

The test binary also accepts flags that control execution of the test; these
flags are also accessible by &#39;go test&#39;.  See &#39;go help testflag&#39; for details.

For more about build flags, see &#39;go help build&#39;.
For more about specifying packages, see &#39;go help packages&#39;.

See also: go build, go vet.
jnml@fsc-r630:~/src/pkg$ 

In other words:

  • go test is okay.
  • go test pkg (assuming $GOPATH is ~ and the package is in ~/src/pkg) is okay.
  • go test whatever_test.go is not okay as that is not supported as documented above.

To select which tests to run use the -run &lt;regular_expression&gt; flag (where the &lt;regular_expression&gt; is interpreted as having wildcards on either end, like .*&lt;regular_expression&gt;.*). For example

$ go test -run Say # from within the package&#39;s directory

or

$ go test -run Say my/package/import/path # from anywhere

答案2

得分: 28

这在Golang中有点奇怪。老实说,我花了一些时间才找到一种解决方法。

一个简单的解决办法是将它们包含在命令中,例如:
go test src/pkg/t1.go src/pkg/t1_test.go

在我看来,最好的方法是保持代码整洁。因此,避免每个测试文件有多个文件作为依赖。如果你使用了多于一个文件作为依赖,请考虑创建一个带有_test包的黑盒测试,并且不使用任何小写的内部变量。

这样可以避免你在日常测试中处理复杂的依赖关系。

英文:

This is somewhat strange in Golang. To be honest it took me some time to figure a way out.

A simple work-around is to include them in the command, eg:
go test src/pkg/t1.go src/pkg/t1_test.go

IMHO, The best way is to keep it clean. So avoid having more than 1 file as dependency per test file. If you are using +1 file as dependency, consider creating a black-box test with a _test package and do not make use of any lowerCase internal vars.

This will avoid you having to deal with complicated dependencies on your day to day testing.

答案3

得分: 8

运行

go test ./...

这将在所有测试文件中找到所有的测试。要运行单个测试,请像这里那样指定依赖项。

英文:

Run

go test ./...

This will find all the tests in all test files. To run individual tests, specify the dependencies like here.

答案4

得分: 1

我在遇到完全相同的问题后,偶然发现了这个Stackoverflow的问题:具体来说,尝试运行go test,然后看到构建失败,指示所讨论的函数未定义。我看到这个问题现在有点旧了(8年前提问),但在我的情况下,问题是我试图在Go 1.16中编写代码,现在似乎假设存在/使用模块。请参阅此页面以获取有关模块和简单示例的起始参考。

在我的情况下,我只需要运行go mod init [模块名称],之后就可以无任何问题地运行go test

希望对使用更现代(1.16+)版本的Go的用户有所帮助。

英文:

I came across this Stackoverflow question after encountering the exact same issue myself: specifically, attempting to run go test and then seeing a build failure indicating that the function in question isn't defined. I see that this question is somewhat old now (asked 8 years ago) but in my case the issue was that I was attempting to write code in Go 1.16 that seems to now assume the presence/use of modules. See this page for a starting reference on modules and easy follow-along example.

All I had to do in my case was run go mod init [module name] and after that I could run go test without any issues.

Hopefully this is of some value to users coming to this page after using a more modern (1.16+) version of Go.

huangapple
  • 本文由 发表于 2013年2月6日 14:49:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/14723229.html
匿名

发表评论

匿名网友

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

确定