-
Notifications
You must be signed in to change notification settings - Fork 384
Add ITestOutputHelper to MockConsole for visible console output in test failures #5700
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
base: main
Are you sure you want to change the base?
Conversation
… CollectCommand tests Co-authored-by: mdh1418 <16830051+mdh1418@users.noreply.github.com>
… handling Co-authored-by: mdh1418 <16830051+mdh1418@users.noreply.github.com>
Co-authored-by: mdh1418 <16830051+mdh1418@users.noreply.github.com>
…ilures Co-authored-by: mdh1418 <16830051+mdh1418@users.noreply.github.com>
mdh1418
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot can we also update the ConsoleExporterTests to use the ITestOutputHelper as well
…leExporterTests Co-authored-by: mdh1418 <16830051+mdh1418@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR improves diagnosability of failing tests by making MockConsole optionally mirror console output to xUnit’s ITestOutputHelper, and updates several test suites to pass the output helper into MockConsole.
Changes:
- Added optional
ITestOutputHelperplumbing toMockConsole, including line-buffering and an explicitFlushOutput()method. - Updated
CollectCommandFunctionalTests,CollectLinuxCommandFunctionalTests, andConsoleExporterTeststo injectITestOutputHelperand pass it toMockConsole.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/tests/Common/MockConsole.cs | Adds optional xUnit output multiplexing, buffering, and flush integration in assert helpers. |
| src/tests/dotnet-trace/CollectCommandFunctionalTests.cs | Injects ITestOutputHelper and routes console output through MockConsole. |
| src/tests/dotnet-trace/CollectLinuxCommandFunctionalTests.cs | Injects ITestOutputHelper and routes console output through MockConsole across test cases. |
| src/tests/dotnet-counters/ConsoleExporterTests.cs | Injects ITestOutputHelper and passes it to MockConsole for better failure output. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/tests/Common/MockConsole.cs
Outdated
| catch (InvalidOperationException ex) when (ex.Message.Contains("no currently active test")) | ||
| { | ||
| // ITestOutputHelper.WriteLine throws InvalidOperationException with message | ||
| // "There is no currently active test" when called after the test has completed. | ||
| // This is expected and can be safely ignored. | ||
| } |
Copilot
AI
Jan 28, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exception filter is case-sensitive: ex.Message.Contains("no currently active test") will NOT match the message quoted in the comment ("There is no currently active test...") due to casing. This means InvalidOperationException can still escape when writes happen after the test completes. Use a case-insensitive check (e.g., IndexOf(..., StringComparison.OrdinalIgnoreCase) >= 0) or compare against the exact expected message consistently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 61ff689. Changed to use StringComparison.OrdinalIgnoreCase for case-insensitive matching.
src/tests/Common/MockConsole.cs
Outdated
| _outputHelper.WriteLine(_outputBuffer.ToString()); | ||
| _outputBuffer.Clear(); | ||
| } | ||
| catch (InvalidOperationException ex) when (ex.Message.Contains("no currently active test")) |
Copilot
AI
Jan 28, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same case-sensitive exception filter issue here: ex.Message.Contains("no currently active test") won’t match "There is no currently active test" due to casing, so FlushOutput() can throw after test completion. Make the match case-insensitive or use the exact message string consistently.
| catch (InvalidOperationException ex) when (ex.Message.Contains("no currently active test")) | |
| catch (InvalidOperationException ex) when (ex.Message.Contains("no currently active test", StringComparison.OrdinalIgnoreCase)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 61ff689. Changed to use StringComparison.OrdinalIgnoreCase for case-insensitive matching.
|
@copilot apply changes based on the comments in this thread |
|
Example outputs when there are failures.
./test.sh |
Co-authored-by: mdh1418 <16830051+mdh1418@users.noreply.github.com>
Applied the suggested changes in 61ff689. Both exception filters now use case-insensitive comparison with |
src/tests/Common/MockConsole.cs
Outdated
| } | ||
| public override void Write(string text) | ||
| { | ||
| if (_outputHelper != null && !string.IsNullOrEmpty(text)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot - please factor this new code out into a helper function such as BufferTestLogging
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in a518e4c. Extracted the test logging buffer logic into a BufferTestLogging helper method.
src/tests/Common/MockConsole.cs
Outdated
| /// Flushes any remaining buffered output to the ITestOutputHelper. | ||
| /// Call this at the end of a test to ensure all output is written. | ||
| /// </summary> | ||
| public void FlushOutput() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot - please rename this so its clear it refers to the test logging. For example FlushTestLogging
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in a518e4c. Renamed to FlushTestLogging to clarify it refers to test logging output.
…FlushTestLogging Co-authored-by: noahfalk <6243776+noahfalk@users.noreply.github.com>
Improve Console Output Visibility in CollectCommand Tests
This PR addresses issue #5698 by making console output visible when tests fail.
Changes completed:
Key features:
Result:
Console output including error messages and exceptions is now visible in test output under "Standard Output Messages:", making it much easier to diagnose issues when tests fail.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.