Skip to content

Testing Platform overwriting/removing user console output #4425

Open

Description

Using TUnit, I created this simple test suite:

public class Tests
{
    [Before(TestSession)]
    public static async Task BeforeTestSession()
    {
        Console.WriteLine($"BeforeTestSession started at {DateTime.Now}");
        await Task.Delay(TimeSpan.FromSeconds(5));
        Console.WriteLine($"BeforeTestSession ended at {DateTime.Now}");
    }
    
    [Before(Assembly)]
    public static async Task BeforeAssembly()
    {
        Console.WriteLine($"BeforeAssembly started at {DateTime.Now}");
        await Task.Delay(TimeSpan.FromSeconds(5));
        Console.WriteLine($"BeforeAssembly ended at {DateTime.Now}");
    }
    
    [Test]
    public void Test1()
    {
        Console.WriteLine($"Executing Test at {DateTime.Now}");
    }
}

If you just do a dotnet run on the CLI, you'll have to be quick, but when it starts to show "BeforeAssembly started", you'll see it render really quickly, but then it'll be removed and overwritten by the rendering of the [✓0/x0/↓0] ConsoleApp5.dll (net9.0|x64).

I guess this is related to the new interactive output and showing running tests etc. But overwriting user output means they may lose visibility of important information.

Activity

  1. Evangelink commented on Dec 22, 2024

    @Evangelink
    Member

    I'm off for now so just dumping my thoughts for when I return. We need to try if the behavior is the same when using IOutput to display messages.

  2. added
    area/mtp
    area/terminal-reporter
    and removed
    needs/triage
    on Jan 9, 2025
  3. Evangelink commented on Jan 9, 2025

    @Evangelink
    Member

    I won't be able to investigate more right now but dumping more info of trying to repro:

    Image

    The before and after on assembly are not displayed at all while the test session is.

  4. Evangelink commented on Jan 9, 2025

    @Evangelink
    Member

    Jakub Jareš (@nohwnd) What's your opinion and recommendations here?

  5. thomhurst commented on Jan 9, 2025

    @thomhurst
    ContributorAuthor

    The before and after on assembly are not displayed at all while the test session is.

    From what I saw the Before assembly happens really quickly, but then immediately after TUnit will pick up the first test and send a test node update with InProgressState, which is probably causing the output terminal to update and re-render.

  6. nohwnd commented on Jan 9, 2025

    @nohwnd
    Member

    This is bound to happen when the progress is enabled, and we write to the screen from a different place, e.g from the test directly without capturing the output.

    There is no way around unless we substitute the Console.Out with one that is respecting the lock we have in progress reporting.

    #3491

    Similarly this can happen when we run in child process and parent process (testhost controller writes to the screen).

  7. Youssef1313 commented on Jan 15, 2025

    @Youssef1313
    Member

    Jakub Jareš (@nohwnd) I didn't look into how the progress-aware terminal works, but I think at whatever point the position of writing is determined, to the end of writing, should be wrapped in lock (Console.Out).

    In short, we can already lock on Console.Out which is implicitly the same lock as user writing to console. So, no need to substitute Console.Out with one that respects our lock. Instead we will respect the lock used by runtime's SyncTextWriter for the default console.

  8. Evangelink commented on Nov 24, 2026

    @Evangelink
    Member

    I reproduced this with the versions in the report: with progress enabled, only two of the five hook messages remained visible. Routing the same messages through MTP's IOutputDevice using TextOutputDeviceData kept all five visible without disabling progress.

    This appears to be a limitation of writing directly to the console while the cursor-based progress renderer is active: a write between redraws moves the cursor, so the next redraw can erase user output. Locking Console.Out around progress rendering would not prevent that. For frameworks such as TUnit, the better fix is to send live hook messages through MTP's output device.

    As a workaround, --no-progress preserved all five messages on MTP 1.4.3. On TUnit 1.87.12 / MTP 2.5.3, --output detailed --show-stdout all --progress off also kept them visible live.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area/mtparea/terminal-reporter

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions