Home > Software design >  Golang: Compile all tests across a repo without executing them
Golang: Compile all tests across a repo without executing them

Time:06-29

Context

I have a repo in which multiple teams contribute integration tests.

All these tests are hidden behind //go:build integration flags, so if I want go to see or run them, I need to pass the -build integration flag to the test command.

Purpose

What I’m trying to accomplish is to compile all tests across the entirety of this repo without actually executing them (would take a long time) so I can catch build errors introduced by PRs that the default go test compilation and execution would not catch.

I see the -c flag:

-c Compile the test binary to pkg.test but do not run it (where pkg is the last element of the package's import path). The file name can be changed with the -o flag.

However… one cannot use the -c flag with the -build flag:

$ go test -build=integration -c ./integrationtests/client/admin-api

go: unknown flag -build=integration cannot be used with -c

Also... one cannot use the -c flag across multiple packages:

$ go test -c ./...

cannot use -c flag with multiple packages

Any ideas?

CodePudding user response:

You can use the go test -run flag and pass it a pattern you know will never match:

go test -run=XXX_SHOULD_NEVER_MATCH_XXX ./...

ok      mypkg       0.731s [no tests to run]

this will catch any test compilation errors - and if there are none - no tests will be run.


Full details on the -run flag from the inline (go help testflag) docs:

        -run regexp
            Run only those tests, examples, and fuzz tests matching the regular
            expression. For tests, the regular expression is split by unbracketed
            slash (/) characters into a sequence of regular expressions, and each
            part of a tests identifier must match the corresponding element in
            the sequence, if any. Note that possible parents of matches are
            run too, so that -run=X/Y matches and runs and reports the result
            of all tests matching X, even those without sub-tests matching Y,
            because it must run them to look for those sub-tests.
  • Related