testing: deferred func runs before parallel subtest #22993
Comments
https://blog.golang.org/subtests says:
which might explain this behavior, but it conflicts with the T documentation in that it breaks T.Run's result. |
Perhaps the core issue is that the T documentation doesn't specify the ordering of a parent test function returning and its parallel subtest goroutine resuming after its T.Parallel call (and also that a T.Run call in a parent test returns true immediately when T.Parallel is called in a subtest, even if that subtest later fails). |
I think this is an issue to be fixed with better documentation. CC @mpvl |
Change https://golang.org/cl/83880 mentions this issue: |
Also, Run's result was wrong.
What did you do?
I deferred a function in a package-scope test, then ran a parallel subtest that assumed the deferred function would run after it completes, but this wasn't the case, and Run returned true when the subtest failed.
Basically, this:
It doesn't matter whether the package-scope test is parallel.
A full example: https://play.golang.org/p/pMsKhRvjiW. Note that you'll have to run it locally.
The documentation for testing.T.Run says:
This seems to say that Run always returns an accurate status of the subtest, even if it's parallel, which means the subtest must finish before the Run call completes, and thus before the parent test returns, after which its deferred functions run, but I'm seeing such a deferred function run first. The discussion in https://golang.org/pkg/testing/#hdr-Subtests_and_Sub_benchmarks similarly indicates to me this is incorrect behavior, specifically the paragraphs starting with "Subtests can also be used to control parallelism" and "Run does not return until parallel subtests have completed".
The documentation for testing.T.Parallel says:
This doesn't seem to augment Run's documentation's guarantees, but I'm not sure.
I feel like I must be missing something obvious, but a colleague agreed with my interpretation.
What did you expect to see?
What did you see instead?
System details
The text was updated successfully, but these errors were encountered: