-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Enforce single unique category per test in xUnit device tests with compile-time analyzers #32366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
@copilot will this detect the case where there is a category on the class AND then method? |
No, the current implementation only checks for multiple So these scenarios work as expected:
The validation specifically targets the problematic pattern of multiple |
|
@copilot Please make sure there is only a single category, do not allow on the class and method, do not allow multiple on the method, do not allow multiple in the the single category attreibute. This issue is not about 1 attribute but 1 category. We need the system to ONLY ALLOW SINGLE categories. No matter what. |
Updated in commit c9865bd. The validation now enforces a single category total per test:
The error message identifies which pattern was violated (multiple attributes, multiple values, or class+method) and directs developers to use |
|
/azp run MAUI-DeviceTests-public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds validation to ensure tests have exactly one category attribute. The validation prevents tests from being silently skipped due to multiple categories being applied (whether through multiple attributes, multiple values in one attribute, or categories on both class and method).
Key changes:
- Added
XUnitValidationHelpersstatic class withValidateSingleCategoryAttributemethod - Integrated validation into both
FactDiscovererandTheoryDiscovererduring test discovery - Throws
InvalidOperationExceptionwith detailed error messages when multiple categories are detected
|
@copilot this is working, but I don't know what the categories are to remove: Can you also have a look and add a feature to ignore duplicate categories. So if there is some way the user has the same category multiple times, that is fine. |
Fixed in commit 62c0edf. The validation now:
The failing test |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
@copilot Have a look at the |
The validation isn't detecting it because The test has:
Solutions:
Which approach would you prefer? |
|
@copilot it seems to be working. Can you now build the solution and fix the errors. Please list all the errors in a comment so we can track it. |
mattleibow
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot some tweaks.
src/TestUtils/src/DeviceTests.Analyzers/XUnitTestMultipleCategoriesAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/TestUtils/src/DeviceTests.Analyzers/XUnitTestMissingCategoryAnalyzer.cs
Outdated
Show resolved
Hide resolved
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
@copilot please do the following.
|
|
Azure Pipelines successfully started running 1 pipeline(s). |
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
…n files Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
# Conflicts: # src/Controls/tests/DeviceTests/Elements/ContentView/ContentViewTests.iOS.cs
8f14d16 to
12e9d04
Compare
|
Azure Pipelines successfully started running 1 pipeline(s). |
Prevents silent test skipping in xUnit device tests by enforcing exactly one unique category per test through compile-time Roslyn analyzers.
Problem
Multiple categories on xUnit device tests cause silent test skipping through:
[Category]attributes on a method:[Category("A")] [Category("B")][Category("A", "B")]Solution
Created new
DeviceTests.Analyzersproject with two Roslyn analyzers:MAUIT1001 (Error): Detects multiple unique categories
Test 'MyTest' has 2 unique categories: Button, LayoutMAUIT1002 (Error): Detects missing categories
Implementation Details
DeviceTests.Analyzers Project
[Category("PlaceholderCategory")]to method or classSolution File Integration
Added DeviceTests.Analyzers to all 6 solution files with complete integration:
Each solution file includes:
Validation Rules
✅ Valid - Single unique category:
✅ Valid - Duplicate categories allowed:
❌ Invalid - Multiple unique categories in one attribute:
❌ Invalid - Multiple unique categories via multiple attributes:
❌ Invalid - Different categories on class and method:
❌ Invalid - Missing category:
Coverage
The analyzers work with ALL xUnit tests:
[Fact]and[Theory]attributesMicrosoft.Maui.FactAttributeandMicrosoft.Maui.TheoryAttributeThis ensures comprehensive coverage across the entire codebase, preventing invalid code from compiling.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.