Skip to content

Commit

Permalink
add TestBase<TAppFixture> class
Browse files Browse the repository at this point in the history
  • Loading branch information
dj-nitehawk committed Feb 26, 2024
1 parent 4adf353 commit 0c93eb3
Show file tree
Hide file tree
Showing 28 changed files with 60 additions and 69 deletions.
2 changes: 1 addition & 1 deletion Src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>

<Version>5.22.0.13-beta</Version>
<Version>5.22.0.14-beta</Version>

<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
Expand Down
2 changes: 1 addition & 1 deletion Src/Testing/AppFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public abstract class BaseFixture : IFaker
}

/// <summary>
/// inherit this class to create a class fixture for an implementation of <see cref="TestClass{TFixture}" />.
/// inherit this class to create a class fixture for an implementation of <see cref="TestBase{TFixture}" />.
/// </summary>
/// <typeparam name="TProgram">the type of the web application to bootstrap via <see cref="WebApplicationFactory{TEntryPoint}" /></typeparam>
public abstract class AppFixture<TProgram> : BaseFixture, IAsyncLifetime where TProgram : class
Expand Down
2 changes: 0 additions & 2 deletions Src/Testing/StateFixture.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Bogus;
using Xunit;

// ReSharper disable VirtualMemberNeverOverridden.Global

namespace FastEndpoints.Testing;

public abstract class StateFixture : IAsyncLifetime, IFaker
Expand Down
71 changes: 33 additions & 38 deletions Src/Testing/TestClass.cs → Src/Testing/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,40 @@ namespace FastEndpoints.Testing;
/// with multiple test-classes.
/// </para>
/// <para>
/// to share common state between multiple test-methods of the same test-class, you can inherit the <see cref="TestClass{TAppFixture,TState}" /> abstract class and
/// to share common state between multiple test-methods of the same test-class, you can inherit the <see cref="TestBase{TAppFixture,TState}" /> abstract class and
/// provide an additional "state fixture" for the test-class.
/// </para>
/// </typeparam>
[TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)]
public abstract class TestClass<TAppFixture>(TAppFixture a, ITestOutputHelper o) : IAsyncLifetime, IClassFixture<TAppFixture> where TAppFixture : BaseFixture
public abstract class TestBase<TAppFixture> : IAsyncLifetime, IFaker, IClassFixture<TAppFixture> where TAppFixture : BaseFixture
{
static readonly Faker _faker = new();

public Faker Fake => _faker;

/// <summary>
/// override this method if you'd like to do some one-time setup for the test-class.
/// it is run before any of the test-methods of the class is executed.
/// </summary>
protected virtual Task SetupAsync()
=> Task.CompletedTask;

/// <summary>
/// override this method if you'd like to do some one-time teardown for the test-class.
/// it is run after all test-methods have executed.
/// </summary>
protected virtual Task TearDownAsync()
=> Task.CompletedTask;

Task IAsyncLifetime.InitializeAsync()
=> SetupAsync();

Task IAsyncLifetime.DisposeAsync()
=> TearDownAsync();
}

[Obsolete("Use the TestBase<TAppFixture> class going forward. This class will be removed at the next major version jump.")]
public abstract class TestClass<TAppFixture>(TAppFixture a, ITestOutputHelper o) : TestBase<TAppFixture> where TAppFixture : BaseFixture
{
/// <summary>
/// app fixture that is shared among all tests of this class
Expand All @@ -36,7 +64,6 @@ public abstract class TestClass<TAppFixture>(TAppFixture a, ITestOutputHelper o)
/// <remarks>
/// NOTE: this property will be deprecated in the future. use the <see cref="App" /> property instead.
/// </remarks>
[Obsolete("Use the 'App' property going forward.", false)]
protected TAppFixture Fixture => App;

/// <summary>
Expand All @@ -45,40 +72,14 @@ public abstract class TestClass<TAppFixture>(TAppFixture a, ITestOutputHelper o)
/// <remarks>
/// NOTE: this property will be deprecated in the future. use the <see cref="App" /> property instead.
/// </remarks>
[Obsolete("Use the 'App' property going forward.", false)]
protected TAppFixture Fx => App;

/// <summary>
/// xUnit test output helper
/// </summary>
protected ITestOutputHelper Output { get; } = o;

/// <summary>
/// bogus data generator
/// </summary>
protected Faker Fake => App.Fake;

//TODO: remove Fixture and Fx properties at v6.0

/// <summary>
/// override this method if you'd like to do some one-time setup for the test-class.
/// it is run before any of the test-methods of the class is executed.
/// </summary>
protected virtual Task SetupAsync()
=> Task.CompletedTask;

/// <summary>
/// override this method if you'd like to do some one-time teardown for the test-class.
/// it is run after all test-methods have executed.
/// </summary>
protected virtual Task TearDownAsync()
=> Task.CompletedTask;

Task IAsyncLifetime.InitializeAsync()
=> SetupAsync();

Task IAsyncLifetime.DisposeAsync()
=> TearDownAsync();
//TODO: remove this class at v6.0. only here for backwards compatibility.
}

