Skip to content

[test-improver] test: add edge case tests for UseAttributeOnTestMethodAnalyzer (MSTEST0007) - #9516

Merged
Amaury Levé (Evangelink) merged 2 commits into
mainfrom
test-assist/use-attribute-on-test-method-edge-cases-434677eb5a320f85
Jun 30, 2026
Merged

Amaury Levé (Evangelink) merged 2 commits into
mainfrom
test-assist/use-attribute-on-test-method-edge-cases-434677eb5a320f85

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Goal and Rationale

UseAttributeOnTestMethodAnalyzer (MSTEST0007) had 37 test cases via 11 test methods (several using DynamicData), but two distinct code paths in the analyzer's logic were not covered:

Untested path Guard
Standard [DataTestMethod] causes early-return (no diagnostic) methodAttribute.AttributeClass.Inherits(testMethodAttributeSymbol) early-return — previously only tested with a custom MyCustomTestMethodAttribute
Non-[Ignore] ConditionBase subclass fires ConditionBaseRule with the concrete class name ReferenceEquals(attribute.Rule, ConditionBaseRule) → attribute.AttributeData.AttributeClass.Name — previously only [Ignore] was tested

Approach

Added two [TestMethod] entries to UseAttributeOnTestMethodAnalyzerTests.cs:

Test What it covers
WhenMethodIsMarkedWithDataTestMethodAndTestAttribute_NoDiagnosticAsync [DataTestMethod] + [Owner("owner")] → no diagnostic. Validates that the standard DataTestMethodAttribute (which inherits TestMethodAttribute) also causes early-return in the analyzer, not just user-defined subclasses.
WhenMethodIsMarkedWithOSConditionAttributeButNotWithTestMethod_DiagnosticAsync [OSCondition(OperatingSystems.Windows)] on a method with no [TestMethod] → ConditionBaseRule with argument "OSConditionAttribute". Validates the ConditionBaseRule branch for a non-[Ignore] subclass of ConditionBaseAttribute.

Coverage Impact

Before After
Tests in UseAttributeOnTestMethodAnalyzerTests 37 39

Trade-offs

Purely additive — no production code changes, no new dependencies.

Test Status

All 39/39 tests pass (net8.0, Debug):

Test run summary: Passed!
  total: 39
  failed: 0
  succeeded: 39
  duration: 7s 251ms

Reproducibility

.dotnet/dotnet test test/UnitTests/MSTest.Analyzers.UnitTests/MSTest.Analyzers.UnitTests.csproj \
  -f net8.0 --no-build -c Debug \
  --filter "FullyQualifiedName~UseAttributeOnTestMethodAnalyzerTests"

🤖 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 Test Improver workflow. · 2K AIC · ⌖ 25.1 AIC · ⊞ 58.2K · [◷]( · ◷)

Add this agentic workflows to your repo

To install this agentic workflow, run

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

…T0007)

Add 2 new tests covering untested code paths:

- WhenMethodIsMarkedWithDataTestMethodAndTestAttribute_NoDiagnosticAsync:
  Verifies the analyzer's early-return path fires for the standard
  [DataTestMethod] attribute (which inherits TestMethodAttribute), not
  just for custom TestMethod subclasses.

- WhenMethodIsMarkedWithOSConditionAttributeButNotWithTestMethod_DiagnosticAsync:
  Verifies that a non-[Ignore] ConditionBase subclass ([OSCondition])
  on a method without [TestMethod] fires ConditionBaseRule with the
  concrete class name 'OSConditionAttribute' as the message argument.
  Previously only [Ignore] was tested for this code path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 29, 2026 23:32
@Evangelink Amaury Levé (Evangelink) added type/automation Created or maintained by an agentic workflow. type/test-gap Missing or insufficient tests. labels Jun 29, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds targeted unit tests to improve coverage for UseAttributeOnTestMethodAnalyzer (MSTEST0007) in the MSTest analyzers test suite, specifically exercising previously untested branches around DataTestMethod inheritance and ConditionBaseAttribute-derived conditions.

Changes:

  • Add a no-diagnostic test validating the analyzer’s early-return behavior when [DataTestMethod] is present (as a TestMethodAttribute subclass).
  • Add a diagnostic + code-fix test validating the ConditionBaseRule path for [OSCondition(...)] when [TestMethod] is missing.
Show a summary per file
File Description
test/UnitTests/MSTest.Analyzers.UnitTests/UseAttributeOnTestMethodAnalyzerTests.cs Adds two new test methods to cover DataTestMethodAttribute early-return and non-Ignore ConditionBaseAttribute diagnostic behavior.

Review details

  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Low

