embedding files in tests

2,754 views
Skip to first unread message

Manlio Perillo

unread,
Jun 1, 2021, 9:17:11 AM6/1/21
to golang-nuts
When a file is embedded in a test file, will the file be embedded only in the test binary?
The documentation says nothing about this case, but the data from go list seems to confirm that the file is only embedded in the test binary.

Thanks
Manlio

jake...@gmail.com

unread,
Jun 1, 2021, 12:03:56 PM6/1/21
to golang-nuts
I would strongly assume that the file is only embedded in the test binary. In fact it is hard to imagine a design where it would not be that way.
If you really want to make sure, you could easily build two, identical binaries, that differ only in that a very large file is embedded in the test file for one, and compare sizes.

peterGo

unread,
Jun 1, 2021, 8:09:20 PM6/1/21
to golang-nuts
On Tuesday, June 1, 2021 at 9:17:11 AM UTC-4 manlio....@gmail.com wrote:
The documentation seems clear to me.

Package testing

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.

Peter

Nikolay Dubina

unread,
Jun 1, 2021, 10:23:32 PM6/1/21
to golang-nuts
I think the question is about `go:embed` directive in `_test.go` files will be included in builds of non-test packages, right?

Don't know for sure, but I think:
- a) if you have `mypkg_test.go` and in it `package mypkg_test` then `go:embed` will not get to your non test build as the package is different
- b) if you have `mypkg_test.go` and in it `pakcage mypkg` then... less clear. As above referenced in original docs `_test.go` will be excluded from non test build regardless of package.

My personal take, is to go:embed only in case a).

Cheers,

Manlio Perillo

unread,
Jun 2, 2021, 5:49:41 AM6/2/21
to golang-nuts

The output of `go list -json ./pkg` only reports `TestEmbedPatterns`.
The output of `go list -json -test ./pkg` reports TestEmbedFiles for the pkg package (it seems a bug, since it should also be reported without the -test flag).
The TestEmbedFiles is also present in the generated `pkg.test` package and the pkg [pkg.test] package.

Thanks
Manlio

peterGo

unread,
Jun 2, 2021, 11:56:42 AM6/2/21
to golang-nuts
"it seems a bug ... it should" Why?

Why perform unnecessary disk directory I/O for a build to discover which files satisfy the patterns for a test? Go tools are expected to be fast.

Peter

Manlio Perillo

unread,
Jun 3, 2021, 3:31:13 PM6/3/21
to golang-nuts
Actually, if a file is embedded in the main package, go list will report it in the EmbedFiles field.
If a file is embedded in a test, the -test flag is required because the test binary is required.

Regards
Manlio

Reply all
Reply to author
Forward
0 new messages