test: add nicer debug output to mock subscribers #919
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
The test-support mock subscribers currently
println!
a bunch ofdebugging output. The debugging output is displayed if the test panics,
or if the test is run with
--nocapture
. This can be useful forfiguring out what went wrong if a test fails.
In some cases, tests involve multiple subscribers, to test behavior that
results from interactions between subscribers. In this case, there's no
way to tell which subscriber in the test the debug output came from.
Similarly, if a bunch of tests are run at the same time with
--nocapture
, output from different test threads are interleaved,making it impossible to interpret.
Solution
This branch adds names to the mock subscribers, which are prepended to
their debugging output.
By default, the mock subscriber's name is the name of the test
(technically, the name of the thread where it was created, which is
the name of the test unless tests are run with
--test-threads=1
).When a test has only one mock subscriber, this is sufficient. However,
some tests may include multiple subscribers, in order to test
interactions between multiple subscribers. In that case, it can be
helpful to give each subscriber a separate name to distinguish where the
debugging output comes from. Therefore, this branch also adds a
.named
method to the mock subscriber builder, allowing them to be given custom
names in tests with more than one subscriber.