Skip to content

Commit 598d42e

Browse files
nohwndCopilot
andcommitted
Build isolated test assets for single TFM instead of 7
GetIsolatedTestAsset now takes a targetFramework parameter and rewrites the csproj to build only that TFM. Previously, test assets built for all 7 target frameworks (net462;net472;net48;net8.0;net9.0;net10.0;net11.0) even though the test assertions only need output from one. Results on affected tests: - MSBuildLoggerCanBeEnabledByBuildPropertyAndDoesNotEatSpecialChars: 76s -> 15s - RunDotnetTestWithCsprojPassInlineSettings: 62s -> 15s - MSBuildLoggerCanBeDisabledByEnvironmentVariableProperty: 83s -> 66s - MSBuildLoggerCanBeDisabledByBuildProperty: 50s -> ~15s (estimated) - RunDotnetTestWithCsproj: 45s -> ~15s (estimated) - RunSettingsAreLoadedFromProject: 31s -> ~15s (estimated) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8c06876 commit 598d42e

4 files changed

Lines changed: 15 additions & 7 deletions

File tree

test/Microsoft.TestPlatform.Acceptance.IntegrationTests/DotnetTestMSBuildOutputTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void MSBuildLoggerCanBeEnabledByBuildPropertyAndDoesNotEatSpecialChars(Ru
2323
{
2424
SetTestEnvironment(_testEnvironment, runnerInfo);
2525

26-
var projectPath = GetIsolatedTestAsset("TerminalLoggerTestProject.csproj");
26+
var projectPath = GetIsolatedTestAsset("TerminalLoggerTestProject.csproj", runnerInfo.TargetFramework);
2727
// Forcing terminal logger so we can see the output when it is redirected
2828
InvokeDotnetTest($@"{projectPath} -tl:on -nodereuse:false /p:PackageVersion={IntegrationTestEnvironment.LatestLocallyBuiltNugetVersion}", workingDirectory: Path.GetDirectoryName(projectPath));
2929

@@ -53,7 +53,7 @@ public void MSBuildLoggerCanBeDisabledByBuildProperty(RunnerInfo runnerInfo)
5353
{
5454
SetTestEnvironment(_testEnvironment, runnerInfo);
5555

56-
var projectPath = GetIsolatedTestAsset("TerminalLoggerTestProject.csproj");
56+
var projectPath = GetIsolatedTestAsset("TerminalLoggerTestProject.csproj", runnerInfo.TargetFramework);
5757
InvokeDotnetTest($@"{projectPath} -nodereuse:false /p:VsTestUseMSBuildOutput=false /p:PackageVersion={IntegrationTestEnvironment.LatestLocallyBuiltNugetVersion}", workingDirectory: Path.GetDirectoryName(projectPath));
5858

5959
// Check that we see the summary that is printed from the console logger, meaning the new output is disabled.
@@ -73,7 +73,7 @@ public void MSBuildLoggerCanBeDisabledByEnvironmentVariableProperty(RunnerInfo r
7373
{
7474
SetTestEnvironment(_testEnvironment, runnerInfo);
7575

76-
var projectPath = GetIsolatedTestAsset("TerminalLoggerTestProject.csproj");
76+
var projectPath = GetIsolatedTestAsset("TerminalLoggerTestProject.csproj", runnerInfo.TargetFramework);
7777
InvokeDotnetTest($@"{projectPath} -nodereuse:false /p:PackageVersion={IntegrationTestEnvironment.LatestLocallyBuiltNugetVersion}", environmentVariables: new Dictionary<string, string?> { ["MSBUILDENSURESTDOUTFORTASKPROCESSES"] = "1" }, workingDirectory: Path.GetDirectoryName(projectPath));
7878

7979
// Check that we see the summary that is printed from the console logger, meaning the new output is disabled.

test/Microsoft.TestPlatform.Acceptance.IntegrationTests/DotnetTestTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void RunDotnetTestWithCsproj(RunnerInfo runnerInfo)
2626
{
2727
SetTestEnvironment(_testEnvironment, runnerInfo);
2828

29-
var projectPath = GetIsolatedTestAsset("SimpleTestProject.csproj");
29+
var projectPath = GetIsolatedTestAsset("SimpleTestProject.csproj", runnerInfo.TargetFramework);
3030
InvokeDotnetTest($@"{projectPath} -tl:off /p:VSTestNoLogo=false --logger:""Console;Verbosity=normal"" /p:PackageVersion={IntegrationTestEnvironment.LatestLocallyBuiltNugetVersion}", workingDirectory: Path.GetDirectoryName(projectPath));
3131

3232
// ensure our dev version is used
@@ -60,7 +60,7 @@ public void RunDotnetTestWithCsprojPassInlineSettings(RunnerInfo runnerInfo)
6060
{
6161
SetTestEnvironment(_testEnvironment, runnerInfo);
6262

63-
var projectPath = GetIsolatedTestAsset("ParametrizedTestProject.csproj");
63+
var projectPath = GetIsolatedTestAsset("ParametrizedTestProject.csproj", runnerInfo.TargetFramework);
6464
InvokeDotnetTest($@"{projectPath} --logger:""Console;Verbosity=normal"" -tl:off /p:VSTestNoLogo=false /p:PackageVersion={IntegrationTestEnvironment.LatestLocallyBuiltNugetVersion} -- TestRunParameters.Parameter(name =\""weburl\"", value=\""http://localhost//def\"")", workingDirectory: Path.GetDirectoryName(projectPath));
6565

6666
// ensure our dev version is used

test/Microsoft.TestPlatform.Acceptance.IntegrationTests/RunsettingsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ public void RunSettingsAreLoadedFromProject(RunnerInfo runnerInfo)
510510
SetTestEnvironment(_testEnvironment, runnerInfo);
511511

512512
var projectName = "ProjectFileRunSettingsTestProject.csproj";
513-
var projectPath = GetIsolatedTestAsset(projectName);
513+
var projectPath = GetIsolatedTestAsset(projectName, runnerInfo.TargetFramework);
514514
InvokeDotnetTest($@"{projectPath} /p:VSTestUseMSBuildOutput=false --logger:""Console;Verbosity=normal"" /p:PackageVersion={IntegrationTestEnvironment.LatestLocallyBuiltNugetVersion}", workingDirectory: Path.GetDirectoryName(projectPath));
515515
ValidateSummaryStatus(0, 1, 0);
516516

test/Microsoft.TestPlatform.TestUtilities/AcceptanceTestBase.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.IO;
66
using System.Linq;
7+
using System.Text.RegularExpressions;
78

89
namespace Microsoft.TestPlatform.TestUtilities;
910

@@ -137,7 +138,7 @@ public static string GetRunSettingsWithTargetFramework(string targetFramework)
137138
return runSettingsXml;
138139
}
139140

140-
protected string GetIsolatedTestAsset(string assetName)
141+
protected string GetIsolatedTestAsset(string assetName, string targetFramework)
141142
{
142143
var projectPath = GetProjectFullPath(assetName);
143144

@@ -147,6 +148,13 @@ protected string GetIsolatedTestAsset(string assetName)
147148

148149
if (file.Extension.Equals(".csproj", StringComparison.OrdinalIgnoreCase))
149150
{
151+
// Build just for the given tfm
152+
var projFile = Path.Combine(TempDirectory.Path, Path.GetFileName(file.FullName));
153+
var csprojContent = File.ReadAllText(projFile);
154+
csprojContent = Regex.Replace(csprojContent, "<TargetFramework>.*?</TargetFramework>", $"<TargetFramework>{targetFramework}</TargetFramework>");
155+
csprojContent = Regex.Replace(csprojContent, "<TargetFrameworks>.*?</TargetFrameworks>", $"<TargetFramework>{targetFramework}</TargetFramework>");
156+
File.WriteAllText(projFile, csprojContent);
157+
150158
string root = IntegrationTestEnvironment.RepoRootDirectory;
151159
var testAssetsRoot = Path.GetFullPath(Path.Combine(root, "test", "TestAssets"));
152160

0 commit comments

Comments
 (0)