首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在本地创建和使用我自己的golang包来运行这个测试?

如何在本地创建和使用我自己的golang包来运行这个测试?
EN

Stack Overflow用户
提问于 2015-04-15 06:26:07
回答 1查看 2.3K关注 0票数 1

我刚开始学习编写代码的练习,下面的所有文件都在一个名为leap的目录中。我使用gvm来运行golang可执行文件(版本1.4),使用诸如"go test leap_test.go“之类的命令。

当我去测试leap_test.go时,我得到了以下结果:

代码语言:javascript
复制
# 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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
package leap

import(
    "fmt"
)

func IsLeap(year int) bool {
  return true
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-15 07:02:47

命令启动 测试包 用法: 测试-c构建和测试二进制测试标志

例如,

leap/

代码语言:javascript
复制
package leap

func IsLeap(year int) bool {
    return true
}

leap/leap_test.go

代码语言:javascript
复制
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包目录:

代码语言:javascript
复制
$ go test leap
--- FAIL: TestLeapYears (0.00s)
    leap_test.go:22: 1997 is a normal year
FAIL
FAIL    leap    0.003s
$

或者,如果您cdleap包目录:

代码语言:javascript
复制
$ go test
--- FAIL: TestLeapYears (0.00s)
    leap_test.go:22: 1997 is a normal year
FAIL
exit status 1
FAIL    so/leap 0.003s
$ 
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29642985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档