Migrate backend test suite from xUnit v2 to v3

- Directory.Packages.props: xunit 2.9.3 -> xunit.v3 4.0.0, bump
  xunit.runner.visualstudio to the matching 4.0.0
- All 5 test csproj: xunit -> xunit.v3 package reference, add
  <OutputType>Exe</OutputType> (v3 test projects are standalone
  executables)
- backend/global.json: opt dotnet test into the new MTP-native mode
  (.NET 10 SDK removed the old VSTest bridge for MTP-based test
  projects like xunit v3)
- IAsyncLifetime implementations (HandlerTestBase, ApiTestFixture,
  InfrastructureTestFixture, StorageServiceTests): v3's IAsyncLifetime
  now IS IAsyncDisposable, so InitializeAsync/DisposeAsync return
  ValueTask instead of Task. ApiTestFixture specifically now overrides
  WebApplicationFactory.DisposeAsync instead of implementing
  IAsyncLifetime.DisposeAsync explicitly, since v3 collapses both to
  the same single DisposeAsync slot
- Replaced CancellationToken.None with TestContext.Current.CancellationToken
  across the suite (a v3-only API) and added it to every call that
  was silently omitting a CancellationToken argument, per the new
  xUnit1051 analyzer warnings that came with the v3 package

Verified: dotnet build (0 warnings, 0 errors) and dotnet test, 250/250
passing under the new MTP runner.
This commit is contained in:
2026-09-09 22:11:29 +02:00
parent b57949a6bc
commit 98e6dfc4c4
62 changed files with 286 additions and 272 deletions
@@ -3,6 +3,7 @@
<PropertyGroup>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
@@ -14,7 +15,7 @@
<PackageReference Include="NSubstitute" />
<PackageReference Include="Testcontainers" />
<PackageReference Include="Testcontainers.PostgreSql" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.v3" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
@@ -14,8 +14,8 @@ public sealed class AuditableEntityInterceptorTests(InfrastructureTestFixture fi
var before = DateTime.UtcNow;
var user = new User { Id = Random.Shared.NextInt64(1, long.MaxValue), Name = "audit-user", NormalizedName = "AUDIT-USER" };
await database.Users.AddAsync(user);
await database.SaveChangesAsync(CancellationToken.None);
await database.Users.AddAsync(user, TestContext.Current.CancellationToken);
await database.SaveChangesAsync(TestContext.Current.CancellationToken);
var after = DateTime.UtcNow;
@@ -29,14 +29,14 @@ public sealed class AuditableEntityInterceptorTests(InfrastructureTestFixture fi
await using var database = fixture.CreateDatabase();
var user = new User { Id = Random.Shared.NextInt64(1, long.MaxValue), Name = "audit-user-2", NormalizedName = "AUDIT-USER-2" };
await database.Users.AddAsync(user);
await database.SaveChangesAsync(CancellationToken.None);
await database.Users.AddAsync(user, TestContext.Current.CancellationToken);
await database.SaveChangesAsync(TestContext.Current.CancellationToken);
var originalCreatedAt = user.CreatedAt;
await Task.Delay(TimeSpan.FromMilliseconds(50));
await Task.Delay(TimeSpan.FromMilliseconds(50), TestContext.Current.CancellationToken);
user.Name = "audit-user-2-renamed";
await database.SaveChangesAsync(CancellationToken.None);
await database.SaveChangesAsync(TestContext.Current.CancellationToken);
Assert.Equal(originalCreatedAt, user.CreatedAt);
Assert.True(user.UpdatedAt > originalCreatedAt,
@@ -15,7 +15,7 @@ public sealed class DatabaseTests(InfrastructureTestFixture fixture)
await using var database = fixture.CreateDatabase();
var user = new User { Id = Random.Shared.NextInt64(1, long.MaxValue), Name = "roundtrip-user", NormalizedName = "ROUNDTRIP-USER" };
await database.Users.AddAsync(user);
await database.Users.AddAsync(user, TestContext.Current.CancellationToken);
var track = new LocalTrack
{
@@ -27,11 +27,11 @@ public sealed class DatabaseTests(InfrastructureTestFixture fixture)
Pictures = new TrackPictures { OriginalName = "cover.webp", ProcessingStatus = ProcessingStatus.Pending },
Audio = new TrackAudio { OriginalName = "song.mp3", TranscodeStatus = ProcessingStatus.Pending }
};
await database.LocalTracks.AddAsync(track);
await database.SaveChangesAsync(CancellationToken.None);
await database.LocalTracks.AddAsync(track, TestContext.Current.CancellationToken);
await database.SaveChangesAsync(TestContext.Current.CancellationToken);
await using var reloaded = fixture.CreateDatabase();
var stored = await reloaded.Tracks.AsNoTracking().SingleAsync(t => t.Id == track.Id);
var stored = await reloaded.Tracks.AsNoTracking().SingleAsync(t => t.Id == track.Id, TestContext.Current.CancellationToken);
Assert.IsType<LocalTrack>(stored);
Assert.Equal("Round Trip Track", stored.Title);
@@ -46,13 +46,13 @@ public sealed class DatabaseTests(InfrastructureTestFixture fixture)
await using var database = fixture.CreateDatabase();
var videoId = $"dup-{Guid.NewGuid():N}";
await database.ExternalTracks.AddAsync(BuildExternalTrack(videoId));
await database.SaveChangesAsync(CancellationToken.None);
await database.ExternalTracks.AddAsync(BuildExternalTrack(videoId), TestContext.Current.CancellationToken);
await database.SaveChangesAsync(TestContext.Current.CancellationToken);
await using var second = fixture.CreateDatabase();
await second.ExternalTracks.AddAsync(BuildExternalTrack(videoId));
await second.ExternalTracks.AddAsync(BuildExternalTrack(videoId), TestContext.Current.CancellationToken);
await Assert.ThrowsAsync<DbUpdateException>(() => second.SaveChangesAsync(CancellationToken.None));
await Assert.ThrowsAsync<DbUpdateException>(() => second.SaveChangesAsync(TestContext.Current.CancellationToken));
}
[Fact]
@@ -66,16 +66,16 @@ public sealed class DatabaseTests(InfrastructureTestFixture fixture)
var link = new AlbumHasTrack { AlbumId = album.Id, TrackId = track.Id, TrackNumber = 1 };
await database.AddRangeAsync(user, album, track, link);
await database.SaveChangesAsync(CancellationToken.None);
await database.SaveChangesAsync(TestContext.Current.CancellationToken);
await using var deleter = fixture.CreateDatabase();
var albumToDelete = await deleter.UserAlbums.SingleAsync(a => a.Id == album.Id);
var albumToDelete = await deleter.UserAlbums.SingleAsync(a => a.Id == album.Id, TestContext.Current.CancellationToken);
deleter.UserAlbums.Remove(albumToDelete);
await deleter.SaveChangesAsync(CancellationToken.None);
await deleter.SaveChangesAsync(TestContext.Current.CancellationToken);
await using var verifier = fixture.CreateDatabase();
Assert.False(await verifier.AlbumHasTracks.AnyAsync(l => l.AlbumId == album.Id));
Assert.True(await verifier.ExternalTracks.AnyAsync(t => t.Id == track.Id));
Assert.False(await verifier.AlbumHasTracks.AnyAsync(l => l.AlbumId == album.Id, TestContext.Current.CancellationToken));
Assert.True(await verifier.ExternalTracks.AnyAsync(t => t.Id == track.Id, TestContext.Current.CancellationToken));
}
private static ExternalTrack BuildExternalTrack(string externalId) => new()
@@ -24,9 +24,9 @@ public sealed class PictureServiceTests
{
using var source = CreateSourceImage(800, 600);
await using var resized = await CreateService().ResizePictureAsWebpAsync(source, 200, 200, CancellationToken.None);
await using var resized = await CreateService().ResizePictureAsWebpAsync(source, 200, 200, TestContext.Current.CancellationToken);
using var result = await Image.LoadAsync(resized);
using var result = await Image.LoadAsync(resized, TestContext.Current.CancellationToken);
Assert.InRange(result.Width, 1, 200);
Assert.InRange(result.Height, 1, 200);
Assert.True(result.Width == 200 || result.Height == 200, "expected the longer side to land exactly on the target box");
@@ -37,9 +37,9 @@ public sealed class PictureServiceTests
{
using var source = CreateSourceImage(100, 100);
await using var resized = await CreateService().ResizePictureAsWebpAsync(source, 50, 50, CancellationToken.None);
await using var resized = await CreateService().ResizePictureAsWebpAsync(source, 50, 50, TestContext.Current.CancellationToken);
var format = await Image.DetectFormatAsync(resized);
var format = await Image.DetectFormatAsync(resized, TestContext.Current.CancellationToken);
Assert.Equal("WEBP", format?.Name, ignoreCase: true);
}
@@ -48,9 +48,9 @@ public sealed class PictureServiceTests
{
using var source = CreateSourceImage(400, 200);
await using var resized = await CreateService().ResizePictureAsWebpAsync(source, 100, 100, CancellationToken.None);
await using var resized = await CreateService().ResizePictureAsWebpAsync(source, 100, 100, TestContext.Current.CancellationToken);
using var result = await Image.LoadAsync(resized);
using var result = await Image.LoadAsync(resized, TestContext.Current.CancellationToken);
Assert.Equal(100, result.Width);
Assert.Equal(50, result.Height);
}
@@ -45,7 +45,7 @@ public sealed class PlayListPresetSeederTests(InfrastructureTestFixture fixture)
private static async Task RunToCompletionAsync(PlayListPresetSeeder seeder)
{
await seeder.StartAsync(CancellationToken.None);
await seeder.StartAsync(TestContext.Current.CancellationToken);
await (seeder.ExecuteTask ?? Task.CompletedTask);
}
@@ -66,7 +66,7 @@ public sealed class PlayListPresetSeederTests(InfrastructureTestFixture fixture)
routes.PresetLargePicturePath
})
{
var metadata = await storage.HeadObjectAsync(InfrastructureTestFixture.S3Bucket, key, CancellationToken.None);
var metadata = await storage.HeadObjectAsync(InfrastructureTestFixture.S3Bucket, key, TestContext.Current.CancellationToken);
Assert.True(metadata is not null, $"expected {key} to have been seeded");
}
}
@@ -82,7 +82,7 @@ public sealed class PlayListPresetSeederTests(InfrastructureTestFixture fixture)
await RunToCompletionAsync(secondSeeder);
var metadata = await storage.HeadObjectAsync(
InfrastructureTestFixture.S3Bucket, configuration.Routes.PresetSmallPicturePath, CancellationToken.None);
InfrastructureTestFixture.S3Bucket, configuration.Routes.PresetSmallPicturePath, TestContext.Current.CancellationToken);
Assert.NotNull(metadata);
}
@@ -96,7 +96,7 @@ public sealed class PlayListPresetSeederTests(InfrastructureTestFixture fixture)
await RunToCompletionAsync(seeder);
var metadata = await storage.HeadObjectAsync(
InfrastructureTestFixture.S3Bucket, configuration.Routes.PresetSmallPicturePath, CancellationToken.None);
InfrastructureTestFixture.S3Bucket, configuration.Routes.PresetSmallPicturePath, TestContext.Current.CancellationToken);
Assert.Null(metadata);
}
}
@@ -16,7 +16,7 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture) : IAs
private Database database = null!;
private StorageService service = null!;
public Task InitializeAsync()
public ValueTask InitializeAsync()
{
database = fixture.CreateDatabase();
var storageConfiguration = new InfrastructureStorageConfiguration
@@ -28,10 +28,10 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture) : IAs
UseHttp = true,
};
service = new StorageService(database, fixture.CreateS3Client(), NullLogger<StorageService>.Instance, storageConfiguration);
return Task.CompletedTask;
return ValueTask.CompletedTask;
}
public Task DisposeAsync() => database.DisposeAsync().AsTask();
public ValueTask DisposeAsync() => database.DisposeAsync();
[Fact]
public async Task UploadThenGetFile_RoundTripsTheSameBytes()
@@ -39,11 +39,11 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture) : IAs
var key = $"tests/{Guid.NewGuid():N}/round-trip.txt";
var content = "hello seaweedfs"u8.ToArray();
await service.UploadFileAsync(new MemoryStream(content), "text/plain", InfrastructureTestFixture.S3Bucket, key, CancellationToken.None);
await service.UploadFileAsync(new MemoryStream(content), "text/plain", InfrastructureTestFixture.S3Bucket, key, TestContext.Current.CancellationToken);
await using var stream = await service.GetFileAsync(InfrastructureTestFixture.S3Bucket, key, CancellationToken.None);
await using var stream = await service.GetFileAsync(InfrastructureTestFixture.S3Bucket, key, TestContext.Current.CancellationToken);
using var reader = new StreamReader(stream, Encoding.UTF8);
var readBack = await reader.ReadToEndAsync();
var readBack = await reader.ReadToEndAsync(TestContext.Current.CancellationToken);
Assert.Equal("hello seaweedfs", readBack);
}
@@ -52,9 +52,9 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture) : IAs
public async Task HeadObjectAsync_ExistingObject_ReturnsMetadata()
{
var key = $"tests/{Guid.NewGuid():N}/head.txt";
await service.UploadFileAsync(new MemoryStream("x"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, key, CancellationToken.None);
await service.UploadFileAsync(new MemoryStream("x"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, key, TestContext.Current.CancellationToken);
var metadata = await service.HeadObjectAsync(InfrastructureTestFixture.S3Bucket, key, CancellationToken.None);
var metadata = await service.HeadObjectAsync(InfrastructureTestFixture.S3Bucket, key, TestContext.Current.CancellationToken);
Assert.NotNull(metadata);
Assert.Equal(1, metadata.ContentLength);
@@ -63,7 +63,7 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture) : IAs
[Fact]
public async Task HeadObjectAsync_MissingObject_ReturnsNull()
{
var metadata = await service.HeadObjectAsync(InfrastructureTestFixture.S3Bucket, $"tests/{Guid.NewGuid():N}/missing.txt", CancellationToken.None);
var metadata = await service.HeadObjectAsync(InfrastructureTestFixture.S3Bucket, $"tests/{Guid.NewGuid():N}/missing.txt", TestContext.Current.CancellationToken);
Assert.Null(metadata);
}
@@ -72,11 +72,11 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture) : IAs
public async Task RemoveFileAsync_ExistingObject_DeletesIt()
{
var key = $"tests/{Guid.NewGuid():N}/to-remove.txt";
await service.UploadFileAsync(new MemoryStream("x"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, key, CancellationToken.None);
await service.UploadFileAsync(new MemoryStream("x"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, key, TestContext.Current.CancellationToken);
await service.RemoveFileAsync(InfrastructureTestFixture.S3Bucket, key, CancellationToken.None);
await service.RemoveFileAsync(InfrastructureTestFixture.S3Bucket, key, TestContext.Current.CancellationToken);
Assert.Null(await service.HeadObjectAsync(InfrastructureTestFixture.S3Bucket, key, CancellationToken.None));
Assert.Null(await service.HeadObjectAsync(InfrastructureTestFixture.S3Bucket, key, TestContext.Current.CancellationToken));
}
[Fact]
@@ -84,22 +84,22 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture) : IAs
{
var sourceKey = $"tests/{Guid.NewGuid():N}/source.txt";
var destinationKey = $"tests/{Guid.NewGuid():N}/destination.txt";
await service.UploadFileAsync(new MemoryStream("copy-me"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, sourceKey, CancellationToken.None);
await service.UploadFileAsync(new MemoryStream("copy-me"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, sourceKey, TestContext.Current.CancellationToken);
await service.CopyFileAsync(InfrastructureTestFixture.S3Bucket, sourceKey, InfrastructureTestFixture.S3Bucket, destinationKey, CancellationToken.None);
await service.CopyFileAsync(InfrastructureTestFixture.S3Bucket, sourceKey, InfrastructureTestFixture.S3Bucket, destinationKey, TestContext.Current.CancellationToken);
Assert.NotNull(await service.HeadObjectAsync(InfrastructureTestFixture.S3Bucket, destinationKey, CancellationToken.None));
Assert.NotNull(await service.HeadObjectAsync(InfrastructureTestFixture.S3Bucket, destinationKey, TestContext.Current.CancellationToken));
}
[Fact]
public async Task ListObjectsAsync_ReturnsEveryObjectUnderThePrefix()
{
var prefix = $"tests/{Guid.NewGuid():N}";
await service.UploadFileAsync(new MemoryStream("a"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, $"{prefix}/a.txt", CancellationToken.None);
await service.UploadFileAsync(new MemoryStream("b"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, $"{prefix}/b.txt", CancellationToken.None);
await service.UploadFileAsync(new MemoryStream("a"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, $"{prefix}/a.txt", TestContext.Current.CancellationToken);
await service.UploadFileAsync(new MemoryStream("b"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, $"{prefix}/b.txt", TestContext.Current.CancellationToken);
var keys = new List<string>();
await foreach (var item in service.ListObjectsAsync(InfrastructureTestFixture.S3Bucket, prefix, CancellationToken.None))
await foreach (var item in service.ListObjectsAsync(InfrastructureTestFixture.S3Bucket, prefix, TestContext.Current.CancellationToken))
keys.Add(item.Key);
Assert.Equal([$"{prefix}/a.txt", $"{prefix}/b.txt"], keys.Order());
@@ -109,13 +109,13 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture) : IAs
public async Task RemoveFolderAsync_DeletesEveryObjectUnderThePrefix()
{
var prefix = $"tests/{Guid.NewGuid():N}";
await service.UploadFileAsync(new MemoryStream("a"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, $"{prefix}/a.txt", CancellationToken.None);
await service.UploadFileAsync(new MemoryStream("b"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, $"{prefix}/nested/b.txt", CancellationToken.None);
await service.UploadFileAsync(new MemoryStream("a"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, $"{prefix}/a.txt", TestContext.Current.CancellationToken);
await service.UploadFileAsync(new MemoryStream("b"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, $"{prefix}/nested/b.txt", TestContext.Current.CancellationToken);
await service.RemoveFolderAsync(InfrastructureTestFixture.S3Bucket, prefix, CancellationToken.None);
await service.RemoveFolderAsync(InfrastructureTestFixture.S3Bucket, prefix, TestContext.Current.CancellationToken);
var remaining = new List<string>();
await foreach (var item in service.ListObjectsAsync(InfrastructureTestFixture.S3Bucket, prefix, CancellationToken.None))
await foreach (var item in service.ListObjectsAsync(InfrastructureTestFixture.S3Bucket, prefix, TestContext.Current.CancellationToken))
remaining.Add(item.Key);
Assert.Empty(remaining);
}
@@ -126,7 +126,7 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture) : IAs
var key = $"tests/{Guid.NewGuid():N}/presigned.txt";
var uploadUrl = await service.GetUploadUrlAsync(
InfrastructureTestFixture.S3Bucket, key, "text/plain", TimeSpan.FromMinutes(5), CancellationToken.None);
InfrastructureTestFixture.S3Bucket, key, "text/plain", TimeSpan.FromMinutes(5), TestContext.Current.CancellationToken);
using var httpClient = new HttpClient();
using var request = new HttpRequestMessage(HttpMethod.Put, uploadUrl)
@@ -141,10 +141,10 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture) : IAs
// this header (see StorageService.GetUploadUrlAsync) — the actual PUT has to send it too,
// or the signature no longer matches and the server answers 403.
request.Headers.TryAddWithoutValidation("If-None-Match", "*");
var response = await httpClient.SendAsync(request);
var response = await httpClient.SendAsync(request, TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var metadata = await service.HeadObjectAsync(InfrastructureTestFixture.S3Bucket, key, CancellationToken.None);
var metadata = await service.HeadObjectAsync(InfrastructureTestFixture.S3Bucket, key, TestContext.Current.CancellationToken);
Assert.NotNull(metadata);
Assert.Equal("presigned content".Length, metadata.ContentLength);
}
@@ -44,7 +44,7 @@ public sealed class InfrastructureTestFixture : IAsyncLifetime
public string S3ServiceUrl => $"http://{seaweedFs.Hostname}:{seaweedFs.GetMappedPublicPort(8333)}";
public async Task InitializeAsync()
public async ValueTask InitializeAsync()
{
await Task.WhenAll(postgres.StartAsync(), seaweedFs.StartAsync());
@@ -56,7 +56,7 @@ public sealed class InfrastructureTestFixture : IAsyncLifetime
await s3.PutBucketAsync(S3Bucket);
}
public async Task DisposeAsync()
public async ValueTask DisposeAsync()
{
await postgres.DisposeAsync();
await seaweedFs.DisposeAsync();