Repository navigation
Migrate NetAnalyzers tests from xUnit to MSTest - #55066
Conversation
Switch Test.Utilities and TestReferenceAssembly off xUnit: - Test.Utilities -> MSTest.Sdk (IsTestApplication=false), drop xunit.v3.assert/Xunit.Combinatorial - Verifier wrappers: remove using Xunit; TestContext.Current.CancellationToken -> CancellationToken.None - Add MSTest-friendly TheoryData shim replacing xUnit's TheoryData - CodeMetricsTestsBase: Assert.* -> MSTest equivalents (Assert.Fail/IsEmpty) - TestReferenceAssembly: add Xunit.Sdk.NamespaceCollisionMarker so CA1724 tests no longer need the real xunit assembly Co-authored-by: Copilot <[email protected]>
Mechanical xUnit -> MSTest conversion across ~380 test files: - [Fact]/[Theory] -> [TestMethod]; [InlineData] -> [DataRow]; [MemberData]/[ClassData] -> [DynamicData] - [Trait] -> [TestProperty]; [Fact/Theory(Skip)] -> [TestMethod]+[Ignore]; [WindowsOnlyFact] -> [TestMethod]+[OSCondition(Windows)] - Assert.* -> MSTest equivalents (Assert.ThrowsExactly etc.); TestContext.Current.CancellationToken -> CancellationToken.None - Add [TestClass] to every test class (incl. inherited-test derived classes); make static/internal test methods instance/public - Project -> MSTest.Sdk; drop Microsoft.NET.TestFramework ref; add Combinatorial.MSTest; alias WorkItemAttribute to Test.Utilities' Co-authored-by: Copilot <[email protected]>
…e string The migration's 'add using Test.Utilities;' helper was line-based and inserted the using after the last top-level using directive in the file. In DoNotCallToImmutableCollectionOnAnImmutableCollectionValueTests.Fixer.cs that last match was a 'using' line INSIDE the interpolated verbatim 'expected' fixed-code baseline string, corrupting the expected output and failing CA2009_Arity2_CSharpAsync for ImmutableDictionary/ImmutableSortedDictionary. Removed the stray line so expected matches the analyzer's actual fix output. Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
CI status: NetAnalyzers migration is green — remaining red is unrelated macOS-arm64 infra flakinessThe one real test failure this PR introduced ( Evidence (this PR only changes
Different unrelated suites failing each run, only on macOS-arm64, on different agents (c6ec5108 vs e3b6ccf5), all with This is repo-wide, not PR-specific: the latest Plan: no code changes for these — they are out of this PR's scope. Once the macOS-arm64 queue recovers we'll do one clean re-run for a fully green build; if the infra flakiness persists, we'll ask the dnceng owners to classify it as a known infra issue rather than chase unrelated suites. |
|
/ba-g #51530 |
Verdict for reviewers/mergers: migration validated — only red is macOS-arm64 infra flakinessTL;DR: The NetAnalyzers xUnit→MSTest migration is correct and green on all platforms. The one real regression it introduced is fixed. The remaining CI red is unrelated macOS-arm64 infrastructure flakiness that also affects (a) The one real failure — root cause & fixThe initial run failed (b) Before/after CI evidence (this PR only touches
|
| Build | NetAnalyzers result |
|---|---|
| 1487277 (pre-fix) | ❌ 4 NetAnalyzers work-item failures (the CA2009 bug) |
| 1487369 (post-fix) | ✅ 0 NetAnalyzers failures; all 4 TestBuild jobs succeeded — linux-x64 14086/0, windows-x64 14373/0, FullFramework 14490/0 |
| 1487539 (re-run) | ✅ 0 NetAnalyzers failures; incl. macOS-arm64 TestBuild 13870/0, linux-x64 14086/0, windows-x64 14373/0 |
(c) The remaining red is macOS-arm64 infra, not this PR
The only failures across the post-fix runs are unrelated suites on macOS-arm64, with exit code 2 and no published test results (host-crash signature), on different agents each run:
- 1487369:
dotnet.Tests,Microsoft.NET.Build.Tests,dotnet-new.IntegrationTests,Microsoft.NET.Sdk.StaticWebAssets.Tests,Microsoft.NET.Sdk.Web.Tests— agentc6ec5108 - 1487539:
Microsoft.NET.Sdk.BlazorWebAssembly.AoT.Tests— agente3b6ccf5
A PR touching only NetAnalyzers tests cannot affect Blazor AoT / dotnet.Tests / Build.Tests. Different unrelated suites, only on macOS-arm64, on different agents each run = a host-level macOS-arm64 problem in dnceng-public. Confirmation it's repo-wide and not this PR: the latest main CI build (1486966) is also red with the same macOS failures across many assemblies — including Microsoft.CodeAnalysis.NetAnalyzers.UnitTests.dll.11 on macOS, which on main is still the old xUnit suite (so macOS fails NetAnalyzers even without this change).
Recommendation
The migration is complete and green on its own merits. The remaining red is external macOS-arm64 infra flakiness (also red on main). Suggest merge-on-merits, or wait for the macOS-arm64 pool to recover / a known-issue classification — no code changes in this PR are warranted for those unrelated suites.
Summary
Migrates the
src/Microsoft.CodeAnalysis.NetAnalyzers/testssuite (~7,000[Fact]+ ~950[Theory]across ~400 files) off xUnit and onto MSTest v4 (MSTest.Sdk), so the tree contains zero functional xUnit test-framework references. This is one of several parallel xUnit-removal efforts; the change is strictly scoped to thetests/tree.Projects
MSTest.SdkwithIsTestApplication=false(CoreMSTest.TestFrameworkreference only). Droppedxunit.v3.assertandXunit.Combinatorial.MSTest.Sdk. Dropped the (unused, xUnit-coupled)Microsoft.NET.TestFrameworkproject reference; addedCombinatorial.MSTest.Xunit.Sdk.NamespaceCollisionMarkertype (see below).Mechanical conversions (string/comment-aware, so attributes inside analyzed test-source strings were left untouched)
[Fact]/[Theory]→[TestMethod];[InlineData]→[DataRow];[MemberData]/[ClassData]→[DynamicData];[Trait]→[TestProperty][Fact/Theory(Skip="…")]→[TestMethod]+[Ignore("…")]; per-row[InlineData(…, Skip=…)](4 rows) commented out (MSTest has no per-row skip)[WindowsOnlyFact](fromMicrosoft.DotNet.XUnitV3Extensions) →[TestMethod]+[OSCondition(OperatingSystems.Windows)]Assert.*→ MSTest equivalents (Assert.AreEqual,Assert.IsTrue,Assert.ThrowsExactly<T>,Assert.IsEmpty,Assert.Fail, …)[TestClass]to every test class (including concrete classes that only inherit test methods from abstract bases); madestatic/internaltest methods instance/publicusing Xunit;everywhere it was real codeTheoryData<…>with a small MSTest-friendly shim inTest.Utilitiesthat implementsIEnumerable<object[]>(consumable by[DynamicData])How the verifier / CancellationToken was handled
The shared verifier wrappers and ~230 test files called
test.RunAsync(Xunit.TestContext.Current.CancellationToken)(xUnit v3's ambient token). MSTest has no static ambient equivalent inside these static helpers, so every occurrence was replaced withCancellationToken.None. Allusing Xunit;lines were removed fromTest.Utilities; the framework-agnosticMicrosoft.CodeAnalysis.*.Testing(DefaultVerifier) references are unchanged.Two notable judgement calls
WorkItemAttributecollision — MSTest ships its ownWorkItemAttribute(int), which collides with the repo'sTest.Utilities.WorkItemAttribute(int, string). Rather than touch 124 files, a single global alias<Using Include="Test.Utilities.WorkItemAttribute" Alias="WorkItemAttribute" />makes the unqualified[WorkItem(id, "url")]usages bind to the repo's attribute.TypeNamesShouldNotMatchNamespacesTestsreferencedtypeof(Xunit.Sdk.AllException)purely to contribute anXunit.Sdknamespace to the analyzed compilation. Replaced with a newXunit.Sdk.NamespaceCollisionMarkerdefined inTestReferenceAssembly, so the real xunit package is no longer needed.Parallelization
Preserved the suite's existing serial execution: xUnit ran with
parallelizeTestCollections: false, andMSTest.Sdkhere emits[assembly: DoNotParallelize](via the repo'sMSTestParallelizeScope=None).Validation
Both projects build clean (0 warnings, 0 errors) with the repo SDK. Ran representative subsets under MTP — all green:
AvoidEmptyInterfaces(53),TypeNamesShouldNotMatchNamespaces(16, validates the CA1724 marker),DoNotCallEnumerableCast…(16,Assert.ThrowsExactly),PreferIsEmptyOverCount(119,ClassData→DynamicData),DoNotUseCountWhenAnyCanBeUsed(664,TheoryData/DynamicData),UseConcreteType(52, de-static classes),UseDefaultDllImportSearchPathsAttribute(56,OSCondition),AvoidDeadConditionalCode(235,Combinatorial.MSTest).grep -ri xunit src/Microsoft.CodeAnalysis.NetAnalyzers/testsshows only incidental references: xUnit-as-analyzed-code test data (AdditionalMetadataReferences.DefaultWithXUnit, which adds thexunitNuGet package as reference assemblies for the code-under-analysis), theXunit.Sdk.NamespaceCollisionMarker, and[Fact]/using Xunit;text embedded inside analyzed-source string literals — none of which are the test framework.Co-authored-by: Copilot [email protected]