[test-improver] test: add edge case tests for UseConditionBaseWithTestClassAnalyzer (MSTEST0041)#9103
Conversation
…MSTEST0041) Add three targeted unit tests that exercise untested code paths: - WhenNonTestClassHasMultipleConditionAttributes_SingleDiagnostic: verifies FirstOrDefault behavior - only one diagnostic fires when a non-test class has two ConditionBase attributes (e.g. [OSCondition] + [CICondition]). - WhenAbstractNonTestClassHasConditionAttribute_Diagnostic: confirms no abstract-class exemption exists; abstract classes without [TestClass] still trigger the diagnostic. - WhenTwoLevelDerivedConditionAttributeOnNonTestClass_Diagnostic: confirms Inherits() is recursive - a 2nd-level ConditionBase subclass attribute on a non-test class still fires the diagnostic. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR extends the MSTest analyzer unit tests for MSTEST0041 (UseConditionBaseWithTestClassAnalyzer) to cover previously untested analyzer behaviors, without modifying production analyzer logic.
Changes:
- Added a test ensuring that multiple
ConditionBaseAttribute-derived attributes on a non-test class produce a single diagnostic (matching the analyzer’sFirstOrDefaultbehavior). - Added a test confirming that an abstract non-test class with a condition attribute still triggers the diagnostic (no abstract exemption).
- Added a test confirming the condition-attribute detection works for a two-level derived condition attribute (recursive
Inherits()).
Show a summary per file
| File | Description |
|---|---|
test/UnitTests/MSTest.Analyzers.UnitTests/UseConditionBaseWithTestClassAnalyzerTests.cs |
Adds three focused test cases covering multi-attribute, abstract-class, and two-level-inheritance scenarios for MSTEST0041. |
Copilot's findings
- Files reviewed: 1/1 changed files
- Comments generated: 0
🧪 Test quality grade — PR #9103
This advisory comment was generated automatically. Grades are heuristic
|
Evangelink
left a comment
There was a problem hiding this comment.
Note
🤖 Automated review by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Expert Code Review workflow. To request a follow-up action, reply by tagging @copilot directly.
✅ 22/22 dimensions clean — no findings.
Context read:
src/Analyzers/MSTest.Analyzers/UseConditionBaseWithTestClassAnalyzer.cs— confirmsFirstOrDefaultbehavior, no abstract-class guard, and that theInherits()call traverses the full type hierarchy.src/TestFramework/TestFramework/Attributes/TestMethod/ConditionBaseAttribute.cs— confirmsGroupNameandIsConditionMetareabstract, soLevel2ConditionAttributecorrectly inheritsIsConditionMet => truefromLevel1ConditionAttributeand gets an implicit parameterless constructor that chains throughLevel1ConditionAttribute()→ConditionBaseAttribute(ConditionMode.Include).
Per-test notes:
WhenNonTestClassHasMultipleConditionAttributes_SingleDiagnostic:GetAttributes()returns source-order attributes in Roslyn, soOSCondition(first in source) is correctly expected as the argument. TheFirstOrDefaultcomment is accurate.WhenAbstractNonTestClassHasConditionAttribute_Diagnostic: The analyzer performs noIsAbstractcheck, so the positive assertion is correct. The existingWhenTestClassHasOSConditionAttribute_NoDiagnosticcovers the no-diagnostic path for the[TestClass]guard, making a separate abstract+TestClass complement unnecessary.WhenTwoLevelDerivedConditionAttributeOnNonTestClass_Diagnostic:Level2ConditionAttributeis non-abstract (concreteIsConditionMetinherited fromLevel1ConditionAttribute), has a valid implicit parameterless constructor, and is a valid attribute target for[Level2Condition].
🤖 Automated content by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Expert Code Review (on PR ready) workflow. · 409.2 AIC · ⌖ 12.4 AIC · ◷
Goal and Rationale
UseConditionBaseWithTestClassAnalyzer(MSTEST0041) flags classes that have aConditionBaseAttributesubclass (e.g.[OSCondition],[CICondition], custom conditions) but are not marked as[TestClass]. Three code paths were untested:Multiple condition attributes on a non-test class — the analyzer uses
FirstOrDefault, so only one diagnostic fires regardless of how manyConditionBaseattributes are present. The message uses the first attribute's name.Abstract class without
[TestClass]— unlike some other analyzers in this repo,UseConditionBaseWithTestClassAnalyzerhas no abstract-class exemption. An abstract class with a condition attribute but no[TestClass]should trigger the diagnostic.Two-level derived condition attribute — the
Inherits()check is recursive. An attribute that is a 2nd-level subclass ofConditionBaseAttributeshould still trigger the diagnostic on a non-test class.Approach
Added three new
[TestMethod]test cases toUseConditionBaseWithTestClassAnalyzerTests.cs:WhenNonTestClassHasMultipleConditionAttributes_SingleDiagnostic— verifiesFirstOrDefaultbehavior: one diagnostic, named after the first attribute.WhenAbstractNonTestClassHasConditionAttribute_Diagnostic— confirms no abstract exemption exists in this analyzer.WhenTwoLevelDerivedConditionAttributeOnNonTestClass_Diagnostic— confirmsInherits()is recursive for condition attribute detection.Test Status
✅ All 12 tests pass (
MSTest.Analyzers.UnitTests, net8.0, Debug — 9 original + 3 new).Reproducibility
Trade-offs
Add this agentic workflows to your repo
To install this agentic workflow, run