doujingxi3356 2015-04-15 06:26
浏览 89
已采纳

如何在本地创建和使用我自己的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]
  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条回答 默认 最新

  • dounaidu0204 2015-04-15 07:02
    关注

    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
    $ 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?