Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC of TUnit #447

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ dotnet_naming_rule.private_fields_should_be__camelcase.style = _camelcase

dotnet_naming_rule.private_static_fields_should_be_s_camelcase.severity = suggestion
dotnet_naming_rule.private_static_fields_should_be_s_camelcase.symbols = private_static_fields
dotnet_naming_rule.private_static_fields_should_be_s_camelcase.style = s_camelcase
dotnet_naming_rule.private_static_fields_should_be_s_camelcase.style = _camelcase

dotnet_naming_rule.public_constant_fields_should_be_pascalcase.severity = suggestion
dotnet_naming_rule.public_constant_fields_should_be_pascalcase.symbols = public_constant_fields
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SSW.CleanArchitecture.Application.Common.Interfaces;
using Microsoft.Extensions.Logging;
using SSW.CleanArchitecture.Application.Common.Interfaces;
using SSW.CleanArchitecture.Domain.Heroes;

namespace SSW.CleanArchitecture.Application.UseCases.Heroes.Commands.CreateHero;
Expand All @@ -9,11 +10,13 @@ public sealed record CreateHeroCommand(
IEnumerable<CreateHeroPowerDto> Powers) : IRequest<ErrorOr<Guid>>;

// ReSharper disable once UnusedType.Global
public sealed class CreateHeroCommandHandler(IApplicationDbContext dbContext)
public sealed class CreateHeroCommandHandler(IApplicationDbContext dbContext, ILogger<CreateHeroCommandHandler> logger)
: IRequestHandler<CreateHeroCommand, ErrorOr<Guid>>
{
public async Task<ErrorOr<Guid>> Handle(CreateHeroCommand request, CancellationToken cancellationToken)
{
logger.LogError("Creating hero with name {Name} and alias {Alias}", request.Name, request.Alias);

var hero = Hero.Create(request.Name, request.Alias);
var powers = request.Powers.Select(p => new Power(p.Name, p.PowerLevel));
hero.UpdatePowers(powers);
Expand Down
4 changes: 2 additions & 2 deletions tests/Architecture.Tests/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Application : TestBase
{
private static readonly Type IRequestHandler = typeof(IRequestHandler<,>);

[Fact]
[Test]
public void CommandHandlers_ShouldHaveCorrectSuffix()
{
var types = Types
Expand All @@ -25,7 +25,7 @@ public void CommandHandlers_ShouldHaveCorrectSuffix()
result.Should().BeSuccessful();
}

[Fact]
[Test]
public void QueryHandlers_ShouldHaveCorrectSuffix()
{
var types = Types
Expand Down
14 changes: 8 additions & 6 deletions tests/Architecture.Tests/Architecture.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
</PackageReference>
<PackageReference Include="FluentAssertions" />
<PackageReference Include="JunitXml.TestLogger" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<!-- <PackageReference Include="Microsoft.NET.Test.Sdk" />-->
<PackageReference Include="NetArchTest.Rules" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<!-- <PackageReference Include="xunit" />-->
<!-- <PackageReference Include="xunit.runner.visualstudio">-->
<!-- <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>-->
<!-- <PrivateAssets>all</PrivateAssets>-->
<!-- </PackageReference>-->
<PackageReference Include="TUnit" />

</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions tests/Architecture.Tests/Common/TestResultAssertions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FluentAssertions.Execution;
using FluentAssertions.Primitives;
using System.Text;
using TestResult = NetArchTest.Rules.TestResult;

namespace SSW.CleanArchitecture.Architecture.UnitTests.Common;

Expand Down
12 changes: 7 additions & 5 deletions tests/Architecture.Tests/Common/TestResultExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
using Xunit.Abstractions;
// using Xunit.Abstractions;

namespace SSW.CleanArchitecture.Architecture.UnitTests.Common;
using TUnit.Core.Logging;
using TestResult = NetArchTest.Rules.TestResult;

namespace SSW.CleanArchitecture.Architecture.UnitTests.Common;
public static class TestResultExtensions
{
public static void DumpFailingTypes(this TestResult result, ITestOutputHelper outputHelper)
public static void DumpFailingTypes(this TestResult result, ILogger logger)
{
if (result.IsSuccessful)
return;

outputHelper.WriteLine("Failing Types:");
logger.LogInformation("Failing Types:");

foreach (var type in result.FailingTypes)
outputHelper.WriteLine(type.FullName);
logger.LogInformation(type.FullName);
}

public static TestResultAssertions Should(this TestResult result) => new(result);
Expand Down
10 changes: 6 additions & 4 deletions tests/Architecture.Tests/Common/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using Xunit.Abstractions;
// using Xunit.Abstractions;

using TUnit.Core.Logging;

namespace SSW.CleanArchitecture.Architecture.UnitTests.Common;

public static class TypeExtensions
{
public static void Dump(this IEnumerable<Type> types, ITestOutputHelper outputHelper)
public static void Dump(this IEnumerable<Type> types, ILogger outputHelper)
{
if (!types.Any())
outputHelper.WriteLine("No types found.");
outputHelper.LogInformation("No types found.");

foreach (var type in types)
outputHelper.WriteLine(type.FullName);
outputHelper.LogInformation(type.FullName);
}
}
4 changes: 2 additions & 2 deletions tests/Architecture.Tests/DomainTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DomainModel : TestBase
private static readonly Type DomainEvent = typeof(IDomainEvent);
private static readonly Type ValueObject = typeof(IValueObject);

[Fact]
[Test]
public void DomainModel_ShouldInheritsBaseClasses()
{
// Arrange
Expand All @@ -37,7 +37,7 @@ public void DomainModel_ShouldInheritsBaseClasses()
result.Should().BeSuccessful();
}

[Fact]
[Test]
public void EntitiesAndAggregates_ShouldHavePrivateParameterlessConstructor()
{
var entityTypes = Types.InAssembly(DomainAssembly)
Expand Down
8 changes: 4 additions & 4 deletions tests/Architecture.Tests/Layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace SSW.CleanArchitecture.Architecture.UnitTests;

public class Layer : TestBase
{
[Fact]
[Test]
public void DomainLayer_Should_NotHaveDependencyOnApplication()
{
var result = Types.InAssembly(DomainAssembly)
Expand All @@ -15,7 +15,7 @@ public void DomainLayer_Should_NotHaveDependencyOnApplication()
result.Should().BeSuccessful();
}

[Fact]
[Test]
public void DomainLayer_ShouldNotHaveDependencyOn_InfrastructureLayer()
{
var result = Types.InAssembly(DomainAssembly)
Expand All @@ -26,7 +26,7 @@ public void DomainLayer_ShouldNotHaveDependencyOn_InfrastructureLayer()
result.Should().BeSuccessful();
}

[Fact]
[Test]
public void ApplicationLayer_ShouldNotHaveDependencyOn_InfrastructureLayer()
{
var result = Types.InAssembly(ApplicationAssembly)
Expand All @@ -37,7 +37,7 @@ public void ApplicationLayer_ShouldNotHaveDependencyOn_InfrastructureLayer()
result.Should().BeSuccessful();
}

[Fact]
[Test]
public void InfrastructureLayer_ShouldNotHaveDependencyOn_PresentationLayer()
{
var result = Types.InAssembly(InfrastructureAssembly)
Expand Down
2 changes: 1 addition & 1 deletion tests/Architecture.Tests/Presentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Presentation : TestBase
private static readonly Type IDbContext = typeof(IApplicationDbContext);
private static readonly Type DbContext = typeof(ApplicationDbContext);

[Fact]
[Test]
public void Endpoints_ShouldNotReferenceDbContext()
{
var types = Types
Expand Down
2 changes: 1 addition & 1 deletion tests/Architecture.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
global using Xunit;
global using TUnit;
1 change: 1 addition & 0 deletions tests/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<PackageVersion Include="FluentAssertions" Version="6.12.2" />
<PackageVersion Include="JunitXml.TestLogger" Version="4.1.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageVersion Include="TUnit" Version="0.3.14" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
Expand Down
13 changes: 7 additions & 6 deletions tests/Domain.UnitTests/Domain.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
</PackageReference>
<PackageReference Include="FluentAssertions" />
<PackageReference Include="JunitXml.TestLogger" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<!-- <PackageReference Include="Microsoft.NET.Test.Sdk" />-->
<PackageReference Include="TUnit" />
<!-- <PackageReference Include="xunit" />-->
<!-- <PackageReference Include="xunit.runner.visualstudio">-->
<!-- <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>-->
<!-- <PrivateAssets>all</PrivateAssets>-->
<!-- </PackageReference>-->
</ItemGroup>

<ItemGroup>
Expand Down
31 changes: 18 additions & 13 deletions tests/Domain.UnitTests/Heroes/HeroTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using SSW.CleanArchitecture.Domain.Heroes;
using TUnit.Core.Logging;

namespace SSW.CleanArchitecture.Domain.UnitTests.Heroes;

public class HeroTests
{
[Theory]
[InlineData("c8ad9974-ca93-44a5-9215-2f4d9e866c7a", "cc3431a8-4a31-4f76-af64-e8198279d7a4", false)]
[InlineData("c8ad9974-ca93-44a5-9215-2f4d9e866c7a", "c8ad9974-ca93-44a5-9215-2f4d9e866c7a", true)]
private readonly DefaultLogger _logger = new();

[Test]
[Arguments("c8ad9974-ca93-44a5-9215-2f4d9e866c7a", "cc3431a8-4a31-4f76-af64-e8198279d7a4", false)]
[Arguments("c8ad9974-ca93-44a5-9215-2f4d9e866c7a", "c8ad9974-ca93-44a5-9215-2f4d9e866c7a", true)]
public void HeroId_ShouldBeComparable(string stringGuid1, string stringGuid2, bool isEqual)
{
// Arrange
Expand All @@ -24,9 +27,11 @@ public void HeroId_ShouldBeComparable(string stringGuid1, string stringGuid2, bo
id2.Value.Should().Be(guid2);
}

[Fact]
[Test]
public void Create_WithValidNameAndAlias_ShouldSucceed()
{
_logger.LogError("Create_WithValidNameAndAlias_ShouldSucceed");

// Arrange
var name = "name";
var alias = "alias";
Expand All @@ -40,7 +45,7 @@ public void Create_WithValidNameAndAlias_ShouldSucceed()
hero.Alias.Should().Be(alias);
}

[Fact]
[Test]
public void Create_WithSameNameAndAlias_ShouldSucceed()
{
// Arrange
Expand All @@ -51,10 +56,10 @@ public void Create_WithSameNameAndAlias_ShouldSucceed()
Hero.Create(name, alias);
}

[Theory]
[InlineData(null, "alias")]
[InlineData("name", null)]
[InlineData(null, null)]
[Test]
[Arguments(null, "alias")]
[Arguments("name", null)]
[Arguments(null, null)]
public void Create_WithNullTitleOrAlias_ShouldThrow(string? name, string? alias)
{
// Arrange
Expand All @@ -66,7 +71,7 @@ public void Create_WithNullTitleOrAlias_ShouldThrow(string? name, string? alias)
act.Should().Throw<ArgumentException>().WithMessage("Value cannot be null*");
}

[Fact]
[Test]
public void AddPower_ShouldUpdateHeroPowerLevel()
{
// Act
Expand All @@ -79,7 +84,7 @@ public void AddPower_ShouldUpdateHeroPowerLevel()
hero.Powers.Should().HaveCount(2);
}

[Fact]
[Test]
public void RemovePower_ShouldUpdateHeroPowerLevel()
{
// Act
Expand All @@ -95,7 +100,7 @@ public void RemovePower_ShouldUpdateHeroPowerLevel()
hero.Powers.Should().HaveCount(1);
}

[Fact]
[Test]
public void AddPower_ShouldRaisePowerLevelUpdatedEvent()
{
// Act
Expand All @@ -117,7 +122,7 @@ public void AddPower_ShouldRaisePowerLevelUpdatedEvent()
hero.Powers.Should().ContainSingle("Super-strength");
}

[Fact]
[Test]
public void RemovePower_ShouldRaisePowerLevelUpdatedEvent()
{
// Act
Expand Down
18 changes: 9 additions & 9 deletions tests/Domain.UnitTests/Heroes/PowerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace SSW.CleanArchitecture.Domain.UnitTests.Heroes;

public class PowerTests
{
[Fact]
[Test]
public void Power_ShouldBeCreatable()
{
// Arrange
Expand All @@ -20,7 +20,7 @@ public void Power_ShouldBeCreatable()
power.PowerLevel.Should().Be(powerLevel);
}

[Fact]
[Test]
public void Power_ShouldBeComparable()
{
// Arrange
Expand All @@ -36,13 +36,13 @@ public void Power_ShouldBeComparable()
areEqual.Should().BeTrue();
}

[Theory]
[InlineData(-1, true)]
[InlineData(0, true)]
[InlineData(1, false)]
[InlineData(9, false)]
[InlineData(10, false)]
[InlineData(11, true)]
[Test]
[Arguments(-1, true)]
[Arguments(0, true)]
[Arguments(1, false)]
[Arguments(9, false)]
[Arguments(10, false)]
[Arguments(11, true)]
public void Power_WithInvalidPowerLevel_ShouldThrow(int powerLevel, bool shouldThrow)
{
// Arrange
Expand Down
6 changes: 3 additions & 3 deletions tests/Domain.UnitTests/Teams/MissionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace SSW.CleanArchitecture.Domain.UnitTests.Teams;

public class MissionTests
{
[Theory]
[InlineData("c8ad9974-ca93-44a5-9215-2f4d9e866c7a", "cc3431a8-4a31-4f76-af64-e8198279d7a4", false)]
[InlineData("c8ad9974-ca93-44a5-9215-2f4d9e866c7a", "c8ad9974-ca93-44a5-9215-2f4d9e866c7a", true)]
[Test]
[Arguments("c8ad9974-ca93-44a5-9215-2f4d9e866c7a", "cc3431a8-4a31-4f76-af64-e8198279d7a4", false)]
[Arguments("c8ad9974-ca93-44a5-9215-2f4d9e866c7a", "c8ad9974-ca93-44a5-9215-2f4d9e866c7a", true)]
public void MissionId_ShouldBeComparable(string stringGuid1, string stringGuid2, bool isEqual)
{
// Arrange
Expand Down
Loading