Testing specific file in a package

159 views
Skip to first unread message

Victor Manuel “Vitu” Giordano

unread,
Jun 28, 2024, 6:25:02 PM (4 days ago) Jun 28
to golang-nuts
Hi guys! How you are doing?

I struggle with performing all the tests written in a file that requires symbols defined on other files of the same package.

For example
$ go test practice_resource_test.go
# command-line-arguments [command-line-arguments.test]
./practice_resource_test.go:11:12: undefined: PracticeResource
./practice_resource_test.go:12:12: undefined: PracticeResource
FAIL command-line-arguments [build failed]
FAIL


The symbol PracticeResource is defined in a file called practice_resource present at the same package.

¿Is it possible to archieve this? If anyone know how  and can explain me I will appreciate that.
Thanks in advance.

I haven't see how to do it in the official doc and in other doc as well.
Greetings.

Ian Lance Taylor

unread,
Jun 28, 2024, 7:41:22 PM (4 days ago) Jun 28
to Victor Manuel “Vitu” Giordano, golang-nuts
When testing a package, you normally write "go test" or "go test
PACKAGEPATH" without listing any files. Listing files is an unusual
case, only used for a standalone test that is not part of a package.

Ian

Kurtis Rader

unread,
Jun 28, 2024, 9:37:28 PM (4 days ago) Jun 28
to Victor Manuel “Vitu” Giordano, golang-nuts
On Fri, Jun 28, 2024 at 3:25 PM Victor Manuel “Vitu” Giordano <vituc...@gmail.com> wrote:
I struggle with performing all the tests written in a file that requires symbols defined on other files of the same package.

For example
$ go test practice_resource_test.go
# command-line-arguments [command-line-arguments.test]
./practice_resource_test.go:11:12: undefined: PracticeResource
./practice_resource_test.go:12:12: undefined: PracticeResource
FAIL command-line-arguments [build failed]
FAIL


The symbol PracticeResource is defined in a file called practice_resource present at the same package.

If the reason you're doing this is because you want to run only the tests in that file rather than all tests for the package you should instead use the "-run" flag to specify which tests you want to run and specify a package rather than a single test file. See "go help testflag". 

--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Marcello H

unread,
Jun 29, 2024, 5:43:08 AM (4 days ago) Jun 29
to golang-nuts
You can look at how your IDE would do it, or, you can put a build tag in and only test that build tag

Op zaterdag 29 juni 2024 om 03:37:28 UTC+2 schreef Kurtis Rader:

Victor Manuel Giordano

unread,
Jun 29, 2024, 10:15:56 AM (3 days ago) Jun 29
to Marcello H, golang-nuts
Thank you all for the feedback. It is appreciated. 
  • So it is not possible via the golang platform native capabilities... 
  • The Visual Studio Code (IDE) offers a good feature building on top of the -run flag
    • It parses the file to test, recovers all the test function names of the given file and builds a regex that matches all the functions to run... so later that regex would be the argument passed to the -run flag.
  • I look for this because I don't want to run all the tests of the package, only the ones present in a given file.



--
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/5noIceyeCa4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/60fc7fef-fa41-4d1d-bed5-83e9705dcdfcn%40googlegroups.com.


--
V

Arkadiusz Drabczyk

unread,
Jun 30, 2024, 3:38:44 PM (2 days ago) Jun 30
to Victor Manuel Giordano, Marcello H, golang-nuts
On Sat, Jun 29, 2024 at 11:14:56AM -0300, Victor Manuel Giordano wrote:
> Thank you all for the feedback. It is appreciated. 
>
> • So it is not possible via the golang platform native capabilities... 

It is as described in the second part of this answer
https://stackoverflow.com/a/16936314/3691891. If you have a typical
pkg/cmd layout as described at
https://github.com/golang-standards/project-layout you can do
something like that:

$ go test ./cmd/<MY_CMD>/main_test.go ./cmd/<MY_CMD>/main.go

--
Arkadiusz Drabczyk <arka...@drabczyk.org>

Victor Manuel “Vitu” Giordano

unread,
9:33 AM (12 hours ago) 9:33 AM
to golang-nuts
Thanks Arkadiusz. 
Good tip.

Reply all
Reply to author
Forward
0 new messages