Testing Golang测试,相同包,未定义函数
我对围棋还很陌生,正试图为围棋编写一个测试文件。当我运行测试时,我有两个问题: 1.我必须运行“go test-cover”,然后我可以看到:Testing Golang测试,相同包,未定义函数,testing,go,Testing,Go,我对围棋还很陌生,正试图为围棋编写一个测试文件。当我运行测试时,我有两个问题: 1.我必须运行“go test-cover”,然后我可以看到: #command-line-arguments ./client_test.go:59: undefined: Init FAIL command-line-arguments [build failed] exit status 1 FAIL command-line-arguments 0.008s 我的问题是为什么我不能运行:go
#command-line-arguments
./client_test.go:59: undefined: Init
FAIL command-line-arguments [build failed]
exit status 1
FAIL command-line-arguments 0.008s
我的问题是为什么我不能运行:go测试(如果我这样做,我会看到:
#command-line-arguments
./client_test.go:59: undefined: Init
FAIL command-line-arguments [build failed]
exit status 1
FAIL command-line-arguments 0.008s
package main
func hello1(i int) string {
if i > 0 {
return "Hello, world"
}
return "no value"
}
您好(test.go):
package main
import (
"testing"
"fmt"
)
func TestHello(t *testing.T) {
var s string
var s2 string
var s3 string
s = hello1(3)
if s != "Hello, world" {
t.Error("Expected: Hello, world, got", s)
}
s2 = hello1(-3)
if s2 != "Hello, world" {
t.Error("Expected: Hello, world, got", s2)
}
s3 = hello1(0)
if s3 != "Hello, world" {
t.Error("Expected: Hello, world, got", s3)
}
}
当我使用IDE运行这些文件时,我看到:
GOROOT=/usr/local/Cellar/go/1.9.3/libexec #gosetup
GOPATH=/Users/<here is my user name>/go #gosetup
/usr/local/Cellar/go/1.9.3/libexec/bin/go test -c -i -o /private/var/folders/cx/0mvyxrxj5755jc68mqvtywth0000gn/T/___TestHello_in_hello_test_go /Users/minghan/awesomeProject/hello_test.go #gosetup
# command-line-arguments
./hello_test.go:13:6: undefined: hello1
./hello_test.go:17:7: undefined: hello1
./hello_test.go:21:7: undefined: hello1
Compilation finished with exit code 2
对于编写go,拥有正确的文件夹结构非常重要,这样以后就不会有麻烦了。您需要这样的gopath:
bin/
hello # command executable
outyet # command executable
pkg/
linux_amd64/
github.com/golang/example/
stringutil.a # package object
src/
github.com/golang/example/
.git/ # Git repository metadata
hello/
hello.go # command source
outyet/
main.go # command source
main_test.go # test source
stringutil/
reverse.go # package source
reverse_test.go # test source
golang.org/x/image/
.git/ # Git repository metadata
bmp/
reader.go # package source
writer.go # package source
... (many more repositories and packages omitted) ...
(摘自)
gopath默认为“$HOME/go”或其他操作系统(HOME_目录/go)上的等效项
因此,最终您的项目将位于以下位置:$HOME/go/src/github.com/matthin/awesomeproject
把你所有的文件都放进去
然后,要运行测试,您可以执行转到test github.com/matthin/awesomeproject
,也可以通过指定该目录中的每个文件来运行测试,如您的示例:
go test main.go main\u test.go
然后go test runner知道它需要计算这两个文件,然后就会找到函数。try
go test*.go-v
,这对我来说没问题
czyt@MiniManjaro ~/go/src/TestProject/casestatement go test *.go -v ✔ 2080 15:03:17
=== RUN TestCaseStatement
TestCaseStatement: casestatement_test.go:9: 11
TestCaseStatement: casestatement_test.go:11: ===================
TestCaseStatement: casestatement_test.go:12: num 1 2
TestCaseStatement: casestatement_test.go:14: ===================
TestCaseStatement: casestatement_test.go:15: num 1 2
--- PASS: TestCaseStatement (0.00s)
PASS
ok command-line-arguments 0.002s
开始时需要知道的所有信息都在。如果你想获得此问题的帮助,首先需要创建一个。如果没有看到问题中的代码,没有人能够提供太多帮助。
hello\u test.go
甚至不在你的GOPATH中,因此它不能与hello.go(你没有显示其位置)在同一个包中IDE无法运行它的原因是,它只在测试文件本身上调用go test
。它需要在该包中的所有文件上调用它。这就是为什么命令行版本可以工作的原因(尽管它会报告失败,因为您的断言不准确。您调用hello1()
使用-3和0,两者都应该返回无值
,但您断言它们将返回Hello,world
,只有将正整数传递给hello1
)时才会出现这种情况,但您的IDE版本不在。另外,正如所指出的,您的项目不在您的GOPATH中。@Kaedys,谢谢您的回答,您介意告诉我您如何判断项目不在GOPATH中吗?