3

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.

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • 1
    "**so I can catch build errors introduced by PRs that the default go test compilation and execution would not catch.**" How would you execute the tests without building them? – Hymns For Disco Jun 22 '22 at 20:39
  • Sorry - some clarification. By "the default go test execution," I mean `go test ./...`. That test execution would not build (nor run) any tests that have build flags. You can have a compiler error in a test file that has a build flag and `go test ./...` will not throw an error like you would expect. – Brendan Ashton Jun 28 '22 at 17:05

1 Answers1

5

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.

If you need to pass any build tags that are typically passed during your go build process (e.g. go build -tags mytag), you can do the same during go test:

go test -tags mytag -run=XXX_SHOULD_NEVER_MATCH_XXX ./...

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.
colm.anseo
  • 19,337
  • 4
  • 43
  • 52
  • Cool idea! I couldn't get `go test -build integration -run ASDFASDF ./...` to work though. That one doesn't catch a compiler error I introduced in a test file. `build constraints exclude all Go files` – Brendan Ashton Jun 28 '22 at 17:20
  • Turns out I can just use `-tags integration` instead of `-build integration` and this works great! – Brendan Ashton Jun 28 '22 at 20:56
  • updated answer to include the `-tags` note. – colm.anseo Jun 29 '22 at 21:24
  • I am seeing this approach taking a really long time when I include this command as part of a jenkins job and multiple builds of that job get triggered on the jenkins node. is this expected? At times (with the parallel jobs) the command takes almost 30-35 minutes for a really small repository. – Abhijeet Vaikar Oct 02 '22 at 06:51