Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go test <function> returns undefined: <function>

Tags:

testing

go

Trying to run "go test sum_test.go" returns an error:

./sum_test.go:18:13: undefined: SumInt8 FAIL command-line-arguments [build failed]

I'm taking an introductory course in golang. Our teacher distributed a code-file, sum.go, and a testing-file, sum_test.go. Trying to run "go test" on sum_test.go returns the error above. The code runs fine on our teachers mac, and he's having difficulties recreating the problem. Here's my go environment settings: https://pastebin.com/HcuNVcAF

sum.go

package sum

func SumInt8(a, b int8) int8 {
  return a + b
}

func SumFloat64(a, b float64) float64 {
  return a + b
}

sum_test.go

package sum

import "testing"

// Check https://golang.org/ref/spec#Numeric_types and stress the limits!
var sum_tests_int8 = []struct{
  n1       int8
  n2       int8
  expected int8
}{
  {1, 2, 3},
  {4, 5, 9},
  {120, 1, 121},
}

func TestSumInt8(t *testing.T) {
for _, v := range sum_tests_int8 {
    if val := SumInt8(v.n1, v.n2); val != v.expected {
        t.Errorf("Sum(%d, %d) returned %d, expected %d", 
v.n1, v.n2, val, v.expected)
    }
  }
}

I see no particular errors, so I expected "go test sum_test.go" to run, and succeed. However it seems it can't find the method SumInt8 in sum.go.

like image 512
brisdalen Avatar asked Feb 08 '19 11:02

brisdalen


People also ask

How do you test a go function?

To run any test function use “go test” command, which automates the execution of any function of the form TestXxx(*testing. T), where Xxx must not start with any lowercase letter.

How do you test private function in go?

Testing private functions in Go is very simple. To do this you put your implementation and tests in the same package. The private functions are available in the test file as they are in the implementation file.

Do go tests run concurrently?

Yes, tests are executed as goroutines and, thus, executed concurrently.

How do I run a test file in Golang?

To run your tests in this mode, run go test in your project's root directory. In the package list mode, go test compiles and tests each package listed as arguments to the command. If a package test passes, go test prints only the final 'ok' summary line. If a package test fails, go test prints the complete test output.


1 Answers

$ go help packages

Many commands apply to a set of packages:

go action [packages]

Usually, [packages] is a list of import paths.

An import path that is a rooted path or that begins with a . or .. element is interpreted as a file system path and denotes the package in that directory.

Otherwise, the import path P denotes the package found in the directory DIR/src/P for some DIR listed in the GOPATH environment variable (For more details see: 'go help gopath').

If no import paths are given, the action applies to the package in the current directory.

As a special case, if the package list is a list of .go files from a single directory, the command is applied to a single synthesized package made up of exactly those files, ignoring any build constraints in those files and ignoring any other files in the directory.


List all the files in the current directory used in the test:

go test sum_test.go sum.go

or simply test the complete package in the current directory.

go test

For example,

$ go test -v sum_test.go sum.go
=== RUN   TestSumInt8
--- PASS: TestSumInt8 (0.00s)
PASS
ok      command-line-arguments  0.002s
$ 

or, for the complete package

$ go test -v
=== RUN   TestSumInt8
--- PASS: TestSumInt8 (0.00s)
PASS
ok      so/sum  0.002s
$

Option '-v' produces verbose output.

sum_test.go:

package sum

import "testing"

// Check https://golang.org/ref/spec#Numeric_types and stress the limits!
var sum_tests_int8 = []struct {
    n1       int8
    n2       int8
    expected int8
}{
    {1, 2, 3},
    {4, 5, 9},
    {120, 1, 121},
}

func TestSumInt8(t *testing.T) {
    for _, v := range sum_tests_int8 {
        if val := SumInt8(v.n1, v.n2); val != v.expected {
            t.Errorf("Sum(%d, %d) returned %d, expected %d",
                v.n1, v.n2, val, v.expected)
        }
    }
}

sum.go:

package sum

func SumInt8(a, b int8) int8 {
    return a + b
}

func SumFloat64(a, b float64) float64 {
    return a + b
}

like image 98
peterSO Avatar answered Oct 11 '22 02:10

peterSO