Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在本地创建和使用我自己的golang包来运行此测试?_Go - Fatal编程技术网

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

如何在本地创建和使用我自己的golang包来运行此测试?,go,Go,我是刚接触golang的,正在进行编码练习,我在一个名为leap的目录中有以下所有文件。我正在使用gvm运行golang可执行文件(版本1.4),使用诸如“go test leap_test.go”之类的命令 当我进行leap\U test.go测试时,我得到以下结果: # command-line-arguments leap_test.go:5:2: open /home/user/go/leap/leap: no such file or directory FAIL command

我是刚接触golang的,正在进行编码练习,我在一个名为leap的目录中有以下所有文件。我正在使用gvm运行golang可执行文件(版本1.4),使用诸如“go test leap_test.go”之类的命令

当我进行leap\U 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]
  • 如何包含IsLeap()函数以使测试正确运行
  • 为什么连测试都包括在内?似乎leap_test.go是所有测试所需的
  • 案例\u测试。开始

    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"},
    }
    
    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)
                }
            }
        }
    
    package leap
    
    import(
        "fmt"
    )
    
    func IsLeap(year int) bool {
      return true
    }
    
    package leap
    
    func IsLeap(year int) bool {
        return true
    }
    
    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)
            }
        }
    }
    
    leap\u测试。开始

    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"},
    }
    
    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)
                }
            }
        }
    
    package leap
    
    import(
        "fmt"
    )
    
    func IsLeap(year int) bool {
      return true
    }
    
    package leap
    
    func IsLeap(year int) bool {
        return true
    }
    
    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)
            }
        }
    }
    
    leap.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"},
    }
    
    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)
                }
            }
        }
    
    package leap
    
    import(
        "fmt"
    )
    
    func IsLeap(year int) bool {
      return true
    }
    
    package leap
    
    func IsLeap(year int) bool {
        return true
    }
    
    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)
            }
        }
    }
    

    用法:

    go test [-c] [-i] [build and test flags] [packages] [flags for test binary]
    
    比如说,

    leap/leap.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"},
    }
    
    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)
                }
            }
        }
    
    package leap
    
    import(
        "fmt"
    )
    
    func IsLeap(year int) bool {
      return true
    }
    
    package leap
    
    func IsLeap(year int) bool {
        return true
    }
    
    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)
            }
        }
    }
    
    leap/leap\u测试。开始

    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"},
    }
    
    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)
                }
            }
        }
    
    package leap
    
    import(
        "fmt"
    )
    
    func IsLeap(year int) bool {
      return true
    }
    
    package leap
    
    func IsLeap(year int) bool {
        return true
    }
    
    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
    程序包目录:

    $ go test leap
    --- FAIL: TestLeapYears (0.00s)
        leap_test.go:22: 1997 is a normal year
    FAIL
    FAIL    leap    0.003s
    $
    
    $ go test
    --- FAIL: TestLeapYears (0.00s)
        leap_test.go:22: 1997 is a normal year
    FAIL
    exit status 1
    FAIL    so/leap 0.003s
    $ 
    
    或者,如果您将
    cd
    转到
    leap
    软件包目录:

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

    如果您有一个sane
    GOPATH
    设置,普通ol'
    go测试应该执行。通常只有
    go run
    采用*.go文件名,其他如
    go install
    go build
    等(通常)采用包名作为参数。哦,通常避免使用相对导入路径。阅读