Skip to content

Add analyzer for Assert APIs with format parameters (MSTEST0053) #6294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 4, 2025

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:

Assert.IsTrue(condition, "Value: {0}", value);
Assert.AreEqual(expected, actual, "Expected {0} but got {1}", expected, actual);
CollectionAssert.AreEqual(list1, list2, "Collections differ: {0}", details);

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:

  • Second-to-last parameter: string with [StringSyntax("CompositeFormat")] attribute
  • Last parameter: params object[]

Code Fixes

Two automated code fix options are provided:

1. String.Format (Always Available)

// Before
Assert.IsTrue(condition, "Value: {0}", value);

// After  
Assert.IsTrue(condition, string.Format("Value: {0}", value));

2. Interpolated String (For String Literals)

// Before
Assert.IsTrue(condition, "Value: {0}", value);

// After
Assert.IsTrue(condition, $"Value: {value}");

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

  • Analyzer: AvoidAssertFormatParametersAnalyzer detects the deprecated pattern across all Assert family classes
  • Code Fix: AvoidAssertFormatParametersFixer provides both migration options with proper parameter mapping
  • Diagnostic ID: MSTEST0053 with Warning severity
  • Test Coverage: Comprehensive unit tests covering all Assert types and edge cases

This 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
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj (dns block)
  • 4zjvsblobprodcus390.vsblob.vsassets.io
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj (dns block)
  • 7devsblobprodcus323.vsblob.vsassets.io
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj (dns block)
  • cflvsblobprodcus383.vsblob.vsassets.io
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj (dns block)
  • l49vsblobprodcus358.vsblob.vsassets.io
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj (dns block)
  • ljcvsblobprodcus317.vsblob.vsassets.io
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj (dns block)
  • st8vsblobprodcus339.vsblob.vsassets.io
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj (dns block)
  • ytvvsblobprodcus310.vsblob.vsassets.io
    • Triggering command: 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.

Copilot AI and others added 2 commits August 4, 2025 05:55
…tation plan

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
…053)

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Add analyzer for Assert APIs with format Add analyzer for Assert APIs with format parameters (MSTEST0053) Aug 4, 2025
@Copilot Copilot AI requested a review from Evangelink August 4, 2025 06:04
Copilot finished work on behalf of Evangelink August 4, 2025 06:04
@Evangelink Evangelink marked this pull request as ready for review August 4, 2025 06:48
@Evangelink Evangelink enabled auto-merge (squash) August 4, 2025 06:48
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.

Add analyzer for Assert APIs with format
2 participants