[test-improver] test: add edge case tests for DoNotStoreStaticTestContextAnalyzer#8809
Draft
Evangelink wants to merge 1 commit into
Draft
Conversation
Add four new tests covering: - Assigning TestContext to instance field/property (no diagnostic) - Assigning TestContext parameter to local variable (no diagnostic) - Assigning non-TestContext parameter to static field (no diagnostic) - Assigning TestContext in a helper (non-lifecycle) method (diagnostic) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds additional unit tests for DoNotStoreStaticTestContextAnalyzer (MSTEST0024) in MSTest.Analyzers.UnitTests to increase coverage of edge cases around the analyzer’s OperationKind.SimpleAssignment logic and its pattern-matching conditions (Target static member + Value TestContext parameter).
Changes:
- Adds new no-diagnostic tests intended to cover instance-member assignments, non-member targets, and non-TestContext assignments.
- Adds a new diagnostic test ensuring the analyzer also fires when the static assignment occurs in a helper method.
Show a summary per file
| File | Description |
|---|---|
test/UnitTests/MSTest.Analyzers.UnitTests/DoNotStoreStaticTestContextAnalyzerTests.cs |
Adds several new edge-case tests for MSTEST0024 analyzer behavior. |
Copilot's findings
- Files reviewed: 1/1 changed files
- Comments generated: 3
Comment on lines
+149
to
+160
| [ClassInitialize] | ||
| public static void ClassInit(TestContext tc) | ||
| { | ||
| } | ||
|
|
||
| [TestInitialize] | ||
| public void TestInit() | ||
| { | ||
| _testContext = TestContext; | ||
| TestContext = TestContext; | ||
| } | ||
| } |
Comment on lines
+179
to
+180
| TestContext local = tc; | ||
| local.WriteLine(""); |
Comment on lines
+200
to
+205
| [AssemblyInitialize] | ||
| public static void AssemblyInit(TestContext tc) | ||
| { | ||
| s_name = "value"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
🤖 Test Improver — automated AI assistant focused on improving tests for this repository.
Goal and Rationale
DoNotStoreStaticTestContextAnalyzer(MSTEST0024) had only 2 tests: one happy path (diagnostic) and one no-diagnostic case. The analyzer's logic has several distinct conditions (Instance: nullfor static members,IParameterReferenceOperationfor the value type) that each deserve coverage to prevent regressions.Approach
Added four new tests that each probe a specific condition the analyzer checks:
WhenAssigningToInstanceMember_NoDiagnosticInstance: nullguard)WhenAssigningToLocalVariable_NoDiagnosticIMemberReferenceOperation) are not flaggedWhenAssigningNonTestContextParameterToStaticField_NoDiagnosticWhenAssigningTestContextInHelperMethod_DiagnosticCoverage Impact
These tests cover the four logical branches in
AnalyzeOperation:Instance: nullcheck (both true and false paths)IParameterReferenceOperationcheck (both true and false paths)Trade-offs
Minimal maintenance burden — tests are straightforward analyzer verifications with no external dependencies.
Reproducibility
./build.sh -test --projects "$(pwd)/test/UnitTests/MSTest.Analyzers.UnitTests/MSTest.Analyzers.UnitTests.csproj"Test Status
✅ All tests passed (0 errors, 0 warnings)
Add this agentic workflows to your repo
To install this agentic workflow, run