/// <summary>
Expand All @@ -93,11 +94,5 @@ Task IAsyncLifetime.DisposeAsync()
/// with multiple test-classes.
/// </typeparam>
/// <typeparam name="TState">the type of the shared state fixture. implement a "state fixture" by inheriting <see cref="StateFixture" /> abstract class.</typeparam>
public abstract class TestClass<TAppFixture, TState>(TAppFixture a, TState s, ITestOutputHelper o) : TestClass<TAppFixture>(a, o), IClassFixture<TState>
where TAppFixture : BaseFixture where TState : StateFixture
{
/// <summary>
/// the shared state fixture for the purpose of sharing some data across all test-methods of this test-class.
/// </summary>
protected TState State { get; } = s;
}
public abstract class TestBase<TAppFixture, TState> : TestBase<TAppFixture>, IClassFixture<TState>
where TAppFixture : BaseFixture where TState : StateFixture;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Swagger;

public class SwaggerDocTests(Fixture f, ITestOutputHelper o) : TestClass<Fixture>(f, o)
public class SwaggerDocTests(Fixture App) : TestBase<Fixture>
{
//NOTE: the Verify snapshot testing doesn't seem to work in gh workflow for some reason
// so we're doing manual json file comparison.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Binding;

public class BindingTests(AppFixture f, ITestOutputHelper o) : TestClass<AppFixture>(f, o)
public class BindingTests(AppFixture App) : TestBase<AppFixture>
{
[Fact]
public async Task RouteValueReadingInEndpointWithoutRequest()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DependencyInjection;

public class DiTests(AppFixture f, ITestOutputHelper o) : TestClass<AppFixture>(f, o)
public class DiTests(AppFixture App) : TestBase<AppFixture>
{
[Fact]
public async Task Service_Registration_Generator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace EndpointTests;

public class EndpointTests(AppFixture f, ITestOutputHelper o) : TestClass<AppFixture>(f, o)
public class EndpointTests(AppFixture App) : TestBase<AppFixture>
{
[Fact]
public async Task EmptyRequest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace FixtureTests;

public class FixtureATests(FixtureA f, ITestOutputHelper o) : TestClass<FixtureA>(f, o)
public class FixtureATests : TestBase<AppFixture>
{
[Fact]
public async Task Mock_Email_Service_Is_Resolved()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace FixtureTests;

public class FixtureBTests : TestClass<FixtureB>
public class FixtureBTests : TestBase<AppFixture>
{
public FixtureBTests(FixtureB f, ITestOutputHelper o) : base(f, o) { }

//[Fact]
public async Task Actual_Email_Service_Is_Resolved()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected override async Task TearDownAsync()
}
}

public class StateFixtureTests(AppFixture f, MyStateFixture s, ITestOutputHelper o) : TestClass<AppFixture, MyStateFixture>(f, s, o)
public class StateFixtureTests(MyStateFixture State) : TestBase<AppFixture, MyStateFixture>
{
[Fact, Priority(1)]
public async Task State_Is_Injected_By_Xunit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Messaging;

public class CommandBusTests(AppFixture f, ITestOutputHelper o) : TestClass<AppFixture>(f, o)
public class CommandBusTests(AppFixture App) : TestBase<AppFixture>
{
[Fact]
public async Task Generic_Command_With_Result()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Messaging;

public class EventBusTests(AppFixture f, ITestOutputHelper o) : TestClass<AppFixture>(f, o)
public class EventBusTests(AppFixture App) : TestBase<AppFixture>
{
[Fact]
public async Task Fake_Handler_Execution()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Messaging;

public class JobQueueTests(AppFixture f, ITestOutputHelper o) : TestClass<AppFixture>(f, o)
public class JobQueueTests : TestBase<AppFixture>
{
[Fact]
public async Task JobsExecuteSuccessfully()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Misc;

public class MiscTestCases(AppFixture f, ITestOutputHelper o) : TestClass<AppFixture>(f, o)
public class MiscTestCases(AppFixture App) : TestBase<AppFixture>
{
[Fact]
public async Task ThrottledGlobalResponse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Processors;

public class ProcessorTests(AppFixture f, ITestOutputHelper o) : TestClass<AppFixture>(f, o)
public class ProcessorTests(AppFixture App) : TestBase<AppFixture>
{
[Fact]
public async Task PreProcessorShortCircuitingWhileValidatorFails()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RemoteProcedureCalls;

public class ClientStreamCommand(AppFixture f, ITestOutputHelper o) : RpcTestBase(f, o)
public class ClientStreamCommand(AppFixture f) : RpcTestBase(f)
{
[Fact]
public async Task Client_Stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RemoteProcedureCalls;

public class EventQueue(AppFixture f, ITestOutputHelper o) : RpcTestBase(f, o)
public class EventQueue(AppFixture f) : RpcTestBase(f)
{
[Fact]
public async Task Event_Queue()
Expand Down
4 changes: 2 additions & 2 deletions Tests/IntegrationTests/FastEndpoints/RPCTests/RPCTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

namespace RemoteProcedureCalls;

public class RpcTestBase : TestClass<AppFixture>
public class RpcTestBase : TestBase<AppFixture>
{
protected readonly RemoteConnection Remote;

public RpcTestBase(AppFixture f, ITestOutputHelper o) : base(f, o)
protected RpcTestBase(AppFixture App)
{
var svcCollection = new ServiceCollection();
svcCollection.AddSingleton<ILoggerFactory, LoggerFactory>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace RemoteProcedureCalls;

public class ServerStreamCommand(AppFixture f, ITestOutputHelper o) : RpcTestBase(f, o)
public class ServerStreamCommand(AppFixture f) : RpcTestBase(f)
{
[Fact]
public async Task Server_Stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RemoteProcedureCalls;

public class UnaryCommand(AppFixture f, ITestOutputHelper o) : RpcTestBase(f, o)
public class UnaryCommand(AppFixture f) : RpcTestBase(f)
{
[Fact]
public async Task Unary()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace RemoteProcedureCalls;

public class VoidCommand(AppFixture f, ITestOutputHelper o) : RpcTestBase(f, o)
public class VoidCommand(AppFixture f) : RpcTestBase(f)
{
[Fact]
public async Task Void()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Security;

public class SecurityTests(AppFixture f, ITestOutputHelper o) : TestClass<AppFixture>(f, o)
public class SecurityTests(AppFixture App) : TestBase<AppFixture>
{
[Fact]
public async Task MultiVerbEndpointAnonymousUserPutFail()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Validation;

public class ValidationTests(AppFixture f, ITestOutputHelper o) : TestClass<AppFixture>(f, o)
public class ValidationTests(AppFixture App) : TestBase<AppFixture>
{
[Fact]
public async Task HeaderMissing()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Web;

public class AdminTests(AppFixture f, ITestOutputHelper o) : TestClass<AppFixture>(f, o)
public class AdminTests(AppFixture App) : TestBase<AppFixture>
{
[Fact]
public async Task AdminLoginWithBadInput()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Int.FastEndpoints.WebTests;

public class AntiforgeryTest(AppFixture f, ITestOutputHelper o) : TestClass<AppFixture>(f, o)
public class AntiforgeryTest(AppFixture App) : TestBase<AppFixture>
{
[Fact]
public async Task Html_Form_Renders_With_Af_Token()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Web;

public class CustomersTests(AppFixture f, ITestOutputHelper o) : TestClass<AppFixture>(f, o)
public class CustomersTests(AppFixture App) : TestBase<AppFixture>
{
[Fact]
public async Task ListRecentCustomers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Web;

public class InventoryTests(AppFixture f, ITestOutputHelper o) : TestClass<AppFixture>(f, o)
public class InventoryTests(AppFixture App) : TestBase<AppFixture>
{
[Fact]
public async Task CreateProductFailValidation()
Expand Down

0 comments on commit 0c93eb3

Please sign in to comment.