Skip to content

[test-improver] Fix MSTEST0063 to detect invalid constructors on derived TestClass attributes #9836

Closed

Description

@github-actions

Goal and Rationale

TestClassConstructorShouldBeValidAnalyzer (MSTEST0063) validates that [TestClass]-decorated classes have a public parameterless constructor or a public TestContext-parameter constructor.

However, the guard used an exact-match check (SymbolEqualityComparer.Default.Equals) for TestClassAttribute, so classes decorated with derived test-class attributes — including the built-in [STATestClass] and any custom class MyAttr : TestClassAttribute — were silently skipped. Because MSTest discovers such classes at runtime via Inherits() logic, a private or internal constructor causes a runtime instantiation failure with no analyzer warning.

Approach

Replace the exact-match guard in AnalyzeSymbol:

// Before
!namedTypeSymbol.GetAttributes().Any(attr =>
    SymbolEqualityComparer.Default.Equals(attr.AttributeClass, testClassAttributeSymbol))

// After
!namedTypeSymbol.IsTestClass(testClassAttributeSymbol)

IsTestClass() is a shared helper that calls Inherits(), matching the runtime discovery behavior.

Four new tests were added to TestClassConstructorShouldBeValidAnalyzerTests.cs:

Scenario Expected
[STATestClass] + private constructor Diagnostic
[STATestClass] + public parameterless constructor NoDiagnostic
Custom derived TestClass attr + internal constructor Diagnostic
Custom derived TestClass attr + public constructor NoDiagnostic

Coverage Impact

No coverage tooling available in CI sandbox; tests were run locally and all pass.

Trade-offs

This is a behavior change: previously, classes with derived [TestClass] attributes and invalid constructors produced no warning. After this fix they do. This is the correct behavior — the fix brings the analyzer in line with MSTest's runtime class-discovery logic.

Test Status

Build and tests passed (./build.sh --restore --test --projects .../MSTest.Analyzers.UnitTests.csproj):

Tests succeeded: MSTest.Analyzers.UnitTests.dll [net8.0|x64]
Build succeeded. 0 Warning(s) 0 Error(s)

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • southcentralus0.in.applicationinsights.azure.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "southcentralus0.in.applicationinsights.azure.com"

See Network Configuration for more information.

🤖 Automated content by GitHub Copilot. Generated by the Test Improver workflow. · 306.9 AIC · ⌖ 15.5 AIC · ⊞ 13K · [◷]( · ◷)

Add this agentic workflows to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/test-improver.md@main

Note

This was originally intended as a pull request, but GitHub Actions is not permitted to create or approve pull requests in this repository.
The changes have been pushed to branch test-assist/mstest0063-derived-testclass-ctor-check-ae2afab754ef0984.

Click here to create the pull request

To fix the permissions issue, go to Settings → Actions → General and enable Allow GitHub Actions to create and approve pull requests. See also: gh-aw FAQ

Show patch preview (152 of 152 lines)
From ee360cb966ba9068e183162bb2650faa9a736e29 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Fri, 10 Jul 2026 23:28:15 +0000
Subject: [PATCH] Fix MSTEST0063 to detect invalid constructors on derived
 TestClass attrs
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

TestClassConstructorShouldBeValidAnalyzer used an exact-match check
(SymbolEqualityComparer.Default.Equals) for TestClassAttribute, so
derived attributes like [STATestClass] or custom [MyTestClass] were
silently skipped. MSTest discovers such classes at runtime via
Inherits() logic, so a private/internal constructor causes a runtime
failure but no analyzer warning.

Switch the guard to IsTestClass() (which uses Inherits()) to match
the runtime discovery logic, and add four tests covering:
- [STATestClass] with a private constructor → Diagnostic
- [STATestClass] with a public parameterless constructor → NoDiagnostic
- Custom derived TestClass attr with an internal constructor → Diagnostic
- Custom derived TestClass attr with a public constructor → NoDiagnostic

Co-authored-by: Copilot <[email protected]>
---
 ...stClassConstructorShouldBeValidAnalyzer.cs |  2 +-
 ...ssConstructorShouldBeValidAnalyzerTests.cs | 95 +++++++++++++++++++
 2 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/src/Analyzers/MSTest.Analyzers/TestClassConstructorShouldBeValidAnalyzer.cs b/src/Analyzers/MSTest.Analyzers/TestClassConstructorShouldBeValidAnalyzer.cs
index e2e1ba2..70bf91d 100644
--- a/src/Analyzers/MSTest.Analyzers/TestClassConstructorShouldBeValidAnalyzer.cs
+++ b/src/Analyzers/MSTest.Analyzers/TestClassConstructorShouldBeValidAnalyzer.cs
@@ -60,7 +60,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
         if (namedTypeSymbol.TypeKind != TypeKind.Class
             || namedTypeSymbol.IsAbstract
             || namedTypeSymbol.IsStatic
-           
... (truncated)

Activity

  1. added
    type/test-gap
    type/automation
    on Jul 10, 2026
  2. added
    area/agentic-workflows
    on Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/agentic-workflowstype/automationtype/test-gap

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions