Skip to content

Commit

Permalink
Add AppBrix.Testing.Xunit project
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinAtanasov committed Oct 16, 2023
1 parent 30f96b7 commit bf41b55
Show file tree
Hide file tree
Showing 60 changed files with 126 additions and 48 deletions.
7 changes: 7 additions & 0 deletions AppBrix.sln
Expand Up @@ -145,6 +145,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AppBrix.Web.Client.Tests",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AppBrix.Web.Server.Tests", "Tests\AppBrix.Web.Server.Tests\AppBrix.Web.Server.Tests.csproj", "{7DB372D5-0673-4852-B9C1-8F31EF57493F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppBrix.Testing.Xunit", "Testing\AppBrix.Testing.Xunit\AppBrix.Testing.Xunit.csproj", "{7600BD9E-E721-4D6A-AEAF-B8FEC4A40A23}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -384,6 +386,10 @@ Global
{F5954D10-7C98-4223-B135-4A37A2DB6703}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F5954D10-7C98-4223-B135-4A37A2DB6703}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F5954D10-7C98-4223-B135-4A37A2DB6703}.Release|Any CPU.Build.0 = Release|Any CPU
{7600BD9E-E721-4D6A-AEAF-B8FEC4A40A23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7600BD9E-E721-4D6A-AEAF-B8FEC4A40A23}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7600BD9E-E721-4D6A-AEAF-B8FEC4A40A23}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7600BD9E-E721-4D6A-AEAF-B8FEC4A40A23}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -447,6 +453,7 @@ Global
{A0C52482-3245-4E24-9131-CE118DED13F5} = {32FF1B46-A25D-406E-9F71-1FD25A3F52E8}
{7DB372D5-0673-4852-B9C1-8F31EF57493F} = {32FF1B46-A25D-406E-9F71-1FD25A3F52E8}
{F5954D10-7C98-4223-B135-4A37A2DB6703} = {2943556E-0B95-4E17-BE68-9DEF1CB20B5A}
{7600BD9E-E721-4D6A-AEAF-B8FEC4A40A23} = {2943556E-0B95-4E17-BE68-9DEF1CB20B5A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A6D3EF8B-A819-42CD-8987-D250605868C5}
Expand Down
23 changes: 23 additions & 0 deletions Testing/AppBrix.Testing.Xunit/AppBrix.Testing.Xunit.csproj
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>AppBrix.Testing.Xunit</AssemblyName>
<AssemblyTitle>AppBrix.Testing.Xunit</AssemblyTitle>
<Description>Tooling for testing AppBrix applications and modules using xUnit.</Description>
<PackageId>AppBrix.Testing.xUnit</PackageId>
<PackageTags>AppBrix Testing xUnit</PackageTags>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\AppBrix.Testing\AppBrix.Testing.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="xunit" Version="2.5.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
Expand Up @@ -3,11 +3,10 @@

using AppBrix.Configuration.Memory;
using AppBrix.Modules;
using FluentAssertions;
using System;
using System.Threading.Tasks;

namespace AppBrix.Testing;
namespace AppBrix.Testing.Xunit;

/// <summary>
/// A base testing class that holds an application.
Expand Down Expand Up @@ -45,31 +44,17 @@ public virtual void Dispose()
/// <param name="action">The action to be invoked.</param>
/// <param name="duration">The maximum allowed duration.</param>
/// <param name="firstPass">The maximum allowed duration on the first pass.</param>
protected void AssertPerformance(Action action, TimeSpan duration = default, TimeSpan firstPass = default)
{
if (duration == default)
duration = TimeSpan.FromMilliseconds(100);
if (firstPass == default)
firstPass = TimeSpan.FromMilliseconds(5000);

action.ExecutionTime().Should().BeLessThan(firstPass, "this is a performance test first pass");

GC.Collect();

action.ExecutionTime().Should().BeLessThan(duration, "this is a performance test");
}
protected void AssertPerformance(Action action, TimeSpan duration = default, TimeSpan firstPass = default) =>
action.AssertPerformance(duration, firstPass);

/// <summary>
/// Checks that the function is executed under a specified time.
/// </summary>
/// <param name="func">The function to be invoked.</param>
/// <param name="duration">The maximum allowed duration.</param>
/// <param name="firstPass">The maximum allowed duration on the first pass.</param>
protected void AssertPerformance(Func<Task> func, TimeSpan duration = default, TimeSpan firstPass = default)
{
var action = () => func().GetAwaiter().GetResult();
this.AssertPerformance(action, duration, firstPass);
}
protected void AssertPerformance(Func<Task> func, TimeSpan duration = default, TimeSpan firstPass = default) =>
func.AssertPerformance(duration, firstPass);
#endregion

#region Private fields and constants
Expand Down
5 changes: 0 additions & 5 deletions Testing/AppBrix.Testing/AppBrix.Testing.csproj
Expand Up @@ -15,11 +15,6 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="xunit" Version="2.5.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
38 changes: 36 additions & 2 deletions Testing/AppBrix.Testing/TestExtensions.cs
Expand Up @@ -4,16 +4,48 @@
using FluentAssertions;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace AppBrix.Testing;
namespace AppBrix;

