microsoft / testfx

MSTest framework and adapter
MIT License
749 stars 255 forks source link

Set encoding in console to UTF8 #2491

Open nohwnd opened 8 months ago

nohwnd commented 8 months ago

Describe the bug

Frameworks or users might use utf8 characters in their output messages. Set console encoding to utf8 (as we do in vstest.console, and as xunit.console does). https://github.com/microsoft/vstest/issues/4605

Optionally put this under environment variable feature flag.

Steps To Reproduce

image

<!-- file mstest108.csproj -->
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>
    <EnableMSTestRunner>true</EnableMSTestRunner>
    <OutputType>Exe</OutputType>
    <GenerateTestingPlatformEntryPoint>false</GenerateTestingPlatformEntryPoint>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="MSTest" Version="3.2.0" />
    <PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="17.10.1" />
  </ItemGroup>

  <ItemGroup>
    <Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
  </ItemGroup>

</Project>
// file MSTestSettings.cs
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]
// file UnitTest1.cs
namespace mstest108;

using Microsoft.Testing.Platform;
using Microsoft.Testing;
using Microsoft.Testing.Framework;
using Microsoft.Testing.Platform.Builder;

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        Console.OutputEncoding = System.Text.Encoding.UTF8;
        Assert.AreEqual("a", "🤨😘");
    }
}

public static class Program {
    public static async Task<int> Main(string[] args)  {

        Console.OutputEncoding = System.Text.Encoding.UTF8;
                ITestApplicationBuilder builder = await TestApplication.CreateBuilderAsync(args);
        Microsoft.Testing.Platform.MSBuild.TestingPlatformBuilderHook.AddExtensions(builder, args);
        Microsoft.Testing.Extensions.Telemetry.TestingPlatformBuilderHook.AddExtensions(builder, args);
        Microsoft.VisualStudio.TestTools.UnitTesting.TestingPlatformBuilderHook.AddExtensions(builder, args);
        Microsoft.Testing.Extensions.CodeCoverage.TestingPlatformBuilderHook.AddExtensions(builder, args);
        using ITestApplication app = await builder.BuildAsync();
        return await app.RunAsync();
    }
}

mstest108.zip

Expected behavior

I can see the smileys.

Actual behavior

I see ??

Additional context

MarcoRossignoli commented 8 months ago

I don't think it's a bug...we don't do nothing and we use the default of the platform.

I think that the adapter should take this responsibility as platform we don't own or know nothing about "what" the plugged testing platform does...it could not send messages to the output device at all.

Another idea can be if we think that's a good "default" for our default console "set by default" utf-8 and provide some "command line args" to change it to something else like '--console-output-encoding xxx'

Waiting for @Evangelink thought

Evangelink commented 8 months ago

Another idea can be if we think that's a good "default" for our default console "set by default" utf-8 and provide some "command line args" to change it to something else like '--console-output-encoding xxx'

Haven't put much thoughts but this seems the best. A "good" default and an easy way to change it.

I don't know how busy is the sprint but if there is some room for it, I would try to work on it for 3.4 (not sure if you already did the feature complete preview release for 3.3).

nohwnd commented 8 months ago

I don't think this can be purely testing framework concern. The encoding needs to align with msbuild integration for dotnet test, if we want to transport the standard output data correctly, otherwise we are losing encoding and information. This is not so painful for english speaking countries, where utf-8 is mostly used in form of emojis that are rarely used in test names and output, but without this we will be breaking chinese and other characters.

MarcoRossignoli commented 8 months ago

The encoding needs to align with msbuild integration for dotnet test, if we want to transport the standard output data correctly, otherwise we are losing encoding and information.

Yep, this needs to be followed also for "custom" possible output display in case.

Evangelink commented 6 months ago

The encoding needs to align with msbuild integration for dotnet test

Seems like having an option to change the encoding and having dotnet test logic calling this option based on current msbuild setting is the logical split. Platform allows to change console encoding and dotnet test that is aware of MSBuild sets the value.