Skip to content

Migrate Microsoft.TemplateEngine.Utils.UnitTests to MSTest.Sdk on MTP#54759

Open
Evangelink wants to merge 1 commit into
dotnet:mainfrom
Evangelink:evangelink/mstest-mtp-templateengine-utils
Open

Migrate Microsoft.TemplateEngine.Utils.UnitTests to MSTest.Sdk on MTP#54759
Evangelink wants to merge 1 commit into
dotnet:mainfrom
Evangelink:evangelink/mstest-mtp-templateengine-utils

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Migrates the Microsoft.TemplateEngine.Utils.UnitTests project from xUnit v3 to MSTest.Sdk on Microsoft.Testing.Platform (MTP). Follows the same pattern as #54758.

Changes

  • Switch the project SDK to MSTest.Sdk (drops the explicit xunit.v3.extensibility.core and AwesomeAssertions PackageReferences — the latter is now provided by test/Directory.Build.targets for MSTest.Sdk projects).
  • Convert xUnit attributes/assertions to MSTest equivalents:
    • [Fact] / [Fact(DisplayName=...)][TestMethod]
    • [Theory] [InlineData][TestMethod] [DataRow]
    • [MemberData(nameof(X))][DynamicData(nameof(X))]
    • Assert.True / Assert.FalseAssert.IsTrue / Assert.IsFalse
    • Assert.Equal / Assert.NotEqualAssert.AreEqual / Assert.AreNotEqual
    • Assert.Null / Assert.NotNullAssert.IsNull / Assert.IsNotNull
    • Assert.Single(coll, pred)Assert.AreEqual(1, coll.Count(pred))
    • TestContext.Current.CancellationTokenTestContext.CancellationTokenSource.Token
    • DirectedGraphTests keeps its existing FluentAssertions usage; the global using FluentAssertions is provided by the repo-wide test/Directory.Build.targets.

IClassFixture replacement

MSTest has no IClassFixture<T> equivalent. DefaultTemplatePackageProviderTests and InstallRequestPathResolutionTests now hold a lazily-initialized static EnvironmentSettingsHelper per class (matching the per-class lifetime IClassFixture provides) and dispose it in [ClassCleanup]. Each test still calls CreateEnvironment from a [TestInitialize] hook to preserve per-test virtualization.

Bridging the xUnit IMessageSink constructor requirement

EnvironmentSettingsHelper lives in the shared Microsoft.TemplateEngine.TestHelper project (consumed by other in-tree xUnit projects and intentionally left untouched as part of this migration). Its constructor takes an Xunit.Sdk.IMessageSink.

  • A small NullMessageSink : Xunit.Sdk.IMessageSink is added so the helper can be constructed without a running xUnit host.
  • A minimal GlobalUsings.cs aliases Xunit.Sdk.IMessageSink / IMessageSinkMessage so the file compiles without dragging xUnit's full global using set (which would conflict with MSTest's global Assert). The xUnit types remain reachable transitively through the Microsoft.TemplateEngine.TestHelper project reference.

Verification

  • ./build.cmd -projects test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/Microsoft.TemplateEngine.Utils.UnitTests.csproj succeeds with 0 warnings / 0 errors on both net11.0 and net481.
  • The produced executable is an MTP host; Microsoft.TemplateEngine.Utils.UnitTests.dll --list-tests discovers all 120 tests (theory rows expanded).

Part of the larger MSTest migration effort.

cc @Evangelink

Switches the project SDK to MSTest.Sdk so it picks up MSTest 4.x +
Microsoft.Testing.Platform (MTP) defaults instead of xUnit v3 + VSTest.

- csproj: <Project Sdk="MSTest.Sdk">, IsTestProject=true, ImplicitUsings=enable.
  Drop the explicit xunit.v3.extensibility.core and AwesomeAssertions
  PackageReferences (the latter is now added by test/Directory.Build.targets
  for MSTest.Sdk projects).

- Convert xUnit attributes/assertions to MSTest equivalents:
  [Fact]/[Fact(DisplayName=...)] -> [TestMethod]
  [Theory] [InlineData] -> [TestMethod] [DataRow]
  [MemberData(nameof(X))] -> [DynamicData(nameof(X))]
  Assert.True/False -> Assert.IsTrue/IsFalse
  Assert.Equal/NotEqual -> Assert.AreEqual/AreNotEqual
  Assert.Null/NotNull -> Assert.IsNull/IsNotNull
  Assert.Single(coll, pred) -> Assert.AreEqual(1, coll.Count(pred))
  TestContext.Current.CancellationToken -> TestContext.CancellationTokenSource.Token
  DirectedGraphTests keeps its existing FluentAssertions (AwesomeAssertions)
  usage; the global "using FluentAssertions" is provided by the repo
  Directory.Build.targets.

- IClassFixture<EnvironmentSettingsHelper> replacement:
  MSTest has no IClassFixture equivalent. DefaultTemplatePackageProviderTests
  and InstallRequestPathResolutionTests now hold a lazily-initialized
  static EnvironmentSettingsHelper per class (matching the per-class
  lifetime xUnit's IClassFixture provided) and dispose it in
  [ClassCleanup]. Tests still call CreateEnvironment from a [TestInitialize]
  hook to preserve per-test virtualization.

- Add a small NullMessageSink : Xunit.Sdk.IMessageSink so
  EnvironmentSettingsHelper's constructor (which requires an xUnit
  IMessageSink) can be satisfied without a running xUnit host. The
  xUnit types are still reachable transitively through the
  Microsoft.TemplateEngine.TestHelper project reference, which is
  consumed by other in-tree xUnit projects and intentionally left
  untouched as part of this migration.

