Skip to content

Commit

Permalink
Restructured web testing projects
Browse files Browse the repository at this point in the history
  • Loading branch information
jezzsantos committed Dec 3, 2024
1 parent 25a019a commit 15f864c
Show file tree
Hide file tree
Showing 50 changed files with 216 additions and 125 deletions.
7 changes: 4 additions & 3 deletions docs/how-to-guides/100-build-adapter-third-party.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,10 @@ These are just some of the reasons why these integration tests should be tagged

Create a new test class in the `Infrastructure.Shared.IntegrationTests` project.

Mark up your class with the attributes: `[Trait("Category", "Integration.External")]` and `[Collection("External")]`.
Mark up your class with the attributes: `[Trait("Category", "Integration.External")]` and `[Collection("EXTERNAL")]`.

> The `[Collection("External")]` attribute is used to ensure that your tests do NOT run in parallel and share the same common setup and tear down methods.
> The
`[Collection("EXTERNAL")]` attribute is used to ensure that your tests do NOT run in parallel and share the same common setup and tear down methods.

Inherit from the `ExternalApiSpec` class, which will require you to inject the `ExternalApiSpec` instance into your constructor. This is also where you can get access to other DI dependencies for use in your tests.

Expand All @@ -239,7 +240,7 @@ For example,

```c#
[Trait("Category", "Integration.External")]
[Collection("External")]
[Collection("EXTERNAL")]
public class MyCustomHttpServiceClientSpec : ExternalApiSpec
{
private readonly MyCustomHttpServiceClient _serviceClient;
Expand Down
23 changes: 23 additions & 0 deletions src/ApiHost1.IntegrationTests/ApiHost1.IntegrationTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\ApiHost1\ApiHost1.csproj" />
<ProjectReference Include="..\IntegrationTesting.WebApi.Common\IntegrationTesting.WebApi.Common.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
</ItemGroup>

<ItemGroup>
<Content Include="appsettings.Testing.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
using IntegrationTesting.WebApi.Common;
using Xunit;

namespace Infrastructure.Web.Api.IntegrationTests;
namespace ApiHost1.IntegrationTests;

[Trait("Category", "Integration.API")]
[Collection("API")]
public class AuthNApiSpec : WebApiSpec<ApiHost1.Program>
public class AuthNApiSpec : WebApiSpec<Program>
{
private readonly IConfigurationSettings _settings;
private readonly ITokensService _tokensService;

public AuthNApiSpec(WebApiSetup<ApiHost1.Program> setup) : base(setup)
public AuthNApiSpec(WebApiSetup<Program> setup) : base(setup)
{
EmptyAllRepositories();
_settings = setup.GetRequiredService<IConfigurationSettings>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
using IntegrationTesting.WebApi.Common;
using Xunit;

namespace Infrastructure.Web.Api.IntegrationTests;
namespace ApiHost1.IntegrationTests;

[Trait("Category", "Integration.API")]
[Collection("API")]
public class AuthZApiSpec : WebApiSpec<ApiHost1.Program>
public class AuthZApiSpec : WebApiSpec<Program>
{
private readonly IConfigurationSettings _settings;
private readonly ITokensService _tokensService;

public AuthZApiSpec(WebApiSetup<ApiHost1.Program> setup) : base(setup)
public AuthZApiSpec(WebApiSetup<Program> setup) : base(setup)
{
EmptyAllRepositories();
_settings = setup.GetRequiredService<IConfigurationSettings>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
using IntegrationTesting.WebApi.Common;
using Xunit;

namespace Infrastructure.Web.Api.IntegrationTests;
namespace ApiHost1.IntegrationTests;

[Trait("Category", "Integration.API")]
[Collection("API")]
public class ContentNegotiationApiSpec : WebApiSpec<ApiHost1.Program>
public class ContentNegotiationApiSpec : WebApiSpec<Program>
{
public ContentNegotiationApiSpec(WebApiSetup<ApiHost1.Program> setup) : base(setup)
public ContentNegotiationApiSpec(WebApiSetup<Program> setup) : base(setup)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
using Xunit;
using Task = System.Threading.Tasks.Task;

namespace Infrastructure.Web.Api.IntegrationTests;
namespace ApiHost1.IntegrationTests;

[Trait("Category", "Integration.API")]
[Collection("API")]
public class DataFormatsApiSpec : WebApiSpec<ApiHost1.Program>
public class DataFormatsApiSpec : WebApiSpec<Program>
{
public DataFormatsApiSpec(WebApiSetup<ApiHost1.Program> setup) : base(setup)
public DataFormatsApiSpec(WebApiSetup<Program> setup) : base(setup)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
using IntegrationTesting.WebApi.Common;
using Xunit;

namespace Infrastructure.Web.Api.IntegrationTests;
namespace ApiHost1.IntegrationTests;

[Trait("Category", "Integration.API")]
[Collection("API")]
public class DefaultStatusCodeApiSpec : WebApiSpec<ApiHost1.Program>
public class DefaultStatusCodeApiSpec : WebApiSpec<Program>
{
public DefaultStatusCodeApiSpec(WebApiSetup<ApiHost1.Program> setup) : base(setup)
public DefaultStatusCodeApiSpec(WebApiSetup<Program> setup) : base(setup)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
using IntegrationTesting.WebApi.Common;
using Xunit;

namespace Infrastructure.Web.Api.IntegrationTests;
namespace ApiHost1.IntegrationTests;

[Trait("Category", "Integration.API")]
[Collection("API")]
public class ErrorApiSpec : WebApiSpec<ApiHost1.Program>
public class ErrorApiSpec : WebApiSpec<Program>
{
public ErrorApiSpec(WebApiSetup<ApiHost1.Program> setup) : base(setup)
public ErrorApiSpec(WebApiSetup<Program> setup) : base(setup)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
using JetBrains.Annotations;
using Xunit;

namespace Infrastructure.Web.Api.IntegrationTests;
namespace ApiHost1.IntegrationTests;

[UsedImplicitly]
[Trait("Category", "Integration.API")]
[Collection("API")]
public class GeneralApiSpec : WebApiSpec<ApiHost1.Program>
public class GeneralApiSpec : WebApiSpec<Program>
{
public GeneralApiSpec(WebApiSetup<ApiHost1.Program> setup) : base(setup)
public GeneralApiSpec(WebApiSetup<Program> setup) : base(setup)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
using IntegrationTesting.WebApi.Common;
using Xunit;

namespace Infrastructure.Web.Api.IntegrationTests;
namespace ApiHost1.IntegrationTests;

[Trait("Category", "Integration.API")]
[Collection("API")]
public class HealthCheckApiSpec : WebApiSpec<ApiHost1.Program>
public class HealthCheckApiSpec : WebApiSpec<Program>
{
public HealthCheckApiSpec(WebApiSetup<ApiHost1.Program> setup) : base(setup)
public HealthCheckApiSpec(WebApiSetup<Program> setup) : base(setup)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
using JetBrains.Annotations;
using Xunit;

namespace Infrastructure.Web.Api.IntegrationTests;
namespace ApiHost1.IntegrationTests;

[UsedImplicitly]
public class RequestCorrelationApiSpec
{
[Trait("Category", "Integration.API")]
[Collection("API")]
public class GivenAnHttpClient : WebApiSpec<ApiHost1.Program>
public class GivenAnHttpClient : WebApiSpec<Program>
{
public GivenAnHttpClient(WebApiSetup<ApiHost1.Program> setup) : base(setup)
public GivenAnHttpClient(WebApiSetup<Program> setup) : base(setup)
{
}

Expand Down Expand Up @@ -66,9 +66,9 @@ public async Task WhenGetWithXCorrelationId_ThenReturnsSameResponseHeader()

[Trait("Category", "Integration.API")]
[Collection("API")]
public class GivenAJsonClient : WebApiSpec<ApiHost1.Program>
public class GivenAJsonClient : WebApiSpec<Program>
{
public GivenAJsonClient(WebApiSetup<ApiHost1.Program> setup) : base(setup)
public GivenAJsonClient(WebApiSetup<Program> setup) : base(setup)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
using IntegrationTesting.WebApi.Common;
using JetBrains.Annotations;
using Xunit;
using Resources = ApiHost1.Resources;

namespace Infrastructure.Web.Api.IntegrationTests;
namespace ApiHost1.IntegrationTests;

[UsedImplicitly]
public class ValidationApiSpec
{
[Trait("Category", "Integration.API")]
[Collection("API")]
public class GivenAnUnvalidatedRequest : WebApiSpec<ApiHost1.Program>
public class GivenAnUnvalidatedRequest : WebApiSpec<Program>
{
public GivenAnUnvalidatedRequest(WebApiSetup<ApiHost1.Program> setup) : base(setup)
public GivenAnUnvalidatedRequest(WebApiSetup<Program> setup) : base(setup)
{
}

Expand All @@ -34,9 +33,9 @@ public async Task WhenGetRequest_ThenReturns200()

[Trait("Category", "Integration.API")]
[Collection("API")]
public class GivenAValidatedGetRequest : WebApiSpec<ApiHost1.Program>
public class GivenAValidatedGetRequest : WebApiSpec<Program>
{
public GivenAValidatedGetRequest(WebApiSetup<ApiHost1.Program> setup) : base(setup)
public GivenAValidatedGetRequest(WebApiSetup<Program> setup) : base(setup)
{
}

Expand Down Expand Up @@ -148,9 +147,9 @@ await Api.GetAsync(new ValidationsValidatedGetTestingOnlyRequest

[Trait("Category", "Integration.API")]
[Collection("API")]
public class GivenAValidatedPostRequest : WebApiSpec<ApiHost1.Program>
public class GivenAValidatedPostRequest : WebApiSpec<Program>
{
public GivenAValidatedPostRequest(WebApiSetup<ApiHost1.Program> setup) : base(setup)
public GivenAValidatedPostRequest(WebApiSetup<Program> setup) : base(setup)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"ApplicationServices": {
"Persistence": {
"LocalMachineJsonFileStore": {
"RootPath": "./saastack/testing/website"
"RootPath": "./saastack/testing/apihost1"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ApiHost1/ApiHost1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Infrastructure.Web.Api.IntegrationTests</_Parameter1>
<_Parameter1>ApiHost1.IntegrationTests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Infrastructure.Shared.IntegrationTests.ApplicationServices.External;

[Trait("Category", "Integration.External")]
[Collection("External")]
[Collection("EXTERNAL")]
public class FlagsmithHttpServiceClientSpec : ExternalApiSpec
{
private const string TestTenant1 = "atenant1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Infrastructure.Shared.IntegrationTests.ApplicationServices.External;

[Trait("Category", "Integration.External")]
[Collection("External")]
[Collection("EXTERNAL")]
public class GravatarHttpServiceClientSpec : ExternalApiSpec
{
private readonly GravatarHttpServiceClient _serviceClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Infrastructure.Shared.IntegrationTests.ApplicationServices.External;

[Trait("Category", "Integration.External")]
[Collection("External")]
[Collection("EXTERNAL")]
public class MailgunHttpServiceClientSpec : ExternalApiSpec
{
private readonly string _recipientEmail;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Infrastructure.Shared.IntegrationTests.ApplicationServices.External;

[Trait("Category", "Integration.External")]
[Collection("External")]
[Collection("EXTERNAL")]
public class TwilioHttpServiceClientSpec : ExternalApiSpec
{
private readonly string _recipientPhoneNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Infrastructure.Shared.IntegrationTests.ApplicationServices.External;

[Trait("Category", "Integration.External")]
[Collection("External")]
[Collection("EXTERNAL")]
public class UserPilotHttpServiceClientSpec : ExternalApiSpec
{
private readonly UserPilotHttpServiceClient _serviceClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.58" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.14" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<_Parameter1>$(AssemblyName).UnitTests</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Infrastructure.Web.Website.IntegrationTests</_Parameter1>
<_Parameter1>WebsiteHost.IntegrationTests</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Infrastructure.Web.Api.IntegrationTests</_Parameter1>
Expand Down
2 changes: 1 addition & 1 deletion src/IntegrationTesting.WebApi.Common/ExternalApiSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace IntegrationTesting.WebApi.Common;
/// <summary>
/// Provides an xUnit collection for running "External" tests together
/// </summary>
[CollectionDefinition("External", DisableParallelization = false)]
[CollectionDefinition("EXTERNAL", DisableParallelization = false)]
public class AllExternalSpecs : ICollectionFixture<ExternalApiSetup>;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.58" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.6" />
<PackageReference Include="System.Resources.Extensions" Version="8.0.0" />
</ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions src/IntegrationTesting.WebApi.Common/WebApiSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@

namespace IntegrationTesting.WebApi.Common;

/// <summary>
/// Provides an xUnit collection for running "Integration.API" tests together
/// </summary>
[CollectionDefinition("API", DisableParallelization = false)]
public class AllApiSpecs<THost> : ICollectionFixture<WebApiSetup<THost>>
where THost : class;

/// <summary>
/// Provides an xUnit collection for running "Integration.Website" tests together
/// </summary>
[CollectionDefinition("WEBSITE", DisableParallelization = true)]
public class AllWebsiteSpecs<THost> : ICollectionFixture<WebApiSetup<THost>>
where THost : class;

/// <summary>
/// Provides an xUnit class fixture for integration testing APIs
/// </summary>
Expand Down
Loading

0 comments on commit 15f864c

Please sign in to comment.