回答

收藏

go test 在同一个包中找不到函数

技术问答 技术问答 241 人阅读 | 0 人回复 | 2023-09-12

目录结构如下:* y! ]( o- T. L) N4 E. X2 n! F
    srcsrc/pkgsrc/pkg/t1.gosrc/pkg/t1_test.go5 X$ u+ r" y1 U  B& d
t1.go
    package pkgimport ("fmt")func SayHI(){     fmt.Println("this is t1")}
    3 U' l, \: ?7 ], h( J6 V
t1_test.go
    package pkgimport  "testing")func TestXYZ(t *testing.T)    SayHI()}
    # f$ F5 A# v- Y" X
在 dir 从命令行调用 go test src/pkg# _* [2 j0 B2 \$ L0 H* ]- P+ b
go test t1_test.go错误:
    ./t1_test.go:8: undefined: SayHIFAIL    command-line-arguments [build failed]
    6 w: z& ]9 P* E7 d" d4 F
但是功能在那里
( L% z- h' Z1 L' `/ F9 h& p/ M感谢您的提示
  {9 d) c' o( t! Y: B                                                               
( d9 l7 Z: o3 ]/ o* W8 p. o    解决方案:                                                                4 h- X6 {: l6 Y% B; N) {
                                                                它按预期工作。
& ~  T; x5 q- c" P
    jnml@fsc-r630:~/src/pkg$ go help testusage: go test [-c] [-i] [build flags] [packages] [flags for test binary]'Go test' 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.'Go test' recompiles each package along with any files with names matchingthe file pattern "*_test.go".  These additional files can contain test functions,benchmark functions,and example functions.  See 'go help testfunc' for more.By default,go test needs no arguments.  It compiles and tests the packagewith 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 thenon-test installation.In addition to the build flags,the flags handled by 'go test' 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; theseflags are also accessible by 'go test'.  See 'go help testflag' for details.For more about build flags,see 'go help build'.For more about specifying packages,see 'go help packages'.See also: go build,go vet.jnml@fsc-r630:~/src/pkg$
    / w% E0 x7 Z2 y4 j& I* _1 n5 ]3 B
换句话说:
3 y3 P! k; @2 z& igo test 没关系。: C3 D" Y& C% t. o7 K9 j
go test pkg (假设 $GOPATH 是 ~ 并在 里~/src/pkg 中)没问题。
9 o9 v2 s  @/ n4 Cgo test whatever_test.go不好,因为上面记录的不支持。
选择要操作的测试,请使用-run 标志(其中解释为两端都有通配符,例如.*.*)。例如
7 Z- A% E* j6 m3 j2 ~
    $ go test -run Say # from within the package's directory
    + z. l1 l" G/ i; O- Y
或者
( D* ?8 h5 t9 [8 P. E' y: V
    $ go test -run Say my/package/import/path # from anywhere
    # r5 A5 n/ k- O" s6 o- G( P" D
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则