/// <summary>
/// Contains commonly used testing utilities.
/// </summary>
public static class TestExtensions
{
/// <summary>
/// Checks that the action is executed under a specified time.
/// It does one initial pass to make sure that the code path is loaded and hot.
/// </summary>
/// <param name="action">The action to be invoked.</param>
/// <param name="duration">The maximum allowed duration.</param>
/// <param name="firstPass">The maximum allowed duration on the first pass.</param>
public static void AssertPerformance(this Action action, TimeSpan duration = default, TimeSpan firstPass = default)
{
if (duration == default)
duration = TimeSpan.FromMilliseconds(100);
if (firstPass == default)
firstPass = TimeSpan.FromMilliseconds(5000);

action.ExecutionTime().Should().BeLessThan(firstPass, "this is a performance test first pass");

GC.Collect();

action.ExecutionTime().Should().BeLessThan(duration, "this is a performance test");
}

/// <summary>
/// Checks that the function is executed under a specified time.
/// </summary>
/// <param name="func">The function to be invoked.</param>
/// <param name="duration">The maximum allowed duration.</param>
/// <param name="firstPass">The maximum allowed duration on the first pass.</param>
public static void AssertPerformance(this Func<Task> func, TimeSpan duration = default, TimeSpan firstPass = default)
{
var action = () => func().GetAwaiter().GetResult();
action.AssertPerformance(duration, firstPass);
}

/// <summary>
/// Asserts that a function will return a predefined value by a certain timeout.
/// </summary>
Expand All @@ -22,6 +54,7 @@ public static class TestExtensions
/// <param name="result">The result which should be returned by the function before the timeout expires.</param>
/// <param name="because">A formatted phrase explaining why the assertion should be satisfied. If the phrase does not start with the word because, it is prepended to the message.</param>
/// <param name="becauseArgs">Zero or more values to use for filling in any <see cref="string.Format(string, object[])"/> compatible placeholders.</param>
/// <returns>A task that completes when the function returns the predefined value</returns>
public static Task ShouldReturn<T>(this Func<T> func, T result, string because = "", params object[] becauseArgs) =>
func.ShouldReturn(result, TimeSpan.FromSeconds(15), because, becauseArgs);

Expand All @@ -34,6 +67,7 @@ public static class TestExtensions
/// <param name="timeout">The timeout.</param>
/// <param name="because">A formatted phrase explaining why the assertion should be satisfied. If the phrase does not start with the word because, it is prepended to the message.</param>
/// <param name="becauseArgs">Zero or more values to use for filling in any <see cref="string.Format(string, object[])"/> compatible placeholders.</param>
/// <returns>A task that completes when the function returns the predefined value</returns>
private static async Task ShouldReturn<T>(this Func<T> func, T result, TimeSpan timeout, string because = "", params object[] becauseArgs)
{
if (func is null)
Expand Down
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<ProjectReference Include="..\..\Modules\AppBrix.Caching.Memory\AppBrix.Caching.Memory.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing\AppBrix.Testing.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing.Xunit\AppBrix.Testing.Xunit.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Tests/AppBrix.Caching.Memory.Tests/MemoryCacheTests.cs
Expand Up @@ -3,6 +3,7 @@

using AppBrix.Caching.Memory.Tests.Mocks;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using System;
using System.Threading;
Expand Down
2 changes: 1 addition & 1 deletion Tests/AppBrix.Caching.Tests/AppBrix.Caching.Tests.csproj
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<ProjectReference Include="..\..\Modules\AppBrix.Caching\AppBrix.Caching.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing\AppBrix.Testing.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing.Xunit\AppBrix.Testing.Xunit.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Tests/AppBrix.Caching.Tests/CacheTests.cs
Expand Up @@ -3,6 +3,7 @@

using AppBrix.Caching.Tests.Mocks;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using Microsoft.Extensions.Caching.Distributed;
using System.Threading.Tasks;
Expand Down
2 changes: 1 addition & 1 deletion Tests/AppBrix.Cloning.Tests/AppBrix.Cloning.Tests.csproj
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<ProjectReference Include="..\..\Modules\AppBrix.Cloning\AppBrix.Cloning.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing\AppBrix.Testing.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing.Xunit\AppBrix.Testing.Xunit.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Tests/AppBrix.Cloning.Tests/CloningTests.cs
Expand Up @@ -3,6 +3,7 @@

using AppBrix.Cloning.Tests.Mocks;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using System;
using System.Collections;
Expand Down
Expand Up @@ -18,7 +18,7 @@
<ProjectReference Include="..\..\Core\AppBrix.Configuration.Files\AppBrix.Configuration.Files.csproj" />
<ProjectReference Include="..\..\Core\AppBrix.Configuration.Json\AppBrix.Configuration.Json.csproj" />
<ProjectReference Include="..\..\Core\AppBrix.Configuration.Yaml\AppBrix.Configuration.Yaml.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing\AppBrix.Testing.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing.Xunit\AppBrix.Testing.Xunit.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Tests/AppBrix.Configuration.Tests/ConfigServiceTests.cs
Expand Up @@ -3,6 +3,7 @@

using AppBrix.Configuration.Tests.Mocks;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using System;
using Xunit;
Expand Down
Expand Up @@ -4,6 +4,7 @@
using AppBrix.Configuration.Files;
using AppBrix.Configuration.Tests.Mocks;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using System;
using System.IO;
Expand Down
Expand Up @@ -4,6 +4,7 @@
using AppBrix.Configuration.Json;
using AppBrix.Configuration.Tests.Mocks;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using System;
using Xunit;
Expand Down
Expand Up @@ -4,6 +4,7 @@
using AppBrix.Configuration.Memory;
using AppBrix.Configuration.Tests.Mocks;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using System;
using Xunit;
Expand Down
Expand Up @@ -4,6 +4,7 @@
using AppBrix.Configuration.Tests.Mocks;
using AppBrix.Configuration.Yaml;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using System;
using Xunit;
Expand Down
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<ProjectReference Include="..\..\Modules\AppBrix.Container\AppBrix.Container.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing\AppBrix.Testing.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing.Xunit\AppBrix.Testing.Xunit.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Tests/AppBrix.Container.Tests/ContainerTests.cs
Expand Up @@ -3,6 +3,7 @@

using AppBrix.Container.Tests.Mocks;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using System;
using System.Collections.Generic;
Expand Down
2 changes: 1 addition & 1 deletion Tests/AppBrix.Data.Tests/AppBrix.Data.Tests.csproj
Expand Up @@ -17,7 +17,7 @@
<ProjectReference Include="..\..\Modules\AppBrix.Data.InMemory\AppBrix.Data.InMemory.csproj" />
<ProjectReference Include="..\..\Modules\AppBrix.Data.Migrations\AppBrix.Data.Migrations.csproj" />
<ProjectReference Include="..\..\Modules\AppBrix.Data.Sqlite\AppBrix.Data.Sqlite.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing\AppBrix.Testing.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing.Xunit\AppBrix.Testing.Xunit.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Tests/AppBrix.Data.Tests/DbContextServiceTests.cs
Expand Up @@ -5,6 +5,7 @@
using AppBrix.Data.InMemory;
using AppBrix.Data.Tests.Mocks;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using System;
using System.Linq;
Expand Down
1 change: 1 addition & 0 deletions Tests/AppBrix.Data.Tests/InMemoryDataTests.cs
Expand Up @@ -5,6 +5,7 @@
using AppBrix.Data.Migrations;
using AppBrix.Data.Tests.Mocks;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using System;
using System.Linq;
Expand Down
Expand Up @@ -5,6 +5,7 @@
using AppBrix.Data.Migrations;
using AppBrix.Data.Tests.Mocks;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using System;
using Xunit;
Expand Down
1 change: 1 addition & 0 deletions Tests/AppBrix.Data.Tests/SqliteDataTests.cs
Expand Up @@ -7,6 +7,7 @@
using AppBrix.Data.Sqlite;
using AppBrix.Data.Tests.Mocks;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using System;
Expand Down
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<ProjectReference Include="..\..\Modules\AppBrix.Events.Async\AppBrix.Events.Async.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing\AppBrix.Testing.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing.Xunit\AppBrix.Testing.Xunit.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Expand Up @@ -5,6 +5,7 @@
using AppBrix.Events.Async.Tests.Mocks;
using AppBrix.Events.Contracts;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using System;
using System.Collections.Generic;
Expand Down
Expand Up @@ -5,6 +5,7 @@
using AppBrix.Events.Async.Tests.Mocks;
using AppBrix.Events.Contracts;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using System;
using System.Collections.Generic;
Expand Down
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<ProjectReference Include="..\..\Modules\AppBrix.Events.Delayed\AppBrix.Events.Delayed.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing\AppBrix.Testing.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing.Xunit\AppBrix.Testing.Xunit.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Tests/AppBrix.Events.Delayed.Tests/DelayedEventHubTests.cs
Expand Up @@ -5,6 +5,7 @@
using AppBrix.Events.Delayed.Configuration;
using AppBrix.Events.Delayed.Tests.Mocks;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using System;
using System.Collections.Generic;
Expand Down
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<ProjectReference Include="..\..\Modules\AppBrix.Events.Schedule.Cron\AppBrix.Events.Schedule.Cron.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing\AppBrix.Testing.csproj" />
<ProjectReference Include="..\..\Testing\AppBrix.Testing.Xunit\AppBrix.Testing.Xunit.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Expand Up @@ -4,6 +4,7 @@
using AppBrix.Events.Schedule.Contracts;
using AppBrix.Events.Schedule.Cron.Tests.Mocks;
using AppBrix.Testing;
using AppBrix.Testing.Xunit;
using FluentAssertions;
using System;
using System.Collections.Generic;
Expand Down

0 comments on commit bf41b55

Please sign in to comment.