From dc42ff2977304025fecea950364c36cd97e58871 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]"
 <41898282+github-actions[bot]@users.noreply.github.com>
Date: Fri, 24 Apr 2026 00:16:27 +0000
Subject: [PATCH] Add tests for UseConditionBaseWithTestClassAnalyzer
 (MSTEST0041)

Add 9 unit tests covering the UseConditionBaseWithTestClassAnalyzer
which warns when a ConditionBaseAttribute-derived attribute is applied
to a class without TestClassAttribute.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
 ...ConditionBaseWithTestClassAnalyzerTests.cs | 178 ++++++++++++++++++
 1 file changed, 178 insertions(+)
 create mode 100644 test/UnitTests/MSTest.Analyzers.UnitTests/UseConditionBaseWithTestClassAnalyzerTests.cs

diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/UseConditionBaseWithTestClassAnalyzerTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/UseConditionBaseWithTestClassAnalyzerTests.cs
new file mode 100644
index 0000000000..b9a8219256
--- /dev/null
+++ b/test/UnitTests/MSTest.Analyzers.UnitTests/UseConditionBaseWithTestClassAnalyzerTests.cs
@@ -0,0 +1,178 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+
+using VerifyCS = MSTest.Analyzers.Test.CSharpCodeFixVerifier<
+    MSTest.Analyzers.UseConditionBaseWithTestClassAnalyzer,
+    Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>;
+
+namespace MSTest.Analyzers.Test;
+
+[TestClass]
+public sealed class UseConditionBaseWithTestClassAnalyzerTests
+{
+    [TestMethod]
+    public async Task WhenTestClassHasOSConditionAttribute_NoDiagnostic()
+    {
+        string code = """
+            using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+            [TestClass]
+            [OSCondition(OperatingSystems.Windows)]
+            public class MyTestClass
+            {
+            }
+            """;
+
+        await VerifyCS.VerifyAnalyzerAsync(code);
+    }
+
+    [TestMethod]
+    public async Task WhenTestClassHasCIConditionAttribute_NoDiagnostic()
+    {
+        string code = """
+            using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+            [TestClass]
+            [CICondition(ConditionMode.Include)]
+            public class MyTestClass
+            {
+            }
+            """;
+
+        await VerifyCS.VerifyAnalyzerAsync(code);
+    }
+
+    [TestMethod]
+    public async Task WhenClassHasOnlyTestClassAttribute_NoDiagnostic()
+    {
+        string code = """
+            using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+            [TestClass]
+            public class MyTestClass
+            {
+            }
+            """;
+
+        await VerifyCS.VerifyAnalyzerAsync(code);
+    }
+
+    [TestMethod]
+    public async Task WhenPlainClassHasNoAttributes_NoDiagnostic()
+    {
+        string code = """
+            using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+            public class MyClass
+            {
+            }
+            """;
+
+        await VerifyCS.VerifyAnalyzerAsync(code);
+    }
+
+    [TestMethod]
+    public async Task WhenNonTestClassHasOSConditionAttribute_Diagnostic()
+    {
+        string code = """
+            using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+            [OSCondition(OperatingSystems.Windows)]
+            public class {|#0:MyClass|}
+            {
+            }
+            """;
+
+        await VerifyCS.VerifyAnalyzerAsync(
+            code,
+            VerifyCS.Diagnostic()
+                .WithLocation(0)
+                .WithArguments("OSConditionAttribute"));
+    }
+
+    [TestMethod]
+    public async Task WhenNonTestClassHasCIConditionAttribute_Diagnostic()
+    {
+        string code = """
+            using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+            [CICondition(ConditionMode.Include)]
+            public class {|#0:MyClass|}
+            {
+            }
+            """;
+
+        await VerifyCS.VerifyAnalyzerAsync(
+            code,
+            VerifyCS.Diagnostic()
+                .WithLocation(0)
+                .WithArguments("CIConditionAttribute"));
+    }
+
+    [TestMethod]
+    public async Task WhenInheritedTestClassAttributeHasOSConditionAttribute_NoDiagnostic()
+    {
+        string code = """
+            using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+            public class MyTestClassAttribute : TestClassAttribute { }
+
+            [MyTestClass]
+            [OSCondition(OperatingSystems.Windows)]
+            public class MyTestClass
+            {
+            }
+            """;
+
+        await VerifyCS.VerifyAnalyzerAsync(code);
+    }
+
+    [TestMethod]
+    public async Task WhenNonTestClassHasCustomConditionAttribute_Diagnostic()
+    {
+        string code = """
+            using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+            public class MyConditionAttribute : ConditionBaseAttribute
+            {
+                public MyConditionAttribute() : base(ConditionMode.Include) { }
+                public override string GroupName => nameof(MyConditionAttribute);
+                public override bool IsConditionMet => true;
+            }
+
+            [MyCondition]
+            public class {|#0:MyClass|}
+            {
+            }
+            """;
+
+        await VerifyCS.VerifyAnalyzerAsync(
+            code,
+            VerifyCS.Diagnostic()
+                .WithLocation(0)
+                .WithArguments("MyConditionAttribute"));
+    }
+
+    [TestMethod]
+    public async Task WhenTestClassHasCustomConditionAttribute_NoDiagnostic()
+    {
+        string code = """
+            using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+            public class MyConditionAttribute : ConditionBaseAttribute
+            {
+                public MyConditionAttribute() : base(ConditionMode.Include) { }
+                public override string GroupName => nameof(MyConditionAttribute);
+                public override bool IsConditionMet => true;
+            }
+
+            [TestClass]
+            [MyCondition]
+            public class MyTestClass
+            {
+            }
+            """;
+
+        await VerifyCS.VerifyAnalyzerAsync(code);
+    }
+}
