1

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
  • 2
    Plain ol' go test should do it if you have a sane GOPATH set. Normally only go run takes *.go file names, the others like go install, go build, etc (normally) take package name(s) as arguments. Oh, and relative import paths are generally avoided. Read How To Write Go Code. Commented Apr 15, 2015 at 6:46

1 Answer 1

3

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
$ 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.