如何在本地创建和使用我自己的golang软件包来运行此测试?
I'm new to golang working through coding exercises and I have all the following files in a directory called leap. I'm using gvm to run the golang executable (version 1.4), using a command such as "go test leap_test.go".
When I do go test leap_test.go I get the following results:
# 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]
- How do I include the IsLeap() function so that the tests run correctly.
- Why is cases_test.go even included? It seems like leap_test.go is all you'd need for tests.
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
}
我是golang编码练习的新手,我将以下所有文件放在一个名为jump的目录中。 我正在使用gvm来运行golang可执行文件(1.4版),并使用“ go test jump_test.go”之类的命令。 p>
当我执行 go测试jump_test.go em>时,我得到以下结果: p>
cases_test.go strong> p>
leap_test.go strong> p>
leap.go strong> p>
#命令行 -arguments
leap_test.go:5:2:打开/ home / user / go / leap / leap:无此类文件或目录
FAIL命令行参数[设置失败]
code> pre> \ n
package jump
//来源:exercism / x-common
//提交:945d08e从soniakeys / master
var testCases = [] struct {
年 int
预期布尔值
描述字符串
} {
{1996,true,“ le年”}},
{1997,false,“非-年”}},
{1998,false,“ non -leap even year“},
{1900,否,“世纪”},
{2400,是,“第四世纪”},
{2000,是,” Y2K“},
}
pre>
程序包飞跃
import(
“测试”
“ ./leap"
)
var testCases = [] struct {
year int
预期布尔值
描述字符串
} {
{1996,是,“香草leap年”} ,
{1997,否,“正常年份”}},
{1900,否,“一个世纪”},
{2400,是真的,“非同寻常的世纪”},
}
func TestLeapYears(t * testing.T){
为_,测试:=范围 testCases {
已观察到:= IsLeap(test.year)
,如果已观察到!= test.expected {
t.Fatalf(“%v is%s”,test.year,test.description)
}
}
}
code> pre>
包裹飞跃
\ nimport(
“ fmt”
)
func IsLeap(year int)bool {
返回true
}
code> pre>
div>
Usage:
go test [-c] [-i] [build and test flags] [packages] [flags for test binary]
For example,
leap/leap.go
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)
}
}
}
If $GOPATH
is set to include the leap
package directory:
$ go test leap
--- FAIL: TestLeapYears (0.00s)
leap_test.go:22: 1997 is a normal year
FAIL
FAIL leap 0.003s
$
Or, if you cd
to the leap
package directory:
$ go test
--- FAIL: TestLeapYears (0.00s)
leap_test.go:22: 1997 is a normal year
FAIL
exit status 1
FAIL so/leap 0.003s
$