Skip to content

Conversation

@thomhurst
Copy link
Owner

Problem

Console output was being truncated when tests completed, particularly when tests ended with Console.Write() without a newline. Users reported that "the end of the output is cut" even after version 1.12.41 (which included the previous fix in c01a6fc).

Example scenario:

Console.WriteLine("Start");
Console.Write("End (no newline)"); // This output was lost!

Root Cause

While commit c01a6fc fixed parallel output mixing by using per-context buffers, it didn't address output truncation at test completion.

When a test ends, any buffered console output (from Console.Write() without newline) remains in the test context's line buffer but is never flushed to the sinks. This buffered output is lost and doesn't appear in test results or IDE output.

Solution

Added explicit flushing of console interceptors (stdout and stderr) in TestCoordinator.ExecuteTestInternalAsync() finally block:

try
{
    await Console.Out.FlushAsync().ConfigureAwait(false);
    await Console.Error.FlushAsync().ConfigureAwait(false);
}
catch (Exception flushEx)
{
    await _logger.LogErrorAsync($"Error flushing console output for {test.TestId}: {flushEx}").ConfigureAwait(false);
}

This ensures all buffered output is captured before test results are reported.

Why this location?

  • ✅ Runs after test execution completes
  • ✅ Runs in finally block (always executes, even on exception)
  • ✅ Runs BEFORE result reporting (so output is available)
  • ✅ Runs while TestContext.Current still points to the test
  • ✅ Handles both stdout and stderr
  • ✅ Error handling prevents flush failures from breaking tests

Testing

  • Added OutputTruncationTests.cs - Tests specifically for output ending without newline
  • Enhanced ParallelConsoleOutputTests.cs - Added Test4 for truncation scenario
  • Existing tests - All parallel output isolation tests continue to pass

Impact

  • ✅ Tests ending with Console.Write() now capture all output
  • ✅ No output loss in Visual Studio Test Explorer
  • ✅ Works with both parallel and [NotInParallel] tests
  • ✅ Maintains existing parallel output isolation

Fixes #4545

🤖 Generated with Claude Code

…#4545)

## Problem
Console output was being truncated when tests completed, particularly when
tests ended with Console.Write() without a newline. Users reported that "the
end of the output is cut" even after the previous fix in c01a6fc.

## Root Cause
While commit c01a6fc fixed parallel output mixing by using per-context
buffers, it didn't address output truncation at test completion. When a test
ends, any buffered console output (from Console.Write without newline) remains
in the test context's line buffer but is never flushed to the sinks. This
buffered output is lost and doesn't appear in test results or IDE output.

## Solution
Added explicit flushing of console interceptors (stdout and stderr) in the
TestCoordinator.ExecuteTestInternalAsync() finally block. This ensures all
buffered output is captured before test results are reported.

The flush happens:
- After test execution completes
- In the finally block (always executes, even on exception)
- Before result reporting (so output is available in results)
- While TestContext.Current still points to the test context
- With error handling to prevent flush failures from breaking tests

## Testing
- Added OutputTruncationTests with tests ending in Console.Write()
- Enhanced ParallelConsoleOutputTests with truncation test case
- Existing parallel output isolation tests continue to pass

Fixes #4545

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@thomhurst
Copy link
Owner Author

Summary

Adds console output flushing in the test cleanup path to prevent loss of buffered output from Console.Write() calls without newlines.

Critical Issues

None found ✅

Suggestions

None - the implementation is solid:

  • Correctly placed early in the finally block (before message bus reporting)
  • Proper async/await with ConfigureAwait(false)
  • Error handling prevents flush failures from breaking tests
  • Good test coverage for the fix

The fix addresses the root cause described in the PR: buffered output (from Console.Write() without newline) now gets flushed before test results are reported, ensuring all console output is captured.

Verdict

APPROVE - No critical issues

@thomhurst thomhurst disabled auto-merge January 24, 2026 17:45
@thomhurst thomhurst merged commit 9a7eee7 into main Jan 24, 2026
13 checks passed
@thomhurst thomhurst deleted the fix/4545-console-output-truncation branch January 24, 2026 17:45
This was referenced Jan 25, 2026
This was referenced Feb 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Log outputs when running multiple tests in parallel are either missing or incomplete

2 participants