Skip to content

Commit ae9d51f

Browse files
Fix ConditionalFact and ConditionalTheory (#2241)
1 parent 033ea04 commit ae9d51f

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

src/TestingUtils/Microsoft.AspNetCore.Testing/src/xunit/ConditionalFactAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Microsoft.AspNetCore.Testing
99
{
1010
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
11-
[XunitTestCaseDiscoverer("Microsoft.AspNetCore.Testing.xunit." + nameof(ConditionalFactDiscoverer), "Microsoft.AspNetCore.Testing")]
11+
[XunitTestCaseDiscoverer("Microsoft.AspNetCore.Testing." + nameof(ConditionalFactDiscoverer), "Microsoft.AspNetCore.Testing")]
1212
public class ConditionalFactAttribute : FactAttribute
1313
{
1414
}

src/TestingUtils/Microsoft.AspNetCore.Testing/src/xunit/ConditionalTheoryAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Microsoft.AspNetCore.Testing
99
{
1010
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
11-
[XunitTestCaseDiscoverer("Microsoft.AspNetCore.Testing.xunit." + nameof(ConditionalTheoryDiscoverer), "Microsoft.AspNetCore.Testing")]
11+
[XunitTestCaseDiscoverer("Microsoft.AspNetCore.Testing." + nameof(ConditionalTheoryDiscoverer), "Microsoft.AspNetCore.Testing")]
1212
public class ConditionalTheoryAttribute : TheoryAttribute
1313
{
1414
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using Xunit.Abstractions;
8+
using Xunit.Sdk;
9+
10+
namespace Microsoft.AspNetCore.Testing
11+
{
12+
public class AlphabeticalOrderer : ITestCaseOrderer
13+
{
14+
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases)
15+
where TTestCase : ITestCase
16+
{
17+
var result = testCases.ToList();
18+
result.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name));
19+
return result;
20+
}
21+
}
22+
}

src/TestingUtils/Microsoft.AspNetCore.Testing/test/ConditionalFactTest.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using Microsoft.AspNetCore.Testing;
65
using Xunit;
76

87
namespace Microsoft.AspNetCore.Testing
98
{
9+
[TestCaseOrderer("Microsoft.AspNetCore.Testing.AlphabeticalOrderer", "Microsoft.AspNetCore.Testing.Tests")]
1010
public class ConditionalFactTest : IClassFixture<ConditionalFactTest.ConditionalFactAsserter>
1111
{
1212
public ConditionalFactTest(ConditionalFactAsserter collector)
@@ -47,13 +47,19 @@ public void ThisTestMustRunOnCLR()
4747
#error Target frameworks need to be updated.
4848
#endif
4949

50+
// Test is named this way to be the lowest test in the alphabet, it relies on test ordering
51+
[Fact]
52+
public void ZzzzzzzEnsureThisIsTheLastTest()
53+
{
54+
Assert.True(Asserter.TestRan);
55+
}
56+
5057
public class ConditionalFactAsserter : IDisposable
5158
{
5259
public bool TestRan { get; set; }
5360

5461
public void Dispose()
5562
{
56-
Assert.True(TestRan, "If this assertion fails, a conditional fact wasn't discovered.");
5763
}
5864
}
5965
}

src/TestingUtils/Microsoft.AspNetCore.Testing/test/ConditionalTheoryTest.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using Microsoft.AspNetCore.Testing;
65
using Xunit;
76
using Xunit.Abstractions;
87

98
namespace Microsoft.AspNetCore.Testing
109
{
10+
[TestCaseOrderer("Microsoft.AspNetCore.Testing.AlphabeticalOrderer", "Microsoft.AspNetCore.Testing.Tests")]
1111
public class ConditionalTheoryTest : IClassFixture<ConditionalTheoryTest.ConditionalTheoryAsserter>
1212
{
1313
public ConditionalTheoryTest(ConditionalTheoryAsserter asserter)
@@ -101,6 +101,13 @@ public void ThisTestMustRunOnCLR(int value)
101101
#error Target frameworks need to be updated.
102102
#endif
103103

104+
// Test is named this way to be the lowest test in the alphabet, it relies on test ordering
105+
[Fact]
106+
public void ZzzzzzzEnsureThisIsTheLastTest()
107+
{
108+
Assert.True(Asserter.TestRan);
109+
}
110+
104111
public static TheoryData<Func<int, int>> GetActionTestData
105112
=> new TheoryData<Func<int, int>>
106113
{
@@ -113,7 +120,6 @@ public class ConditionalTheoryAsserter : IDisposable
113120

114121
public void Dispose()
115122
{
116-
Assert.True(TestRan, "If this assertion fails, a conditional theory wasn't discovered.");
117123
}
118124
}
119125

0 commit comments

Comments
 (0)