Add conditional attribute tests to ensure we're always running conditional tests...
authorSantiago Fernandez Madero <safern@microsoft.com>
Wed, 12 Sep 2018 01:54:33 +0000 (18:54 -0700)
committerGitHub <noreply@github.com>
Wed, 12 Sep 2018 01:54:33 +0000 (18:54 -0700)
* Add conditional attribute tests to ensure we're always running conditional tests

* Add licence headers

* Remove fixture and use static fields

* PR Feedback

Commit migrated from https://github.com/dotnet/corefx/commit/9c847e3b1bbc3a0e5b27a0ca3125937a83eff929

src/libraries/CoreFx.Private.TestUtilities/tests/ConditionalAttributeTests.cs [new file with mode: 0644]
src/libraries/CoreFx.Private.TestUtilities/tests/CoreFx.Private.TestUtilities.Tests.csproj

diff --git a/src/libraries/CoreFx.Private.TestUtilities/tests/ConditionalAttributeTests.cs b/src/libraries/CoreFx.Private.TestUtilities/tests/ConditionalAttributeTests.cs
new file mode 100644 (file)
index 0000000..5cee77a
--- /dev/null
@@ -0,0 +1,66 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Xunit;
+using Xunit.Abstractions;
+using Xunit.Sdk;
+
+namespace CoreFx.Private.TestUtilities.Tests
+{
+    [TestCaseOrderer("CoreFx.Private.TestUtilities.Tests.AlphabeticalOrderer", "CoreFx.Private.TestUtilities.Tests")]
+    public class ConditionalAttributeTests
+    {
+        // The tests under this class are to validate that ConditionalFact and ConditionalAttribute tests are being executed correctly
+        // In the past we have had cases where infrastructure changes broke this feature and tests where not running in certain framework.
+        // This test class is test order dependent so do not rename the tests.
+        // If new tests need to be added, follow the same naming pattern ConditionalAttribute{LetterToOrderTest} and then add a Validate{TestName}.
+
+        private static bool s_conditionalFactExecuted;
+        private static int s_conditionalTheoryCount;
+
+        public static bool AlwaysTrue => true;
+
+        [ConditionalFact(nameof(AlwaysTrue))]
+        public void ConditionalAttributeA()
+        {
+            s_conditionalFactExecuted = true;
+        }
+
+        [ConditionalTheory(nameof(AlwaysTrue))]
+        [InlineData(1)]
+        [InlineData(2)]
+        [InlineData(3)]
+        public void ConditionalAttributeB(int _)
+        {
+            s_conditionalTheoryCount++;
+        }
+
+        [Fact]
+        public void ValidateConditionalFact()
+        {
+            Assert.True(s_conditionalFactExecuted);
+        }
+
+        [Fact]
+        public void ValidateConditionalTheory()
+        {
+            Assert.Equal(3, s_conditionalTheoryCount);
+        }
+    }
+
+    // We need this TestCaseOrderer in order to guarantee that the ConditionalAttributeTests are always executed in the same order.
+    public class AlphabeticalOrderer : ITestCaseOrderer
+    {
+        public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases)
+                where TTestCase : ITestCase
+        {
+            List<TTestCase> result = testCases.ToList();
+            result.Sort((x, y) => StringComparer.Ordinal.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name));
+            return result;
+        }
+    }
+}
index f9c26f8..0f2ccec 100644 (file)
@@ -8,6 +8,7 @@
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="AssertExtensionTests.cs" />
+    <Compile Include="ConditionalAttributeTests.cs" />
     <Compile Include="TheoryExtensionTests.cs" />
   </ItemGroup>
 </Project>
\ No newline at end of file