How to create and use my own golang package locally to get this test to run?

huangapple go评论135阅读模式
英文:

How to create and use my own golang package locally to get this test to run?

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是一个新手,正在通过编码练习学习golang,并且我在一个名为leap的目录中有以下所有文件。我正在使用gvm运行golang可执行文件(版本1.4),使用类似"go test leap_test.go"的命令。

当我运行"go test leap_test.go"时,我得到以下结果:

  1. # command-line-arguments
  2. leap_test.go:5:2: open /home/user/go/leap/leap: no such file or directory
  3. FAIL command-line-arguments [setup failed]
  1. 如何包含IsLeap()函数以使测试正确运行?
  2. 为什么要包含cases_test.go?似乎只需要leap_test.go就足够进行测试了。

cases_test.go

  1. package leap
  2. // 来源: exercism/x-common
  3. // 提交: 945d08e Merge pull request #50 from soniakeys/master
  4. var testCases = []struct {
  5. year int
  6. expected bool
  7. description string
  8. }{
  9. {1996, true, "闰年"},
  10. {1997, false, "非闰年"},
  11. {1998, false, "非闰年的偶数年"},
  12. {1900, false, "世纪年"},
  13. {2400, true, "第四个世纪"},
  14. {2000, true, "Y2K"},
  15. }

leap_test.go

  1. package leap
  2. import (
  3. "testing"
  4. "./leap"
  5. )
  6. var testCases = []struct {
  7. year int
  8. expected bool
  9. description string
  10. }{
  11. {1996, true, "一个普通的闰年"},
  12. {1997, false, "一个普通年"},
  13. {1900, false, "一个世纪"},
  14. {2400, true, "一个特殊的世纪"},
  15. }
  16. func TestLeapYears(t *testing.T) {
  17. for _, test := range testCases {
  18. observed := IsLeap(test.year)
  19. if observed != test.expected {
  20. t.Fatalf("%v是%s", test.year, test.description)
  21. }
  22. }
  23. }

leap.go

  1. package leap
  2. import(
  3. "fmt"
  4. )
  5. func IsLeap(year int) bool {
  6. return true
  7. }
英文:

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:

  1. # command-line-arguments
  2. leap_test.go:5:2: open /home/user/go/leap/leap: no such file or directory
  3. FAIL command-line-arguments [setup failed]
  1. How do I include the IsLeap() function so that the tests run correctly.
  2. Why is cases_test.go even included? It seems like leap_test.go is all you'd need for tests.

cases_test.go

  1. package leap
  2. // Source: exercism/x-common
  3. // Commit: 945d08e Merge pull request #50 from soniakeys/master
  4. var testCases = []struct {
  5. year int
  6. expected bool
  7. description string
  8. }{
  9. {1996, true, "leap year"},
  10. {1997, false, "non-leap year"},
  11. {1998, false, "non-leap even year"},
  12. {1900, false, "century"},
  13. {2400, true, "fourth century"},
  14. {2000, true, "Y2K"},
  15. }

leap_test.go

  1. package leap
  2. import (
  3. "testing"
  4. "./leap"
  5. )
  6. var testCases = []struct {
  7. year int
  8. expected bool
  9. description string
  10. }{
  11. {1996, true, "a vanilla leap year"},
  12. {1997, false, "a normal year"},
  13. {1900, false, "a century"},
  14. {2400, true, "an exceptional century"},
  15. }
  16. func TestLeapYears(t *testing.T) {
  17. for _, test := range testCases {
  18. observed := IsLeap(test.year)
  19. if observed != test.expected {
  20. t.Fatalf("%v is %s", test.year, test.description)
  21. }
  22. }
  23. }

leap.go

  1. package leap
  2. import(
  3. "fmt"
  4. )
  5. func IsLeap(year int) bool {
  6. return true
  7. }

答案1

得分: 3

> 命令 go
>
> 测试包
>
> 用法:
>
> go test [-c] [-i] [构建和测试标志] [包] [测试二进制文件的标志]

例如,

leap/leap.go

  1. package leap
  2. func IsLeap(year int) bool {
  3. return true
  4. }

leap/leap_test.go

  1. package leap
  2. import (
  3. "testing"
  4. )
  5. var testCases = []struct {
  6. year int
  7. expected bool
  8. description string
  9. }{
  10. {1996, true, "一个普通的闰年"},
  11. {1997, false, "一个普通年"},
  12. {1900, false, "一个世纪"},
  13. {2400, true, "一个特殊的世纪"},
  14. }
  15. func TestLeapYears(t *testing.T) {
  16. for _, test := range testCases {
  17. observed := IsLeap(test.year)
  18. if observed != test.expected {
  19. t.Fatalf("%v 是 %s", test.year, test.description)
  20. }
  21. }
  22. }

如果 $GOPATH 设置包含 leap 包目录:

  1. $ go test leap
  2. --- FAIL: TestLeapYears (0.00s)
  3. leap_test.go:22: 1997 是一个普通年
  4. FAIL
  5. FAIL leap 0.003s
  6. $

或者,如果你 cdleap 包目录:

  1. $ go test
  2. --- FAIL: TestLeapYears (0.00s)
  3. leap_test.go:22: 1997 是一个普通年
  4. FAIL
  5. exit status 1
  6. FAIL so/leap 0.003s
  7. $
英文:

> Command go
>
> Test packages
>
> Usage:
>
> go test [-c] [-i] [build and test flags] [packages] [flags for test binary]

For example,

leap/leap.go

  1. package leap
  2. func IsLeap(year int) bool {
  3. return true
  4. }

leap/leap_test.go

  1. package leap
  2. import (
  3. "testing"
  4. )
  5. var testCases = []struct {
  6. year int
  7. expected bool
  8. description string
  9. }{
  10. {1996, true, "a vanilla leap year"},
  11. {1997, false, "a normal year"},
  12. {1900, false, "a century"},
  13. {2400, true, "an exceptional century"},
  14. }
  15. func TestLeapYears(t *testing.T) {
  16. for _, test := range testCases {
  17. observed := IsLeap(test.year)
  18. if observed != test.expected {
  19. t.Fatalf("%v is %s", test.year, test.description)
  20. }
  21. }
  22. }

If $GOPATH is set to include the leap package directory:

  1. $ go test leap
  2. --- FAIL: TestLeapYears (0.00s)
  3. leap_test.go:22: 1997 is a normal year
  4. FAIL
  5. FAIL leap 0.003s
  6. $

Or, if you cd to the leap package directory:

  1. $ go test
  2. --- FAIL: TestLeapYears (0.00s)
  3. leap_test.go:22: 1997 is a normal year
  4. FAIL
  5. exit status 1
  6. FAIL so/leap 0.003s
  7. $

huangapple
  • 本文由 发表于 2015年4月15日 14:26:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/29642985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定