Skip to content

Commit ee0d745

Browse files
committed
wip order test case
1 parent ea4ac33 commit ee0d745

File tree

4 files changed

+137
-13
lines changed

4 files changed

+137
-13
lines changed

F2.Testing.Tests/OrderedTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using F2.Testing.Ordering;
2+
using Xunit;
3+
4+
namespace F2.Tests;
5+
6+
[TestCaseOrderer(ordererTypeName: "F2.Testing.Ordering.PriorityOrderer", ordererAssemblyName: "F2.Testing")]
7+
public class ByPriorityOrder
8+
{
9+
public static bool Test1Called;
10+
public static bool Test2ACalled;
11+
public static bool Test2BCalled;
12+
public static bool Test3Called;
13+
14+
[Fact, TestPriority(5)]
15+
public void Test3()
16+
{
17+
Test3Called = true;
18+
19+
Assert.True(Test1Called);
20+
Assert.True(Test2ACalled);
21+
Assert.True(Test2BCalled);
22+
}
23+
24+
[Fact, TestPriority(0)]
25+
public void Test2B()
26+
{
27+
Test2BCalled = true;
28+
29+
Assert.True(Test1Called);
30+
Assert.True(Test2ACalled);
31+
Assert.False(Test3Called);
32+
}
33+
34+
[Fact]
35+
public void Test2A()
36+
{
37+
Test2ACalled = true;
38+
39+
Assert.True(Test1Called);
40+
Assert.False(Test2BCalled);
41+
Assert.False(Test3Called);
42+
}
43+
44+
[Fact, TestPriority(-5)]
45+
public void Test1()
46+
{
47+
Test1Called = true;
48+
49+
Assert.False(Test2ACalled);
50+
Assert.False(Test2BCalled);
51+
Assert.False(Test3Called);
52+
}
53+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Xunit.Abstractions;
5+
using Xunit.Sdk;
6+
7+
namespace F2.Testing.Ordering;
8+
9+
/// <summary>
10+
/// <see href="https://learn.microsoft.com/it-it/dotnet/core/testing/order-unit-tests?pivots=xunit"></see>
11+
/// </summary>
12+
public class PriorityOrderer : ITestCaseOrderer
13+
{
14+
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases) where TTestCase : ITestCase
15+
{
16+
var sortedMethods = new SortedDictionary<int, List<TTestCase>>();
17+
18+
foreach (TTestCase testCase in testCases)
19+
{
20+
int priority = 0;
21+
22+
foreach (IAttributeInfo attr in testCase.TestMethod.Method.GetCustomAttributes((typeof(TestPriorityAttribute).AssemblyQualifiedName)))
23+
priority = attr.GetNamedArgument<int>("Priority");
24+
25+
GetOrCreate(sortedMethods, priority).Add(testCase);
26+
}
27+
28+
foreach (var list in sortedMethods.Keys.Select(priority => sortedMethods[priority]))
29+
{
30+
list.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name));
31+
foreach (TTestCase testCase in list)
32+
yield return testCase;
33+
}
34+
}
35+
36+
static TValue GetOrCreate<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key) where TValue : new()
37+
{
38+
ArgumentNullException.ThrowIfNull(dictionary, nameof(dictionary));
39+
40+
if (dictionary.TryGetValue(key, out TValue result)) return result;
41+
42+
result = new TValue();
43+
dictionary[key] = result;
44+
45+
return result;
46+
}
47+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace F2.Testing.Ordering;
4+
5+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
6+
public class TestPriorityAttribute : Attribute
7+
{
8+
public int Priority { get; private set; }
9+
10+
public TestPriorityAttribute(int priority) => Priority = priority;
11+
}

F2.Testing/ServerFixture.cs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System;
88
using System.Collections.Generic;
99
using System.IO;
10+
using System.Net;
1011
using System.Net.Http;
1112
using System.Threading;
1213
using System.Threading.Tasks;
@@ -20,36 +21,48 @@ public class ServerFixture<TStartup> : WebApplicationFactory<TStartup>, IAsyncLi
2021
/// Your <see cref="HttpClient"/>
2122
/// </summary>
2223
public HttpClient Client { get; internal set; }
24+
public CookieContainer CookieContainer { get; private set; } = new CookieContainer();
25+
2326
//private bool _clientConfigured = false;
2427

2528
//public ICollection GetRoutesOfController() => throw new NotImplementedException();
2629
//public ICollection GetRoutesOfControllerMethod() => throw new NotImplementedException();
2730

2831
private IServiceScope _scope;
32+
private HttpClientHandler _handler;
33+
34+
public ServerFixture()
35+
{
36+
//_handler = new HttpClientHandler
37+
//{
38+
// CookieContainer = CookieContainer,
39+
// UseCookies = true,
40+
//};
41+
42+
//var delegateHandler = new DelegatingHandler { InnerHandler = _handler };
43+
44+
//Client = this.CreateClient(new WebApplicationFactoryClientOptions { Handler = delegatingHandler });
45+
//Client = CreateDefaultClient(_handler);
46+
}
2947

3048
/// <inheritdoc cref="ServiceProviderServiceExtensions.GetService{T}(IServiceProvider)"/>
3149
public TService GetService<TService>()
3250
{
33-
return ServiceProvider.GetService<TService>();
51+
return Services.GetService<TService>();
3452
}
3553

36-
/// <inheritdoc cref="ServiceProviderServiceExtensions.GetServices{T}(IServiceProvider)"/>
37-
public IEnumerable<TService> GetServices<TService>()
54+
/// <inheritdoc cref="ServiceProviderServiceExtensions.GetRequiredService{T}(IServiceProvider)"/>
55+
public TService GetRequiredService<TService>()
3856
{
39-
return ServiceProvider.GetServices<TService>();
57+
return Services.GetRequiredService<TService>();
4058
}
4159

42-
/// <inheritdoc cref="IServiceProvider"/>
43-
public IServiceProvider ServiceProvider
60+
/// <inheritdoc cref="ServiceProviderServiceExtensions.GetServices{T}(IServiceProvider)"/>
61+
public IEnumerable<TService> GetServices<TService>()
4462
{
45-
get
46-
{
47-
_scope ??= Server.Services.CreateScope();
48-
return _scope.ServiceProvider;
49-
}
63+
return Services.GetServices<TService>();
5064
}
5165

52-
5366
/// <summary>
5467
/// Instantiate a test server with asppsettings and environment valiables
5568
/// </summary>
@@ -154,7 +167,7 @@ public Task<HttpResponseMessage> DeleteAsync(string requestUri, CancellationToke
154167
return Client.DeleteAsync(requestUri, cancellationToken);
155168
}
156169

157-
public Task InitializeAsync()
170+
public virtual Task InitializeAsync()
158171
{
159172
//if (!_clientConfigured)
160173
{

0 commit comments

Comments
 (0)