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

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

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"时,我得到以下结果:

# 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]
  1. 如何包含IsLeap()函数以使测试正确运行?
  2. 为什么要包含cases_test.go?似乎只需要leap_test.go就足够进行测试了。

cases_test.go

package leap

// 来源: exercism/x-common
// 提交: 945d08e Merge pull request #50 from soniakeys/master

var testCases = []struct {
	year        int
	expected    bool
	description string
}{
	{1996, true, "闰年"},
	{1997, false, "非闰年"},
	{1998, false, "非闰年的偶数年"},
	{1900, false, "世纪年"},
	{2400, true, "第四个世纪"},
	{2000, true, "Y2K"},
}

leap_test.go

package leap

import (
	"testing"
	"./leap"
)

var testCases = []struct {
	year        int
	expected    bool
	description string
}{
	{1996, true, "一个普通的闰年"},
	{1997, false, "一个普通年"},
	{1900, false, "一个世纪"},
	{2400, true, "一个特殊的世纪"},
}

func TestLeapYears(t *testing.T) {
	for _, test := range testCases {
		observed := IsLeap(test.year)
		if observed != test.expected {
			t.Fatalf("%v是%s", test.year, test.description)
		}
	}
}

leap.go

package leap

import(
	"fmt"
)

func IsLeap(year int) bool {
  return true
}
英文:

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]
  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

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
}

答案1

得分: 3

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

例如,

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, "一个普通的闰年"},
	{1997, false, "一个普通年"},
	{1900, false, "一个世纪"},
	{2400, true, "一个特殊的世纪"},
}

func TestLeapYears(t *testing.T) {
	for _, test := range testCases {
		observed := IsLeap(test.year)
		if observed != test.expected {
			t.Fatalf("%v 是 %s", test.year, test.description)
		}
	}
}

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

$ go test leap
--- FAIL: TestLeapYears (0.00s)
	leap_test.go:22: 1997 是一个普通年
FAIL
FAIL	leap	0.003s
$

或者,如果你 cdleap 包目录:

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

> Command go
>
> Test packages
>
> 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
$ 

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:

确定