| File: SqlServerContainerFixture.cs | Web Access |
| Project: src\tests\Aspire.Hosting.Azure.Tests\Aspire.Hosting.Azure.Tests.csproj (Aspire.Hosting.Azure.Tests) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using Aspire.TestUtilities; using Testcontainers.MsSql; namespace Aspire.Hosting.Azure.Tests; /// <summary> /// Starts a single SQL Server container shared by every test in a class, so the T-SQL emitted by /// <see cref="AzureSqlServerResource"/> can be executed against a real database engine. /// </summary> public sealed class SqlServerContainerFixture : IAsyncLifetime { public MsSqlContainer? Container { get; private set; } public string GetConnectionString() => Container?.GetConnectionString() ?? throw new InvalidOperationException("The test container was not initialized."); public async ValueTask InitializeAsync() { if (RequiresFeatureAttribute.IsFeatureSupported(TestFeature.Testcontainers)) { Container = new MsSqlBuilder() .WithImage($"{SqlServerContainerImageTags.Registry}/{SqlServerContainerImageTags.Image}:{SqlServerContainerImageTags.Tag}") .Build(); await Container.StartAsync(); } } public async ValueTask DisposeAsync() { if (Container is not null) { await Container.DisposeAsync(); } } }