From 43b93af47074ce1aa791972f5ff2c3dd58eb2f0c Mon Sep 17 00:00:00 2001 From: Azat Mukhametshin Date: Tue, 1 Dec 2026 16:30:36 +0100 Subject: [PATCH 1/2] Honor verbosity for informational messages in MSBuildLogger MSBuildLogger ignored the Verbosity parameter that VSTestTask2 passes, so informational test run messages were always shown when dotnet test used the terminal logger. Drop them at quiet and minimal, like ConsoleLogger. Fixes #16728 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- src/vstest.console/Internal/MSBuildLogger.cs | 20 ++++ .../Internal/MSBuildLoggerTests.cs | 91 +++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 test/vstest.console.UnitTests/Internal/MSBuildLoggerTests.cs diff --git a/src/vstest.console/Internal/MSBuildLogger.cs b/src/vstest.console/Internal/MSBuildLogger.cs index b831cfb09f..b1c63f2fbe 100644 --- a/src/vstest.console/Internal/MSBuildLogger.cs +++ b/src/vstest.console/Internal/MSBuildLogger.cs @@ -41,6 +41,12 @@ protected static IOutput? Output private set; } + /// + /// Gets the verbosity passed by VSTestTask2. Defaults to , + /// so informational messages are forwarded when no verbosity is given. + /// + internal ConsoleLogger.Verbosity VerbosityLevel { get; private set; } = ConsoleLogger.Verbosity.Normal; + // Default constructor is needed for a logger to be able to activate it. public MSBuildLogger() { } @@ -67,6 +73,14 @@ public void Initialize(TestLoggerEvents events, string testRunDirectory) public void Initialize(TestLoggerEvents events, Dictionary parameters) { + ValidateArg.NotNull(parameters, nameof(parameters)); + + if (parameters.TryGetValue(ConsoleLogger.VerbosityParam, out var verbosity) + && Enum.TryParse(verbosity, ignoreCase: true, out ConsoleLogger.Verbosity verbosityLevel)) + { + VerbosityLevel = verbosityLevel; + } + Initialize(events, string.Empty); } @@ -75,6 +89,12 @@ private void TestMessageHandler(object? sender, TestRunMessageEventArgs e) switch (e.Level) { case TestMessageLevel.Informational: + // Same policy as ConsoleLogger: hide informational messages at quiet and minimal verbosity. + if (VerbosityLevel is ConsoleLogger.Verbosity.Quiet or ConsoleLogger.Verbosity.Minimal) + { + break; + } + SendMessage($"output-info", e.Message); break; case TestMessageLevel.Warning: diff --git a/test/vstest.console.UnitTests/Internal/MSBuildLoggerTests.cs b/test/vstest.console.UnitTests/Internal/MSBuildLoggerTests.cs new file mode 100644 index 0000000000..64697c8bc2 --- /dev/null +++ b/test/vstest.console.UnitTests/Internal/MSBuildLoggerTests.cs @@ -0,0 +1,91 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Generic; + +using Microsoft.VisualStudio.TestPlatform.CommandLine.Internal; +using Microsoft.VisualStudio.TestPlatform.Common.Logging; +using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; +using Microsoft.VisualStudio.TestPlatform.Utilities; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Moq; + +namespace vstest.console.UnitTests.Internal; + +[TestClass] +public class MSBuildLoggerTests +{ + private readonly List _output = []; + private readonly MSBuildLogger _logger; + + public MSBuildLoggerTests() + { + var mockOutput = new Mock(); + mockOutput.Setup(o => o.WriteLine(It.IsAny(), It.IsAny())) + .Callback((message, _) => + { + lock (_output) + { + _output.Add(message); + } + }); + _logger = new MSBuildLogger(mockOutput.Object); + } + + [TestMethod] + [DataRow("quiet", false)] + [DataRow("minimal", false)] + [DataRow("Minimal", false)] + [DataRow("normal", true)] + [DataRow("detailed", true)] + [DataRow(null, true)] + public void InformationalMessageIsForwardedOnlyAtNormalVerbosityOrHigher(string? verbosity, bool expectedForwarded) + { + RaiseMessages(verbosity, new TestRunMessageEventArgs(TestMessageLevel.Informational, "Informational123")); + + Assert.AreEqual(expectedForwarded, _output.Contains(MSBuildLogger.FormatMessage("output-info", "Informational123"))); + } + + [TestMethod] + [DataRow("quiet")] + [DataRow("minimal")] + public void WarningsAndErrorsAreForwardedAtLowVerbosity(string verbosity) + { + RaiseMessages( + verbosity, + new TestRunMessageEventArgs(TestMessageLevel.Warning, "Warning123"), + new TestRunMessageEventArgs(TestMessageLevel.Warning, "Skipped123 [SKIP]"), + new TestRunMessageEventArgs(TestMessageLevel.Error, "Error123")); + + Assert.AreSequenceEqual( + new[] + { + MSBuildLogger.FormatMessage("output-warning", "Warning123"), + MSBuildLogger.FormatMessage("output-info", "Skipped123 [SKIP]"), + MSBuildLogger.FormatMessage("output-error", "Error123"), + }, + _output); + } + + private void RaiseMessages(string? verbosity, params TestRunMessageEventArgs[] messages) + { + using var loggerEvents = new InternalTestLoggerEvents(TestSessionMessageLogger.Instance); + loggerEvents.EnableEvents(); + + var parameters = new Dictionary(); + if (verbosity is not null) + { + parameters[ConsoleLogger.VerbosityParam] = verbosity; + } + + _logger.Initialize(loggerEvents, parameters); + + foreach (var message in messages) + { + loggerEvents.RaiseTestRunMessage(message); + } + + loggerEvents.WaitForEventCompletion(); + } +} From f1791573692317cdf68c997b540adbb6b7d98bcf Mon Sep 17 00:00:00 2001 From: Azat Mukhametshin Date: Tue, 1 Dec 2026 16:42:29 +0100 Subject: [PATCH 2/2] Address review: stricter unit assertion, acceptance test for verbosity filtering Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../DotnetTestMSBuildOutputTests.cs | 21 +++++++++++++++++++ .../TerminalLoggerTestProject/UnitTest1.cs | 4 ++++ .../Internal/MSBuildLoggerTests.cs | 6 +++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/test/Microsoft.TestPlatform.Acceptance.IntegrationTests/DotnetTestMSBuildOutputTests.cs b/test/Microsoft.TestPlatform.Acceptance.IntegrationTests/DotnetTestMSBuildOutputTests.cs index 2418480f05..047ee2babc 100644 --- a/test/Microsoft.TestPlatform.Acceptance.IntegrationTests/DotnetTestMSBuildOutputTests.cs +++ b/test/Microsoft.TestPlatform.Acceptance.IntegrationTests/DotnetTestMSBuildOutputTests.cs @@ -53,6 +53,27 @@ public void MSBuildLoggerCanBeEnabledByBuildPropertyAndDoesNotEatSpecialChars(Ru ExitCodeEquals(1); } + [TestMethod] + [TestMatrix(console: Net, testHost: Net)] + public void MSBuildLoggerHidesInformationalMessagesBelowNormalVerbosity(RunnerInfo runnerInfo) + { + SetTestEnvironment(_testEnvironment, runnerInfo); + + var projectPath = GetIsolatedTestAsset("TerminalLoggerTestProject.csproj", runnerInfo.TargetFramework); + // MSTest sends this informational message because the test asset sets [assembly: Parallelize]. + const string informationalMessage = "Test Parallelization enabled for"; + + // dotnet test defaults to minimal verbosity, which hides informational messages. + InvokeDotnetTest($@"{projectPath} -tl:on -nodereuse:false /p:PackageVersion={IntegrationTestEnvironment.LatestLocallyBuiltNugetVersion}", workingDirectory: Path.GetDirectoryName(projectPath)); + StdOutputContains("TESTERROR"); + StdOutputDoesNotContains(informationalMessage); + ExitCodeEquals(1); + + InvokeDotnetTest($@"{projectPath} -tl:on -v:n -nodereuse:false /p:PackageVersion={IntegrationTestEnvironment.LatestLocallyBuiltNugetVersion}", workingDirectory: Path.GetDirectoryName(projectPath)); + StdOutputContains(informationalMessage); + ExitCodeEquals(1); + } + [TestMethod] [TestMatrix(console: Net, testHost: Net)] public void MSBuildLoggerCanBeDisabledByBuildProperty(RunnerInfo runnerInfo) diff --git a/test/TestAssets/TerminalLoggerTestProject/UnitTest1.cs b/test/TestAssets/TerminalLoggerTestProject/UnitTest1.cs index 7d96676c59..06fec93b58 100644 --- a/test/TestAssets/TerminalLoggerTestProject/UnitTest1.cs +++ b/test/TestAssets/TerminalLoggerTestProject/UnitTest1.cs @@ -7,6 +7,10 @@ #endif using Microsoft.VisualStudio.TestTools.UnitTesting; +// MSTest reports this setting as an informational test run message, which +// DotnetTestMSBuildOutputTests uses to check verbosity filtering. One worker keeps execution sequential. +[assembly: Parallelize(Workers = 1, Scope = ExecutionScope.ClassLevel)] + namespace TerminalLoggerUnitTests { /// diff --git a/test/vstest.console.UnitTests/Internal/MSBuildLoggerTests.cs b/test/vstest.console.UnitTests/Internal/MSBuildLoggerTests.cs index 64697c8bc2..6caf67e028 100644 --- a/test/vstest.console.UnitTests/Internal/MSBuildLoggerTests.cs +++ b/test/vstest.console.UnitTests/Internal/MSBuildLoggerTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System; using System.Collections.Generic; using Microsoft.VisualStudio.TestPlatform.CommandLine.Internal; @@ -40,11 +41,14 @@ public MSBuildLoggerTests() [DataRow("normal", true)] [DataRow("detailed", true)] [DataRow(null, true)] + [DataRow("diagnostic", true)] public void InformationalMessageIsForwardedOnlyAtNormalVerbosityOrHigher(string? verbosity, bool expectedForwarded) { RaiseMessages(verbosity, new TestRunMessageEventArgs(TestMessageLevel.Informational, "Informational123")); - Assert.AreEqual(expectedForwarded, _output.Contains(MSBuildLogger.FormatMessage("output-info", "Informational123"))); + Assert.AreSequenceEqual( + expectedForwarded ? [MSBuildLogger.FormatMessage("output-info", "Informational123")] : Array.Empty(), + _output); } [TestMethod]