- Add a minimal GlobalUsings.cs that aliases Xunit.Sdk.IMessageSink /
  IMessageSinkMessage so NullMessageSink compiles without dragging in
  xUnit's full global using set (which would conflict with MSTest's
  global Assert).

Verification:
- build.cmd -projects ...Microsoft.TemplateEngine.Utils.UnitTests.csproj
  succeeds with 0 warnings / 0 errors on net11.0 and net481.
- The produced executable is an MTP host;
  Microsoft.TemplateEngine.Utils.UnitTests.dll --list-tests discovers
  all 120 tests (theory rows expanded).
Copilot AI review requested due to automatic review settings June 13, 2026 15:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Migrates Microsoft.TemplateEngine.Utils.UnitTests from xUnit v3 to MSTest.Sdk on Microsoft.Testing.Platform by updating the test project configuration and converting test attributes/assertions and shared test infrastructure usage to MSTest equivalents.

Changes:

  • Switch test project SDK to MSTest.Sdk and remove xUnit/AwesomeAssertions direct package references.
  • Convert xUnit [Fact]/[Theory]/[MemberData] and assertions to MSTest [TestMethod]/[DataRow]/[DynamicData] and Assert.*.
  • Add an xUnit IMessageSink bridge (NullMessageSink) + aliases (GlobalUsings.cs) to continue using EnvironmentSettingsHelper without a running xUnit host.
Show a summary per file
File Description
test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/Microsoft.TemplateEngine.Utils.UnitTests.csproj Switches project to MSTest.Sdk and adjusts basic test project properties.
test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/GlobalUsings.cs Adds global aliases for xUnit sink types to avoid pulling xUnit global usings.
test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/NullMessageSink.cs Adds a no-op xUnit IMessageSink implementation for EnvironmentSettingsHelper.
test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/DefaultTemplatePackageProviderTests.cs Converts fixture pattern and assertions; uses MSTest TestContext.
test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/InstallRequestPathResolutionTests.cs Converts fixture pattern and assertions to MSTest.
test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/DirectedGraphTests.cs Converts data-driven tests from xUnit theories to MSTest DynamicData.
test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/CombinedListTests.cs Converts xUnit theory to MSTest data-driven test via DataRow.
test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/VersionStringTests.cs Converts xUnit theories/assertions to MSTest equivalents.
test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/ListExtensionsTests.cs Converts xUnit fact/assertions to MSTest and replaces Assert.Single usage.
test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/EqualityExtensionsTests.cs Converts xUnit facts/assertions to MSTest equivalents.
test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/GlobTests.cs Converts xUnit facts/assertions to MSTest equivalents.
test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/InMemoryFileSystemTests.cs Converts xUnit fact/assertions to MSTest equivalents.
test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/WellKnownSearchFiltersTests.cs Converts xUnit theories/assertions to MSTest DataRow + Assert.AreEqual.

Copilot's findings

Comments suppressed due to low confidence (1)

test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/InstallRequestPathResolutionTests.cs:100

  • File.Create returns a FileStream that must be disposed. Leaving these streams open can leak handles and may prevent deleting the temp directory on Windows.
            var testRootDir = TestUtils.CreateTemporaryFolder();
            File.Create(Path.Combine(testRootDir, "1.nupkg"));
            File.Create(Path.Combine(testRootDir, "2.nupkg"));
            File.Create(Path.Combine(testRootDir, "3.txt"));
  • Files reviewed: 13/13 changed files
  • Comments generated: 5

Comment on lines +86 to 88
[TestMethod]
[DynamicData(nameof(DirectedGraphHasCycleData))]
public void HasCycleTests(Dictionary<int, HashSet<int>> dependencies, bool shouldHaveCycle, IReadOnlyList<int> expectedCycle, IReadOnlyList<int> unused)
Comment on lines +95 to 97
[TestMethod]
[DynamicData(nameof(DirectedGraphHasCycleData))]
public void EnumerateTopologicalSortTests(Dictionary<int, HashSet<int>> dependencies, bool shouldHaveCycle, IReadOnlyList<int> unused, IReadOnlyList<int> expectedOrder)
Comment on lines +211 to 213
[TestMethod]
[DynamicData(nameof(DirectedGraphSubgraphData))]
public void GetSubGraphDependandOnVerticesTests(Dictionary<int, HashSet<int>> dependencies, IReadOnlyList<int> vertices, bool includeSeedVertices, Dictionary<int, HashSet<int>> expectedResult)
Comment on lines 49 to 52

var provider = new DefaultTemplatePackageProvider(null!, _engineEnvironmentSettings, nupkgs, folders);
var sources = await provider.GetAllTemplatePackagesAsync(TestContext.Current.CancellationToken);
var sources = await provider.GetAllTemplatePackagesAsync(TestContext.CancellationTokenSource.Token);

Comment on lines 131 to 133
Directory.CreateDirectory(Path.Combine(testRootDir, "dir"));
File.Create(Path.Combine(testRootDir, "dir", "1.nupkg"));
File.Create(Path.Combine(testRootDir, "dir", "2.nupkg"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants