Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: add go test -skip to skip specific tests #41583

Closed
dprotaso opened this issue Sep 23, 2020 · 44 comments
Closed

cmd/go: add go test -skip to skip specific tests #41583

dprotaso opened this issue Sep 23, 2020 · 44 comments

Comments

@dprotaso
Copy link

t.Skip() is an option but it requires changing the test code.

It's hard to write a correct regex to pass to the -run flag in order to skip specific tests. This is due to the lack of lookaheads in the regexp library. Previous discussion here: https://groups.google.com/g/golang-nuts/c/7qgSDWPIh_E?pli=1

@dprotaso dprotaso changed the title Make it easier to skip tests with go test CLI Make it easier to skip tests with the go test CLI Sep 23, 2020
@rsc rsc added this to Incoming in Proposals (old) Sep 23, 2020
@ianlancetaylor ianlancetaylor changed the title Make it easier to skip tests with the go test CLI cmd/go: make it easier to skip tests with the go test CLI Sep 24, 2020
@ianlancetaylor
Copy link
Contributor

Do you have a specific suggestion?

To run specific tests you can already write go test -run="TestOne|TestTwo|TestThree".

@ianlancetaylor ianlancetaylor added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Sep 24, 2020
@ianlancetaylor ianlancetaylor changed the title cmd/go: make it easier to skip tests with the go test CLI proposal: cmd/go: make it easier to skip tests with the go test CLI Sep 24, 2020
@gopherbot gopherbot added this to the Proposal milestone Sep 24, 2020
@dprotaso
Copy link
Author

Do you have a specific suggestion?

I didn't want to prescribe a solution but here's some off the top of my head

  1. expand run flag functionality - -run [regex] to allow look aheads - thus negation

    • pros
      • most people are familiar with this flag
    • cons
      • requires expanded regex implementation
  2. new flag -skip [regex]

    • pros
      • mirrors run flag
      • may be easier to use than a negative regex
    • cons
      • requires definition of precedence between -skip& -run
      • it'd be nice to see skipped test output - that might require more work to implement

To run specific tests you can already write go test -run="TestOne|TestTwo|TestThree".

This doesn't scale if I have O(10s) of tests in a package and want to skip one or two

@dprotaso
Copy link
Author

dprotaso commented Oct 6, 2020

hey @ianlancetaylor - do you have any additional questions or want me to clarify anything above?

@ianlancetaylor
Copy link
Contributor

No, I'm good.

This issue is in the proposal process now and will get into the proposal committee in due course. The committee looks at a lot of issues (see minutes at #33502), so it won't be immediately, but it shouldn't take too long.

Thanks.

@rsc
Copy link
Contributor

rsc commented Oct 7, 2020

Almost no one on earth understands how to write negation regexps correctly, so I would lean toward -skip if we do this.

@bcmills
Copy link
Contributor

bcmills commented Oct 7, 2020

requires definition of precedence between -skip & -run

I can see only two reasonable definitions.

  1. Define that the default value of -run matches everything, and therefore -skip takes precedence over -run.
  2. Define that the default value of -run imposes no constraints, and therefore it is an error to specify values of -run and -skip that both match the same test.

I have a slight preference for (1), because it treats the default value of the -run flag the same as other values.

@jimmyfrasche
Copy link
Member

Does precedence mean the order the filters are applied in or which is used if both are given?

If it's the order, then -run followed by -skip makes the most sense to me and seems more useful than disallowing the combination.

@bcmills
Copy link
Contributor

bcmills commented Oct 7, 2020

Yeah, it seems especially useful in the context of subtests:

go test foo -run=TestBar -skip=TestBar/flaky_subtest

@dprotaso
Copy link
Author

dprotaso commented Oct 7, 2020

Does precedence mean the order the filters are applied in or which is used if both are given?

The latter in my opinion

@rsc rsc moved this from Incoming to Active in Proposals (old) Oct 8, 2020
@ChrisHines
Copy link
Contributor

I have almost moved some subtests into their own top level test function on a few occasions exactly because there was not a good way to skip some subset of them. I have also had to run go test -list iterations just to figure out a regex that would run all tests except a select few, and the resulting regex is often overly long given the use case of wanting to skip a small subset when a package has a lot of tests.

@rsc
Copy link
Contributor

rsc commented Oct 14, 2020

It sounds like people are generally in favor of adding -skip with a default of "matches nothing at all".
The -skip setting applies after the -run setting (which already defaults to "match everything")
and only applies to tests (not benchmarks).

Do I have that right? Does anyone object to adding -skip as described?

@rsc rsc changed the title proposal: cmd/go: make it easier to skip tests with the go test CLI proposal: cmd/go: add go test -skip to skip specific tests Oct 14, 2020
@bcmills
Copy link
Contributor

bcmills commented Oct 14, 2020

The -skip setting … only applies to tests (not benchmarks).

Hmm. It would be unfortunate to have an easy way to skip specific tests but not specific benchmarks.

I think it would make sense to either have -skip apply to both tests and benchmarks, or to add a separate -benchskip or similar flag for those. (It seems a bit simpler to me to make -skip also apply to benchmarks, but I don't have a strong preference either way.)

@dprotaso
Copy link
Author

Do I have that right? Does anyone object to adding -skip as described?

Sounds good to me

Hmm. It would be unfortunate to have an easy way to skip specific tests but not specific benchmarks.

Expanding the scope of -skip to cover benchmarks makes sense to me. I don't mind if this comes later if it's not a trivial thing to do.

@ChrisHines
Copy link
Contributor

ChrisHines commented Oct 14, 2020

A way to skip benchmarks would be welcome as well.

The -run and -bench flags have different defaults. In my experience go test -run=^$ -bench=. is pretty common today, but would people start trying to use go test -skip=. -bench=. to mean the same thing? It seems to me a separate -benchskip flag would avoid cross pollinating the two sets and reduce the chances for surprise.

@magical
Copy link
Contributor

magical commented Oct 18, 2020

Instead of introducing a new flag, could we add some syntax to the existing flag? Let a pattern starting with ! invert the sense of the match. So go test -run=!Foo|Bar would run all tests that don't match Foo or Bar, same as the proposed -skip flag.

This avoids introducing new flags (-skip and -benchskip) and avoids issues about whether -run or -skip takes precedence. -run already has special handling for / so it is already not a straight regexp.

@dprotaso
Copy link
Author

dprotaso commented Oct 19, 2020

-run already has special handling for / so it is already not a straight regexp.

Truthfully I was tripped up by this when I first encountered it

@bcmills
Copy link
Contributor

bcmills commented Oct 19, 2020

@magical, further complications in the syntax for the -skip flag would be pretty awkward for the “run a specific test but skip a specific subtest” use-case mentioned previously.

@bcmills
Copy link
Contributor

bcmills commented Oct 19, 2020

@ChrisHines, I think the question is, would someone running go test -bench=BenchmarkFoo -skip=BenchmarkFoo/some_subbench expect it to skip BenchmarkFoo/some_subbench?

I would argue that pretty much everyone would expect that to skip the sub-benchmark, so I suspect that a unified -skip flag would be less confusing than a separate -benchskip flag.

@rsc
Copy link
Contributor

rsc commented Oct 20, 2020

The -run and -bench flags have different defaults. In my experience go test -run=^$ -bench=. is pretty common today

FWIW this is usually a bad idea, at least interactively, since you inevitably end up benchmarking broken code. (And boy can you make broken code run fast!)

@dkegel-fastly
Copy link

Would love to see this land!

@wojciechka
Copy link

Would love to be able to do this to disable flaky tests in CI.

@mvdan
Copy link
Member

mvdan commented Mar 3, 2022

I still intend to get to this for 1.19. There's no more need for more comments to just say it would be nice to have :) See https://github.com/golang/go/wiki/NoPlusOne.

@linzhp
Copy link
Contributor

linzhp commented Apr 2, 2022

Can you make it support skipping multiple tests from different packages? Like:

go test -skip "pkg1.TestFoo|pkg2.TestBar" ./...

It would be useful to disable flaky tests in different packages on CI

izaaklauer added a commit to hashicorp/waypoint that referenced this issue Apr 20, 2022
Currently, there's no good way for any other project consuming these tests to selectively skip any of them. Maybe there will be [as of go 1.19](golang/go#41583), but until then, this introduces a new "skipTests" argument that allows users to pass in test names to skip.
@mvdan
Copy link
Member

mvdan commented May 8, 2022

@linzhp that sounds like it should be a separate issue or proposal, because it should also affect the -run flag, and this issue does not cover changing the -run flag in any way. The two flags will take the same form of pattern in this proposal, and I don't see a reason why we should make them inconsistent.

@linzhp
Copy link
Contributor

linzhp commented Aug 5, 2022

Any plan to implement this? It seems not in Go 1.19 yet

@mvdan mvdan removed their assignment Aug 5, 2022
@mvdan
Copy link
Member

mvdan commented Aug 5, 2022

I clearly did not get to this for 1.19, so I'm unassigning myself for now. There is no plan or estimate right now; the issue is in the backlog milestone.

@gopherbot
Copy link

Change https://go.dev/cl/421439 mentions this issue: cmd/go, testing: add go test -skip flag

@mvdan
Copy link
Member

mvdan commented Aug 5, 2022

Not entirely sure whether that was a coincidence :)

@06kellyjac
Copy link

Woah, next release can I finally stop sed-ing individual tests to skip? 🎉🎊

@gopherbot
Copy link

Change https://go.dev/cl/449075 mentions this issue: doc/go1.20: add release notes for cmd/go changes

gopherbot pushed a commit that referenced this issue Nov 14, 2022
Updates #41696.
Updates #50332.
Updates #41583.

Change-Id: I99e96a2996f14da262570a5cb5273dcdce45df2b
Reviewed-on: https://go-review.googlesource.com/c/go/+/449075
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
JAORMX pushed a commit to metal-toolbox/audito-maldito that referenced this issue Mar 3, 2023
This implementation is based on a prior discussion with Ozz.

I spent some time investigating other patterns for designing
integration tests. See below for my notes on that.

testing.Short approach:

Another possible pattern is to use the "testing.Short" function
to check if the user requested only short tests run. The issue
with this is integration tests *and* unit tests will be run by
default. Also, there is no way to only run integration tests
using this pattern. This idea is discussed on stackoverflow. [1]

"go test -run|-skip <regex> ..." approach:

There is another pattern we could implement when Go 1.20 is
released. Currently, it is possible to filter tests by their
names using "-run <test-name-regex>". By naming integration
tests "Test(...)Integration" (or some other naming pattern),
we could filter tests by running:

  go test -run '.*Integration.*' -v ./...

The issue with this approach is there is no way to explicitly
skip tests - thus "go test" on its own will run both unit tests
and integration tests. It is theoretically possible to invert
regex - but it is difficult to read. [2] Rather than create
unreadable regex, we could wait for Go 1.20. Luckily for us,
Go 1.20 introduces the "-skip" argument for "go test". [3]
This new argument would make this approach feasible.

References:

1. https://stackoverflow.com/a/41407042
2. https://stackoverflow.com/a/406408
3. golang/go#41583
@golang golang locked and limited conversation to collaborators Nov 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

No branches or pull requests