13

I have following code under packagepack1. Name of file ispack1.go

package pack1var Pack1Int int = 42var pack1Float = 3.14func ReturnStr() string {    return "Hello world!"}

And following code in main program. Name of file ispackage_test.go

package mainimport (    "fmt"    "./pack1")func main() {    var test1 string    test1 = pack1.ReturnStr()    fmt.Printf("Return string from pack1 : %s\n", test1)    fmt.Printf("Integer from pack1 : %d\n", pack1.Pack1Int)}

When I try to run it with commandgo run package_test.go I get following error:

go run: cannot run *_test.go files (package_test.go)

But if I rename file toabc.go then I am getting proper output i.e.

Return string from pack1 : Hello world!Integer from pack1 : 42

I am curious about what is wrong with usingpackage_test.go as file name. For code with only main package this name is working fine.

Is this a bug in Go or I am doing something wrong ?

TGrif's user avatar
TGrif
5,9709 gold badges36 silver badges56 bronze badges
askedJan 30, 2016 at 7:51
shivams's user avatar

4 Answers4

15

Not a bug, it's designed so.go run detects the_test files and consider them as test files for a package, test files will be compiled as a separate package, and then linked and run with the main test binary.
It's recommended to put your package file toGOPATH/src/PACK_NAME/, then run your*_test.go withgo test.

answeredJan 30, 2016 at 7:58
jfly's user avatar
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please elaborate it a little ?
This seems like a silly language feature, as it means you can't store _test.go files in the same directory as the rest of your go package. Unless I'm misunderstanding?
10

You can't name your program files as*_test.go as this is part of integrated Gotesting system

To write a new test suite,create a file whose name ends _test.go that contains the TestXxx functions as described here. Put the file in the same package as the one being tested. The file will be excluded from regular package builds but will be included when the “go test” command is run. For more detail, run “go help test” and “go help testflag”.

Just renamepackage_test.go topackagetest.go

answeredJan 30, 2016 at 8:20
CrazyCrow's user avatar

Comments

1

ForBASH

Rungo run PATH_TO_FILES/!(*_test).go

NOTE

If you get an event not found error when running this command, you probably need to enable extended globbing in your bash terminal.

Runshopt-s extglob

after you Rungo run PATH_TO_FILES/!(*_test).go


For those usingZSH

setopt extendedglob # to get help regarding globbing

next

foo*~*bar* # match everything that starts with foo but doesn't contain bar

So with my case,go run PATH_TO_FILES/*~*_test.go*

answeredMay 3, 2022 at 21:50
jeffasante's user avatar

Comments

-1

if you are using linux:

go run `ls PATH_TO_FILES/*.go | grep -v _test.go`

it workes for me.

answeredNov 15, 2022 at 12:35
salar's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.