Newbie question #2: cannot get gotest to work

1,356 views
Skip to first unread message

Martin Ellison

unread,
May 5, 2015, 9:51:37 AM5/5/15
to golan...@googlegroups.com
I cannot get gotest to work. Issues:
  1. when I try go test main I get: /tmp/go-build550118144/main/_test/_testmain.go:9: cannot import "main" and FAIL    main [build failed]
  2. when I do run some tests, I get a report that the tests have failed, but no t.Errorf messages.
Any idea what I should be doing?

Jan Mercl

unread,
May 5, 2015, 10:16:11 AM5/5/15
to golan...@googlegroups.com

On Tue, May 5, 2015 at 3:51 PM Martin Ellison <martin....@gmail.com> wrote:

> I cannot get gotest to work. Issues:
>
> when I try go test main

Try dropping the 'main', unless it's really the intended import path of the package you want to run tests on.

-j

Martin Ellison

unread,
May 5, 2015, 10:57:20 PM5/5/15
to golan...@googlegroups.com
On Tuesday, 5 May 2015 22:16:11 UTC+8, Jan Mercl wrote:

Try dropping the 'main', unless it's really the intended import path of the package you want to run tests on.
 
~/go/src/jennyemily2/src/main> go test
# testmain
/tmp/go-build458325106/main/_test/_testmain.go:9: cannot import "main"
FAIL    main [build failed]

Volker Dobler

unread,
May 6, 2015, 2:10:10 AM5/6/15
to golan...@googlegroups.com
You will have to provide more details like output of `go env`
files in ~/go/src/jennyemily2/src/main and at least their
package clause and imports. What does go build -v do?

V.

Martin Ellison

unread,
May 6, 2015, 9:56:59 AM5/6/15
to golan...@googlegroups.com

You will have to provide more details like output of `go env`
files in ~/go/src/jennyemily2/src/main and at least their
package clause and imports. What does go build -v do?

I have constructed a test project to focus on these issues.

~/go/src/golang-test> find src
src
src/main
src/main/main.go
src/main/main_test.go

main.go

// main is a test of golang.
package main

import "fmt"

// Test is a visible func
func Test() int {
    println("running Test")
    return 17
}
func main() {
    x := Test()
    fmt.Printf("test is %d\n", x)
}


main_test.go

package main

import "testing"

func TestMain(t *testing.T) {
    x := Test()
    if x != 0 {t.Errorf("bad test expected 0 got %d", x)}
}


~/go/src/golang-test> go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/work/golang:/home/martin/go/src/golang-test"
GORACE=""
GOROOT="/usr/lib/golang"
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
You have new mail in /var/spool/mail/martin
~/go/src/golang-test> go build -v main
main
~/go/src/golang-test> go test main
# testmain
/tmp/go-build149703873/main/_test/_testmain.go:9: cannot import "main"
FAIL    main [build failed]
~/go/src/golang-test> godoc main
COMMAND DOCUMENTATION

    main is a test of golang.

~/go/src/golang-test> ./main
running Test
test is 17


Jan Mercl

unread,
May 6, 2015, 10:45:37 AM5/6/15
to golan...@googlegroups.com
On Wed, May 6, 2015 at 3:57 PM Martin Ellison <martin....@gmail.com> wrote:

The directory name "main" is magic or there's a bug in the Go build tool.

(16:42) jnml@r550:~$ cd $GOPATH/src/main
(16:42) jnml@r550:~/src/main$ go test
# testmain
/tmp/go-build582720844/main/_test/_testmain.go:12: cannot import "main"
FAIL main [build failed]
(16:42) jnml@r550:~/src/main$ cd ..
(16:42) jnml@r550:~/src$ mv main/ foo
(16:42) jnml@r550:~/src$ cd foo/
/home/jnml/src/foo
(16:42) jnml@r550:~/src/foo$ go test
running Test
--- FAIL: TestMain (0.00s)
main_test.go:8: bad test expected 0 got 17
FAIL
exit status 1
FAIL foo 0.002s
(16:42) jnml@r550:~/src/foo$

-j

Martin Ellison

unread,
May 6, 2015, 10:59:28 PM5/6/15
to Jan Mercl, golan...@googlegroups.com
Indeed, I changed the 'main' directory to 'golangtest' and did the same for the package statements in both .go files, and everything worked.

I had somehow got the impression that the main program had to be in package main; I am not sure why.

The golang documentation should be updated to say that one cannot use 'main' as a package name, and there should be an error message if one does.

Jan, thanks for your help.

Regards,
Martin

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/ubYw0OQXLvA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

minux

unread,
May 7, 2015, 1:55:04 AM5/7/15
to Martin Ellison, Jan Mercl, golang-nuts
On Wed, May 6, 2015 at 10:58 PM, Martin Ellison <m...@acm.org> wrote:
Indeed, I changed the 'main' directory to 'golangtest' and did the same for the package statements in both .go files, and everything worked.

I had somehow got the impression that the main program had to be in package main; I am not sure why.

The golang documentation should be updated to say that one cannot use 'main' as a package name, and there should be an error message if one does.

It does seem like a bug of the go tool, although a minor one.

If you rename the directory to something other than main, go test should work,
even if the package name is still "main".

minux

unread,
May 7, 2015, 2:04:03 AM5/7/15
to Martin Ellison, Jan Mercl, golang-nuts
On Thu, May 7, 2015 at 1:54 AM, minux <mi...@golang.org> wrote:
On Wed, May 6, 2015 at 10:58 PM, Martin Ellison <m...@acm.org> wrote:
Indeed, I changed the 'main' directory to 'golangtest' and did the same for the package statements in both .go files, and everything worked.

I had somehow got the impression that the main program had to be in package main; I am not sure why.

The golang documentation should be updated to say that one cannot use 'main' as a package name, and there should be an error message if one does.

It does seem like a bug of the go tool, although a minor one.
After more thought, I don't think it's a bug. You just can't test a package whose
import path is "main". Because the test driver is the package main, so it can't
import another package whose import path is also "main". 
Reply all
Reply to author
Forward
0 new messages