add Testing Framework

This commit is contained in:
Jonas Seiler 2025-04-02 09:32:19 +02:00
parent 48afaeec3f
commit 4054629ad7
3 changed files with 80 additions and 0 deletions

View File

@ -5,6 +5,8 @@ VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PTV-Street", "src\PTV-Street.csproj", "{B1AF2E92-D6AD-4999-8341-60CE9A728C3B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "tests", "tests\tests.csproj", "{FEB9535F-F5DF-43B1-B6B2-24D5D2102E78}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -18,5 +20,9 @@ Global
{B1AF2E92-D6AD-4999-8341-60CE9A728C3B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B1AF2E92-D6AD-4999-8341-60CE9A728C3B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B1AF2E92-D6AD-4999-8341-60CE9A728C3B}.Release|Any CPU.Build.0 = Release|Any CPU
{FEB9535F-F5DF-43B1-B6B2-24D5D2102E78}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FEB9535F-F5DF-43B1-B6B2-24D5D2102E78}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FEB9535F-F5DF-43B1-B6B2-24D5D2102E78}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FEB9535F-F5DF-43B1-B6B2-24D5D2102E78}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,41 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Testcontainers.PostgreSql;
public class StreetWebApplicationFactory<Program> : WebApplicationFactory<Program>, IAsyncLifetime where Program : class
{
private readonly PostgreSqlContainer dbTestContainer;
public StreetWebApplicationFactory()
{
dbTestContainer = new PostgreSqlBuilder().WithImage("postgis/postgis:17-3.5").Build();
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
// Use the test database
services.AddDbContext<StreetDbContext>(options => options.UseNpgsql(dbTestContainer.GetConnectionString()));
// Ensure the database is created before running the tests
using var scope = services.BuildServiceProvider().CreateScope();
var db = scope.ServiceProvider.GetRequiredService<StreetDbContext>();
db.Database.EnsureCreated();
});
}
public async Task InitializeAsync()
{
await dbTestContainer.StartAsync();
}
async Task IAsyncLifetime.DisposeAsync()
{
await dbTestContainer.DisposeAsync();
}
}

33
tests/tests.csproj Normal file
View File

@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="FluentAssertions" Version="8.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="npgsql" Version="9.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
<PackageReference Include="Testcontainers" Version="4.3.0" />
<PackageReference Include="Testcontainers.PostgreSql" Version="4.3.0" />
<PackageReference Include="Testcontainers.Xunit" Version="4.3.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\PTV-Street.csproj" />
</ItemGroup>
</Project>