我刚开始学习编写代码的练习,下面的所有文件都在一个名为leap的目录中。我使用gvm来运行golang可执行文件(版本1.4),使用诸如"go test leap_test.go“之类的命令。
当我去测试leap_test.go时,我得到了以下结果:
# command-line-arguments
leap_test.go:5:2: open /home/user/go/leap/leap: no such file or directory
FAIL command-line-arguments [setup failed]cases_test.go
package leap
// Source: exercism/x-common
// Commit: 945d08e Merge pull request #50 from soniakeys/master
var testCases = []struct {
year int
expected bool
description string
}{
{1996, true, "leap year"},
{1997, false, "non-leap year"},
{1998, false, "non-leap even year"},
{1900, false, "century"},
{2400, true, "fourth century"},
{2000, true, "Y2K"},
}leap_test.go
package leap
import (
"testing"
"./leap"
)
var testCases = []struct {
year int
expected bool
description string
}{
{1996, true, "a vanilla leap year"},
{1997, false, "a normal year"},
{1900, false, "a century"},
{2400, true, "an exceptional century"},
}
func TestLeapYears(t *testing.T) {
for _, test := range testCases {
observed := IsLeap(test.year)
if observed != test.expected {
t.Fatalf("%v is %s", test.year, test.description)
}
}
}leap.go
package leap
import(
"fmt"
)
func IsLeap(year int) bool {
return true
}发布于 2015-04-15 07:02:47
命令启动 测试包 用法: 测试-c构建和测试二进制测试标志
例如,
leap/
package leap
func IsLeap(year int) bool {
return true
}leap/leap_test.go
package leap
import (
"testing"
)
var testCases = []struct {
year int
expected bool
description string
}{
{1996, true, "a vanilla leap year"},
{1997, false, "a normal year"},
{1900, false, "a century"},
{2400, true, "an exceptional century"},
}
func TestLeapYears(t *testing.T) {
for _, test := range testCases {
observed := IsLeap(test.year)
if observed != test.expected {
t.Fatalf("%v is %s", test.year, test.description)
}
}
}如果$GOPATH设置为包括leap包目录:
$ go test leap
--- FAIL: TestLeapYears (0.00s)
leap_test.go:22: 1997 is a normal year
FAIL
FAIL leap 0.003s
$或者,如果您cd到leap包目录:
$ go test
--- FAIL: TestLeapYears (0.00s)
leap_test.go:22: 1997 is a normal year
FAIL
exit status 1
FAIL so/leap 0.003s
$ https://stackoverflow.com/questions/29642985
复制相似问题