我很陌生,并尝试为go编写测试文件。当我进行测试时,我有两个问题: 1.我必须运行"去测试 - 然后我可以看到:
#command-line-arguments
./client_test.go:59: undefined: Init
FAIL command-line-arguments [build failed]
我的问题是为什么我不能运行:去测试(如果我这样做,我会看到:
exit status 1
FAIL command-line-arguments 0.008s
我已经在网上查询了几天的解决方案。任何想法都会有所帮助。 很抱歉,我无法显示代码,我非常感谢您的帮助。
谢谢
EDIT2: 谢谢,各位,我正在考虑env变量,(我正在设置它们)。我非常感谢你的评论。
EDIT1: 感谢您的回复,我尝试了一小段代码,看看go测试是否正常: (对不起,我可以做我正在进行的项目) hello.go:
package main
func hello1(i int) string {
if i > 0 {
return "Hello, world"
}
return "no value"
}
hello_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 test -cover:
--- FAIL: TestHello (0.00s)
hello_test.go:19: Expected: Hello, world, got no value
hello_test.go:23: Expected: Hello, world, got no value
FAIL
coverage: 100.0% of statements
exit status 1
FAIL _/Users/minghan/awesomeProject 0.007s
答案 0 :(得分:1)
尝试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
答案 1 :(得分:0)
对于写作来说,拥有正确的文件夹结构非常重要,这样您以后就不会感到头痛。你需要这样的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) ...
(摘自https://golang.org/doc/code.html#Workspaces)
gopath默认为“$ HOME / go”或其他操作系统上的等价物(home_directory / go)
因此,您最终会在以下内容中使用您的项目:$HOME/go/src/github.com/matthin/awesomeproject
并将所有文件放入其中。
然后要运行测试,您可以执行go test github.com/matthin/awesomeproject
,或者您可以通过指定该目录中的每个文件来运行测试,就像您的情况一样:
go test main.go main_test.go
然后go测试运行器知道它需要评估两个文件并找到该函数。