diff --git a/src/Cli/dotnet/commands/dotnet-test/VSTestForwardingApp.cs b/src/Cli/dotnet/commands/dotnet-test/VSTestForwardingApp.cs index f6a1e7e07ce4..c3b6c7b63030 100644 --- a/src/Cli/dotnet/commands/dotnet-test/VSTestForwardingApp.cs +++ b/src/Cli/dotnet/commands/dotnet-test/VSTestForwardingApp.cs @@ -23,6 +23,15 @@ public VSTestForwardingApp(IEnumerable argsToForward) private static string GetVSTestExePath() { + // Provide custom path to vstest.console.dll or exe to be able to test it against any version of + // vstest.console. This is useful especially for our integration tests. + // This is equivalent to specifying -p:VSTestConsolePath when using dotnet test with csproj. + string vsTestConsolePath = Environment.GetEnvironmentVariable("VSTEST_CONSOLE_PATH"); + if (!string.IsNullOrWhiteSpace(vsTestConsolePath)) + { + return vsTestConsolePath; + } + return Path.Combine(AppContext.BaseDirectory, VstestAppName); } diff --git a/src/Tests/dotnet.Tests/dotnet-msbuild/GivenDotnetVsTestForwardingApp.cs b/src/Tests/dotnet.Tests/dotnet-msbuild/GivenDotnetVsTestForwardingApp.cs index 46e56e71c7e7..85866364a5fe 100644 --- a/src/Tests/dotnet.Tests/dotnet-msbuild/GivenDotnetVsTestForwardingApp.cs +++ b/src/Tests/dotnet.Tests/dotnet-msbuild/GivenDotnetVsTestForwardingApp.cs @@ -5,6 +5,7 @@ using FluentAssertions; using Xunit; using System; +using System.IO; namespace Microsoft.DotNet.Cli.MSBuild.Tests { @@ -16,5 +17,23 @@ public void ItRunsVsTestApp() new VSTestForwardingApp(new string[0]) .GetProcessStartInfo().Arguments.Should().EndWith("vstest.console.dll"); } + + [Fact] + public void ItCanUseEnvironmentVariableToForceCustomPathToVsTestApp() + { + string vsTestConsolePath = "VSTEST_CONSOLE_PATH"; + string dummyPath = Path.Join(Path.GetTempPath(), "vstest.custom.console.dll"); + + try + { + Environment.SetEnvironmentVariable(vsTestConsolePath, dummyPath); + new VSTestForwardingApp(new string[0]) + .GetProcessStartInfo().Arguments.Should().EndWith("vstest.custom.console.dll"); + } + finally + { + Environment.SetEnvironmentVariable(vsTestConsolePath, null); + } + } } }