diff --git a/TUnit.Analyzers.CodeFixers/TwoPhase/MSTestTwoPhaseAnalyzer.cs b/TUnit.Analyzers.CodeFixers/TwoPhase/MSTestTwoPhaseAnalyzer.cs index f71a922370..322619d6e0 100644 --- a/TUnit.Analyzers.CodeFixers/TwoPhase/MSTestTwoPhaseAnalyzer.cs +++ b/TUnit.Analyzers.CodeFixers/TwoPhase/MSTestTwoPhaseAnalyzer.cs @@ -122,7 +122,13 @@ private bool VerifyMSTestNamespace(InvocationExpressionSyntax invocation) if (symbolInfo.Symbol is IMethodSymbol methodSymbol) { var containingNamespace = methodSymbol.ContainingType?.ContainingNamespace?.ToDisplayString(); - return containingNamespace?.StartsWith("Microsoft.VisualStudio.TestTools.UnitTesting") == true; + // Only return false if we positively know it's NOT MSTest + if (containingNamespace != null && !containingNamespace.StartsWith("Microsoft.VisualStudio.TestTools.UnitTesting")) + { + return false; + } + // Namespace matches or is null (resolution failed) - assume it's MSTest + return true; } } catch diff --git a/TUnit.Analyzers.CodeFixers/TwoPhase/NUnitTwoPhaseAnalyzer.cs b/TUnit.Analyzers.CodeFixers/TwoPhase/NUnitTwoPhaseAnalyzer.cs index 1f6a8d0ee9..9374bc6b89 100644 --- a/TUnit.Analyzers.CodeFixers/TwoPhase/NUnitTwoPhaseAnalyzer.cs +++ b/TUnit.Analyzers.CodeFixers/TwoPhase/NUnitTwoPhaseAnalyzer.cs @@ -109,7 +109,13 @@ private bool VerifyNUnitNamespace(InvocationExpressionSyntax invocation) if (symbolInfo.Symbol is IMethodSymbol methodSymbol) { var containingNamespace = methodSymbol.ContainingType?.ContainingNamespace?.ToDisplayString(); - return containingNamespace?.StartsWith("NUnit.Framework") == true; + // Only return false if we positively know it's NOT NUnit + if (containingNamespace != null && !containingNamespace.StartsWith("NUnit.Framework")) + { + return false; + } + // Namespace matches or is null (resolution failed) - assume it's NUnit + return true; } } catch diff --git a/TUnit.Analyzers.CodeFixers/TwoPhase/XUnitTwoPhaseAnalyzer.cs b/TUnit.Analyzers.CodeFixers/TwoPhase/XUnitTwoPhaseAnalyzer.cs index d0294f1db9..47f149e8b6 100644 --- a/TUnit.Analyzers.CodeFixers/TwoPhase/XUnitTwoPhaseAnalyzer.cs +++ b/TUnit.Analyzers.CodeFixers/TwoPhase/XUnitTwoPhaseAnalyzer.cs @@ -103,12 +103,28 @@ private bool IsXUnitAssertion(InvocationExpressionSyntax invocation) if (XUnitAssertMethods.Contains(methodName)) { // Semantic check to confirm it's xUnit - var symbolInfo = SemanticModel.GetSymbolInfo(invocation); - if (symbolInfo.Symbol is IMethodSymbol methodSymbol) + try { - var containingType = methodSymbol.ContainingType?.ToDisplayString(); - return containingType?.StartsWith("Xunit.Assert") == true; + var symbolInfo = SemanticModel.GetSymbolInfo(invocation); + if (symbolInfo.Symbol is IMethodSymbol methodSymbol) + { + var containingType = methodSymbol.ContainingType?.ToDisplayString(); + // Only return false if we positively know it's NOT xUnit + if (containingType != null && !containingType.StartsWith("Xunit.Assert")) + { + return false; + } + // Namespace matches or is null (resolution failed) - assume it's xUnit + return true; + } } + catch + { + // Fall back to syntax-based detection + } + + // Semantic check failed or symbol not found - assume it's xUnit if syntax matches + return true; } } }