Add analyzer to flag non-serializable test case implementations (xUnit3006/xUnit3007)#207
Conversation
Introduce X3006/X3007 diagnostics to warn when test case classes implementing ITestCase are not serializable in xUnit v3. Sealed classes trigger X3006, while unsealed classes trigger X3007. Add unit tests for both diagnostics. Also, fix a typo in SerializabilityAnalyzer method name.
…s (for performance)
6aee36a to
0fa1fc6
Compare
|
Thanks! I made a few changes:
|
|
I don't know what the stack overflow is that cause the macOS build to fail. I don't see any xUnit.net Analyzer code in the stack. |
|
Thanks! |
|
Thanks for the refinements and for pushing the merge through, Brad! That StackOverflowException makes sense now,I looked at the trace and it’s hitting WeightedMatch.MatchRecursive in the Microsoft testing library. It looks like the combination of the consolidated tests and the 19 nested markers in the CS0535 helper created a diagnostic matching tree that was just too deep for the macOS runner's stack and its capacity is less in compare to Windows /Linux? Also, thanks for the clarification on [JsonTypeID] support for test cases and the update to the IXunitSerializer logic. Glad I could help. |
That's disappointing to hear. It sounds like I'm going to end up removing macOS from the CI test suite until it gets resolved. Did you find an open issue for this already? |
|
I took a look through dotnet/roslyn-sdk and dotnet/roslyn, and I couldn’t find an existing issue that tracks this specific StackOverflowException in WeightedMatch.MatchRecursive. From what I can tell, this seems to be triggered by the matching algorithm when there are many ambiguous diagnostics. In this case, the nested CS0535 markers generate a large number of similar diagnostics, which leads to very deep recursion and ends up hitting the stack limit on macOS. I opened an upstream issue (dotnet/roslyn-sdk#1253) to document this behavior so it can be tracked on the Roslyn side. In the meantime, I’ll put together a follow-up PR to simplify these test cases by reducing the number of compiler diagnostics generated. That should make the tests stable across platforms and avoid the need to remove macOS from CI. |
Add analyzer to flag non-serializable test case implementations (xUnit3006/xUnit3007)
Closes xunit/xunit#3535
Summary
Adds a new diagnostic analyzer
TestCaseMustBeSerializablethat flagsITestCaseimplementations which are not serializable. Test cases must be serializable to support test discovery and execution.Also fixes a pre-existing typo:
AnalayzeSerializability→AnalyzeSerializability.New Diagnostics
ITestCasebut does not implementIXunitSerializableor[JsonTypeID]ITestCasebut does not implementIXunitSerializableor[JsonTypeID]The distinction between X3006 and X3007 exists because unsealed classes might be handled by an external
IXunitSerializerregistration or a serializable subclass, which cannot be determined at compile time.Behavior
ITestCaseinherits fromIXunitSerializable, so all implementors are inherently serializable. The analyzer correctly does not trigger for v2 projects.ITestCasedoes not inherit fromIXunitSerializable. The analyzer triggers when a concrete class implementsITestCasewithout:IXunitSerializable, or[JsonTypeID]