testfx icon indicating copy to clipboard operation
testfx copied to clipboard

Displaying immediate output of executing tests on console

Open Suremaker opened this issue 7 years ago • 2 comments

Description

Hello, I would like to ask a question if there is currently any way to display immediate output of currently executing tests in MsTest?

The use case for that scenario is reporting progress of long running tests on CI (like slower acceptance or end to end tests).

So far I see that the only way to report anything from the test is to use Console.WriteLine(), however all the console output is captured during test execution and reported only after test is finished.

I am wondering if there is any mechanism present, allowing to print message on console during test execution (using dotnet test or similar command)?

In nunit it is possible to achieve it by using TestContext.Progress.WriteLine("message"); and dotnet test or nunit3-console.exe commands:

[TestFixture]
public class SampleTest
{
    [Test]
    public void Test_reporting_immediate_progress()
    {
        TestContext.Progress.WriteLine("before operation");
        Thread.Sleep(2000);
        TestContext.Progress.WriteLine("after operation");
    }
}

In xunit, the similar behaviour can be achieved by using Console.WriteLine("message"); and dotnet xunit or xunit.console.exe commands:

public class SampleTest
{
    [Fact]
    public void Test_reporting_immediate_progress()
    {
        Console.WriteLine("before operation");
        Thread.Sleep(2000);
        Console.WriteLine("after operation");
    }
}

If there is currently no such mechanism present, are there any considerations to add it in future? Thanks.

Suremaker avatar Jan 11 '18 23:01 Suremaker

@Suremaker : Thank you for your query but I am afraid currently in-progress reporting is not supported by mstest. Tagging @pvlakshm for answering the future considerations part.

jayaranigarg avatar Jan 12 '18 21:01 jayaranigarg

using (var writer = new System.IO.StreamWriter(System.Console.OpenStandardOutput()))
       writer.WriteLine("This will show up!");

Worked for me under linux at least

wizofaus avatar Nov 21 '18 00:11 wizofaus

✅ Successfully linked to Azure Boards work item(s):

testplatform-bot avatar Mar 17 '24 17:03 testplatform-bot

We will introduce a new runsettings entry to let users decide whether or not to capture console calls. This entry will be enabled by default to avoid breaking changes.

Evangelink avatar Mar 17 '24 17:03 Evangelink

@Suremaker MSTest has got one option that you can use to "disable" the capture of the output using a runsettings option as explained in the documentation

https://learn.microsoft.com/en-us/visualstudio/test/configure-unit-tests-by-using-a-dot-runsettings-file?view=vs-2022#mstest-element

The options is CaptureTraceOutput and should be set to false.

MarcoRossignoli avatar Mar 20 '24 18:03 MarcoRossignoli