Repository navigation
Use query providers for MSTest count and emptiness assertions - #12603
Amaury Levé (Evangelink) merged 1 commit into
Conversation
Add IQueryable<T> overloads for HasCount, IsEmpty, and IsNotEmpty and align MSTEST0090 with the overload the compiler selects. Keep explicitly enumerable calls on the existing path. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🧪 Test quality review — PR #12603
Compared merge base 8c841a21bf80dd5b19330547ed875f47e853d2bc with head 797c7e53a10927dd48970d8aeff5b8f4de9d71c7. Evidence applies to this head, not every intermediate commit. No inline findings were verified.
Test-value evidence
All outcomes below are static predictions unless noted. This workflow did not run tests.
| Contract | Test and decisive assertion | Counterfactual | Result |
|---|---|---|---|
HasCount(IQueryable) calls Queryable.Count and does not enumerate |
Count_IQueryable_UsesQueryableCountWithoutEnumerating asserts ScalarOperations == [Count] and EnumerationCount == 0 |
Switch to Enumerable.Count: the scalar-operation list is empty and enumeration is 1 |
Predicted detected. The PR body reports one executed red-green run for this counterfactual (exit 2 without the change, exit 0 with it). That run is the author's claim and is not independently verified. |
HasCount failure message reports the actual count and provider exceptions propagate |
Count_IQueryable_WhenCountDiffers_* asserts the full message; ..._WhenProviderCountThrows_* asserts the exception type and message |
Fall back to enumeration or swallow the exception: the operation and enumeration assertions fail | Predicted detected |
IsEmpty(IQueryable) uses Count and reports the actual count |
IsEmpty_IQueryable_WhenNotEmpty_* asserts the full message with actual count: 2 and [Count] |
Use Any, or enumerate: the operation or message assertion fails |
Predicted detected |
IsNotEmpty(IQueryable) uses Any and reports an empty query |
IsNotEmpty_IQueryable_* assert [Any] and the full failure message |
Use Count or enumeration: the operation assertion fails |
Predicted detected |
| Interpolated handlers are lazy on success and formatted on failure | The three *_InterpolatedStringHandler_IsLazyOnSuccessAndFormatsFailure tests assert WasToStringCalled |
Always append, or never append: the flag assertions fail | Predicted detected |
MSTEST0090 stays silent for native queryable overloads and still fires for AsEnumerable(), IEnumerable casts and ContainsSingle |
QueryableCountAndExistenceAssertions_Diagnostic and AvoidEnumerableAssertionsOnQueryableAnalyzerTests mark [|...|] spans. Inheritance_ConstrainedQueryable_UsesNativeOverloadsWithoutDiagnostic asserts no diagnostics. |
Remove the new early return: the non-diagnostic cases report spurious MSTEST0090 | Predicted detected |
| The new packaged overloads compile for C# 12 consumers and the analyzer behaves the same way when packed | QueryableEnumerableAndArrayCalls in AssertSourceCompatibilityTests; PackagedQueryableAssertionsUseNativeOverloads counts four MSTEST0090 warnings by assertion name |
Remove an overload or the analyzer change: compilation fails or the warning count changes | Unverified. Both need packed NuGets, so no executed record exists in what I can access. |
Docs and PublicAPI.Unshipped.txt |
Not executable | n/a | Not applicable |
Evidence gaps:
- There is no accessible paired execution record for the
IsEmpty,IsNotEmptyand analyzer mutations. - The explicit-generic null/default source break,
Assert.IsEmpty<int>(default!)→CS0121, is documented and the compatibility test is adjusted. The adjustment shows the break is intended rather than regressed, but the break is a behavior change for consumers.
🧵 Parallel safety
Status: CLEAN. No findings. Counts: Critical=0, High=0, Warning=0, Info=0.
| Test assembly | Scope | Notes |
|---|---|---|
| MSTest.Acceptance.IntegrationTests | AnalyzersTests (changed) |
The new test uses a uniquely named generated asset with its binlog inside it, and only reads the packed NuGets. |
| MSTest.Analyzers.UnitTests | Changed tests | No shared state found. |
| TestFramework.UnitTests | AssertTests.Items.cs |
The fixtures are per-test local RecordingQueryable instances. |
Per-test assessment
Pseudo-mutation evidence below consists of static predictions, not executed mutation results.
| Result | Quality | Test | Notes | How to improve |
|---|---|---|---|---|
| Pass | A (90–100) | new AssertTests.Count_IQueryable_UsesQueryableCountWithoutEnumerating |
It asserts the provider operation and zero enumeration. | None |
| Pass | A (90–100) | new AssertTests.Count_IQueryable_WhenCountDiffers_ReportsActualCountWithoutEnumerating |
It asserts the exact failure message and the provider operation. | None |
| Pass | A (90–100) | new AssertTests.Count_IQueryable_WhenProviderCountThrows_PropagatesWithoutEnumerating |
It asserts the exception type and message and that no enumeration occurred. | None |
| Pass | B (80–89) | new AssertTests.Count_IQueryable_InterpolatedStringHandler_IsLazyOnSuccessAndFormatsFailure |
It covers laziness and failure formatting, but the failure message is checked with wildcards. | None |
| Pass | A (90–100) | new AssertTests.IsEmpty_IQueryable_UsesQueryableCountWithoutEnumerating |
It asserts Count and no enumeration. |
None |
| Pass | A (90–100) | new AssertTests.IsEmpty_IQueryable_WhenNotEmpty_ReportsActualCountWithoutEnumerating |
It asserts the exact message and a single Count call. |
None |
| Pass | B (80–89) | new AssertTests.IsEmpty_IQueryable_InterpolatedStringHandler_IsLazyOnSuccessAndFormatsFailure |
It covers laziness, but the failure message is checked with wildcards. | None |
| Pass | A (90–100) | new AssertTests.IsNotEmpty_IQueryable_UsesQueryableAnyWithoutEnumerating |
It asserts Any and no enumeration. |
None |
| Pass | A (90–100) | new AssertTests.IsNotEmpty_IQueryable_WhenEmpty_ReportsZeroCountWithoutEnumerating |
It asserts the exact message and a single Any call. |
None |
| Pass | B (80–89) | new AssertTests.IsNotEmpty_IQueryable_InterpolatedStringHandler_IsLazyOnSuccessAndFormatsFailure |
It covers laziness, but the failure message is checked with wildcards. | None |
| Pass | B (80–89) | new AnalyzersTests.PackagedQueryableAssertionsUseNativeOverloads |
It counts four warnings by assertion name; the long body is acceptable for an integration test. | None |
| Pass | A (90–100) | mod AvoidEnumerableAssertionsOnQueryableAnalyzerTests.QueryableCountAndExistenceAssertions_Diagnostic |
The expectDiagnostic flag selects an exact diagnostic or a clean result. |
None |
| Pass | B (80–89) | mod AvoidEnumerableAssertionsOnQueryableAnalyzerTests (other changed methods) |
The marker spans separate native overloads from enumerable-forcing forms. | None |
| Pass | A (90–100) | mod AnalyzerDiagnosticContinuationTests.Inheritance_ConstrainedQueryable_UsesNativeOverloadsWithoutDiagnostic |
It asserts empty diagnostics for a constrained IQueryable<int>. |
None |
| Uncertain | — | mod AssertSourceCompatibilityTests (the ArrayAndEnumerableCalls/QueryableEnumerableAndArrayCalls source in the raw string) |
This is compile-time test source, not a test method. Its effect depends on packed-package compilation, which I did not run. | Review the executed PublicAssertCallShapes_CompileWithCSharp12 results for the packed package. |
<sub>This advisory review is heuristic and non-blocking.
Re-run the combined review with /review-tests, or only the parallel-safety specialist with /parallel-audit.</sub>
🤖 Automated content by GitHub Copilot. Generated by the Test Quality Review on PR (on open / sync) workflow. · copilot · auto (Claude Sonnet 5.5) · ~62 AIC · ⌖ ~2 AIC · ⊞ 8.4K
There was a problem hiding this comment.
🔵 Needs a closer look
Maintainer acceptance is needed for the documented source-compatibility break: existing explicit-generic null/default calls become ambiguous on modern assets, and the compatibility fixture now casts those arguments rather than preserving the original call shapes.
No open issues 🎉
What changed in this PR
Adds provider-aware MSTest scalar collection assertions for #12244, allowing IQueryable<T> inputs to use query-provider operations rather than enumerate results.
Changes:
- Adds
IQueryable<T>overloads usingCountforHasCount/IsEmptyandAnyforIsNotEmpty, preserving lazy interpolated messages. - Updates MSTEST0090 to recognize native overloads while retaining guidance for enumerable-bound calls and
ContainsSingle. - Adds provider-recording tests, packaged-consumer coverage, API declarations, and design documentation.
The PR documents a source-compatibility break for explicit-generic null/default literals, which now require typed arguments to avoid overload ambiguity.
Confidence at a glance
🟡 MSTest framework and analyzer — execution evidence is author-reported
Test-value evidence: For revision 797c7e53a10927dd48970d8aeff5b8f4de9d71c7, the PR reports a failing/passing HasCount mutation check and three passing packaged-consumer cases. Raw execution artifacts were not independently inspected; separate mutation checks for the emptiness assertions and analyzer were not reported.
| File | Description |
|---|---|
test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Items.cs |
Tests provider execution, failures, and lazy messages. |
test/UnitTests/MSTest.Analyzers.UnitTests/AvoidEnumerableAssertionsOnQueryableAnalyzerTests.cs |
Updates native and enumerable-bound diagnostic expectations. |
test/UnitTests/MSTest.Analyzers.UnitTests/AnalyzerDiagnosticContinuationTests.cs |
Expects no diagnostics for constrained queryable calls. |
test/IntegrationTests/MSTest.Acceptance.IntegrationTests/AssertSourceCompatibilityTests.cs |
Adds C# 12 consumer calls and typed null/default arguments. |
test/IntegrationTests/MSTest.Acceptance.IntegrationTests/AnalyzersTests.cs |
Exercises packaged assertions and analyzer diagnostics together. |
src/MSTest/TestFramework/PublicAPI/PublicAPI.Unshipped.txt |
Declares new assertion overloads and handler constructors. |
src/MSTest/TestFramework/Assertions/Assert.IsNotEmpty.cs |
Adds query-provider Any support. |
src/MSTest/TestFramework/Assertions/Assert.IsEmpty.cs |
Adds queryable emptiness overloads using count diagnostics. |
src/MSTest/TestFramework/Assertions/Assert.HasCount.cs |
Adds query-provider Count support. |
src/MSTest/TestFramework/Assertions/Assert.Count.InterpolatedStringHandlers.cs |
Captures provider counts for lazy failure messages. |
src/MSTest/MSTest.Analyzers/AvoidEnumerableAssertionsOnQueryableAnalyzer.cs |
Suppresses MSTEST0090 for native queryable overloads. |
docs/decisions/queryable-assertion-diagnostics.md |
Records design choices, compatibility impact, and validation. |
Review effort: Balanced
Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
797c7e53a10927dd48970d8aeff5b8f4de9d71c7↑ The packaged framework and analyzer passed all three consumer cases.
↓ Explicit-generic null/default literals can stop compiling. Ten unrelated analyzer tests failed locally because generator outputs were missing.
Pull request
Summary
Assert.HasCount,Assert.IsEmpty, andAssert.IsNotEmptycan now ask anIQueryable<T>provider for a scalar result instead of enumerating the matching items.HasCountandIsEmptyrequestCount; this letsIsEmptyreport the actual count on failure without issuing a second query.IsNotEmptyrequestsAny. Interpolated failure messages stay lazy. Provider exceptions propagate rather than triggering a fallback to enumeration.Closes #12244
Design and decision record
Decision record: native scalar assertion support. Explicit overloads preserve the caller's choice:
AsEnumerable()returns the same object, so a runtime type check inside the enumerable overload would ignore that choice. MSTEST0090 follows the overload the compiler selects; its guidance forContainsSingleis unchanged.Source compatibility: On recompilation, explicit-generic calls such as
Assert.IsEmpty<int>(default!)are ambiguous between the array and queryable overloads (CS0121). The same applies toHasCountandIsNotEmptywith an untyped null/default literal. A typed argument, e.g.(IEnumerable<int>)default!, selects the existing enumerable path. No existing signature was removed.Demonstration
These two calls from the packaged consumer illustrate the difference. There is no UI or provider-specific SQL to show.
Validation and regression proof
Local Windows results with the pinned SDK
12.0.100-alpha.1.26570.125and the source committed at the assessed revision:net48(1,736),net10.0(1,892),net11.0(1,892), andnet10.0-windows(1,899).net10.0MSTest.GlobalConfigsGeneratoroutputs, not queryable diagnostics.PackagedQueryableAssertionsUseNativeOverloadsbuilt and ran with the expected warnings;PublicAssertCallShapes_CompileWithCSharp12passed for bothnet10.0andnet11.0. Three selected, three executed, three passed, zero skipped; outer exit 0. No particular database provider or translation was tested.Release pack and acceptance commands (each exited 0)
The validation record identifies the two package hashes, validation artifacts, and the known source break; raw TRX and binary logs are local, not CI results.
Test-value evidence
Base
8c841a21bf80dd5b19330547ed875f47e853d2bc; head is the assessed revision above. The comparison retained the new API and the same test. Changing justQueryable.Count(collection)toEnumerable.Count(collection)produced one failed test (exit 2): the recording provider had received noCountoperation. The correct implementation produced one passed test (exit 0). This mutation proves theHasCounttest distinguishes the execution paths; a separate mutation was not run forIsEmpty,IsNotEmpty, or the analyzer.