Add analyzer for Assert APIs with format parameters (MSTEST0053) #6294
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces a new analyzer to help users migrate away from Assert API overloads that use format parameters, which will be deprecated in MSTest v4. The analyzer detects usage of the
string format, params object[] parameters
pattern and provides automated code fixes.Problem
MSTest v4 will remove Assert API overloads that accept format strings with parameter arrays, such as:
Users need guidance and tooling to migrate to supported alternatives before upgrading.
Solution
The new MSTEST0053 analyzer detects calls to Assert, CollectionAssert, and StringAssert methods that use the deprecated format parameter pattern by identifying method signatures with:
string
with[StringSyntax("CompositeFormat")]
attributeparams object[]
Code Fixes
Two automated code fix options are provided:
1. String.Format (Always Available)
2. Interpolated String (For String Literals)
The interpolated string fix is only offered for string literals to ensure safety, while the string.Format fix is always available for any format string expression.
Implementation Details
AvoidAssertFormatParametersAnalyzer
detects the deprecated pattern across all Assert family classesAvoidAssertFormatParametersFixer
provides both migration options with proper parameter mappingThis analyzer helps users proactively prepare their codebases for MSTest v4 while providing flexible migration options based on their preferences and code complexity.
Fixes #6293.
Warning
Firewall rules blocked me from connecting to one or more addresses
I tried to connect to the following addresses, but was blocked by firewall rules:
37cvsblobprodcus359.vsblob.vsassets.io
dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj
(dns block)4zjvsblobprodcus390.vsblob.vsassets.io
dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj
(dns block)7devsblobprodcus323.vsblob.vsassets.io
dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj
(dns block)cflvsblobprodcus383.vsblob.vsassets.io
dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj
(dns block)l49vsblobprodcus358.vsblob.vsassets.io
dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj
(dns block)ljcvsblobprodcus317.vsblob.vsassets.io
dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj
(dns block)st8vsblobprodcus339.vsblob.vsassets.io
dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj
(dns block)ytvvsblobprodcus310.vsblob.vsassets.io
dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.