@Evangelink
Amaury Levé (Evangelink) marked this pull request as ready for review June 30, 2026 05:13
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 30, 2026 05:13
@Evangelink Amaury Levé (Evangelink) added the state/needs-review Awaiting review from the team. label Jun 30, 2026
@Evangelink
Amaury Levé (Evangelink) enabled auto-merge (squash) June 30, 2026 05:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 1/1 changed files
  • Comments generated: 0 new
  • Review effort level: Low

@Evangelink

Copy link
Copy Markdown
Member Author

🧪 Test quality grade — PR #9516

ΔTestGradeBandNotes
new UseAttributeOnTestMethodAnalyzerTests.
WhenMethodIsMarkedWithOSConditionAttributeButNotWithTestMethod_
DiagnosticAsync
B 80–89 Body is slightly over ~30 lines (~36) due to the code-fix pattern needing both input and expected-output snippets; assertions thoroughly verify the diagnostic rule, location, arguments, and fix output.
new UseAttributeOnTestMethodAnalyzerTests.
WhenMethodIsMarkedWithDataTestMethodAndOwnerAttribute_
NoDiagnosticAsync
A 90–100 No issues found.

This advisory comment was generated automatically. Grades are heuristic
and informational — they do not block merging. Re-run with
/grade-tests.

🤖 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 Grade Tests on PR (on open / sync) workflow. · 217.9 AIC · ⌖ 13.2 AIC · ⊞ 45.7K · [◷]( · ◷)

@Evangelink Amaury Levé (Evangelink) left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Dimension coverage
# Dimension Verdict
1 Algorithmic Correctness ✅ LGTM
2 Threading & Concurrency N/A
3 Security & IPC Contract Safety N/A
4 Public API & Binary Compatibility N/A
5 Performance & Allocations N/A
6 Cross-TFM Compatibility N/A
7 Resource & IDisposable Management N/A
8 Defensive Coding at Boundaries N/A
9 Localization & Resources N/A
10 Test Isolation ✅ LGTM
11 Assertion Quality ✅ LGTM
12 Flakiness Patterns ✅ LGTM
13 Test Completeness & Coverage ✅ LGTM
14 Data-Driven Test Coverage ✅ LGTM
15 Code Structure & Simplification ✅ LGTM
16 Naming & Conventions ✅ LGTM
17 Documentation Accuracy ✅ LGTM
18 Analyzer & Code Fix Quality ✅ LGTM
19 IPC Wire Compatibility N/A
20 Build Infrastructure & Dependencies N/A
21 Scope & PR Discipline ✅ LGTM
22 PowerShell Scripting Hygiene N/A

Notes

Test 1 (WhenMethodIsMarkedWithDataTestMethodAndOwnerAttribute_NoDiagnosticAsync) — Correctly validates the early-return path in AnalyzeSymbol. DataTestMethodAttribute inherits TestMethodAttribute, so methodAttribute.AttributeClass.Inherits(testMethodAttributeSymbol) returns true and the method exits before collecting any diagnostics — regardless of attribute ordering in GetAttributes(). The [Owner("owner")] companion attribute is a suitable witness that a diagnostic would have been emitted had the early-return not triggered. Clean.

Test 2 (WhenMethodIsMarkedWithOSConditionAttributeButNotWithTestMethod_DiagnosticAsync) — Correctly exercises the ReferenceEquals(attribute.Rule, ConditionBaseRule) branch for a non-[Ignore] subclass. OSConditionAttribute : ConditionBaseAttribute is confirmed in source; OperatingSystems.Windows is a valid enum value in Microsoft.VisualStudio.TestTools.UnitTesting; attribute.AttributeData.AttributeClass.Name yields "OSConditionAttribute", matching .WithArguments("OSConditionAttribute"). The code fix expectation (inserting [TestMethod] below the condition attribute) is consistent with the fixer's existing behaviour verified by the [Ignore] analogue. Clean.


Pre-existing observation (not introduced by this PR)

ConditionBaseRule is absent from SupportedDiagnostics (line 119–125 of UseAttributeOnTestMethodAnalyzer.cs), while it is present in RuleTuples. This does not cause a runtime failure because all six rule descriptors share the same DiagnosticIds.UseAttributeOnTestMethodRuleId ID, and Roslyn filters by ID when deciding whether a reported diagnostic is supported. However, the missing entry means IDE tooling cannot surface ConditionBaseRule's distinct title/message-format in the analyzer catalog. The existing [Ignore] test — and now the new OSCondition test — both exercise this descriptor without issue, confirming the gap is benign in practice. Worth tracking separately if full descriptor fidelity in the manifest matters.

@Evangelink
Amaury Levé (Evangelink) merged commit 683d94c into main Jun 30, 2026
43 checks passed
@Evangelink
Amaury Levé (Evangelink) deleted the test-assist/use-attribute-on-test-method-edge-cases-434677eb5a320f85 branch June 30, 2026 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

state/needs-review Awaiting review from the team. type/automation Created or maintained by an agentic workflow. type/test-gap Missing or insufficient tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants