Skip to content

Commit

Permalink
Fix ConditionalFact and ConditionalTheory (#2241)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanConroy authored Aug 28, 2019
1 parent 033ea04 commit ae9d51f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Microsoft.AspNetCore.Testing
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
[XunitTestCaseDiscoverer("Microsoft.AspNetCore.Testing.xunit." + nameof(ConditionalFactDiscoverer), "Microsoft.AspNetCore.Testing")]
[XunitTestCaseDiscoverer("Microsoft.AspNetCore.Testing." + nameof(ConditionalFactDiscoverer), "Microsoft.AspNetCore.Testing")]
public class ConditionalFactAttribute : FactAttribute
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Microsoft.AspNetCore.Testing
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
[XunitTestCaseDiscoverer("Microsoft.AspNetCore.Testing.xunit." + nameof(ConditionalTheoryDiscoverer), "Microsoft.AspNetCore.Testing")]
[XunitTestCaseDiscoverer("Microsoft.AspNetCore.Testing." + nameof(ConditionalTheoryDiscoverer), "Microsoft.AspNetCore.Testing")]
public class ConditionalTheoryAttribute : TheoryAttribute
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace Microsoft.AspNetCore.Testing
{
public class AlphabeticalOrderer : ITestCaseOrderer
{
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases)
where TTestCase : ITestCase
{
var result = testCases.ToList();
result.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name));
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.AspNetCore.Testing;
using Xunit;

namespace Microsoft.AspNetCore.Testing
{
[TestCaseOrderer("Microsoft.AspNetCore.Testing.AlphabeticalOrderer", "Microsoft.AspNetCore.Testing.Tests")]
public class ConditionalFactTest : IClassFixture<ConditionalFactTest.ConditionalFactAsserter>
{
public ConditionalFactTest(ConditionalFactAsserter collector)
Expand Down Expand Up @@ -47,13 +47,19 @@ public void ThisTestMustRunOnCLR()
#error Target frameworks need to be updated.
#endif

// Test is named this way to be the lowest test in the alphabet, it relies on test ordering
[Fact]
public void ZzzzzzzEnsureThisIsTheLastTest()
{
Assert.True(Asserter.TestRan);
}

public class ConditionalFactAsserter : IDisposable
{
public bool TestRan { get; set; }

public void Dispose()
{
Assert.True(TestRan, "If this assertion fails, a conditional fact wasn't discovered.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.AspNetCore.Testing;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.AspNetCore.Testing
{
[TestCaseOrderer("Microsoft.AspNetCore.Testing.AlphabeticalOrderer", "Microsoft.AspNetCore.Testing.Tests")]
public class ConditionalTheoryTest : IClassFixture<ConditionalTheoryTest.ConditionalTheoryAsserter>
{
public ConditionalTheoryTest(ConditionalTheoryAsserter asserter)
Expand Down Expand Up @@ -101,6 +101,13 @@ public void ThisTestMustRunOnCLR(int value)
#error Target frameworks need to be updated.
#endif

// Test is named this way to be the lowest test in the alphabet, it relies on test ordering
[Fact]
public void ZzzzzzzEnsureThisIsTheLastTest()
{
Assert.True(Asserter.TestRan);
}

public static TheoryData<Func<int, int>> GetActionTestData
=> new TheoryData<Func<int, int>>
{
Expand All @@ -113,7 +120,6 @@ public class ConditionalTheoryAsserter : IDisposable

public void Dispose()
{
Assert.True(TestRan, "If this assertion fails, a conditional theory wasn't discovered.");
}
}

Expand Down

0 comments on commit ae9d51f

Please sign in to comment.