【发布时间】:2022-12-29 21:04:33
【问题描述】:
我正在使用 dotnet Testcontainers 库在 Docker 中启动 SQL Server 数据库以进行集成测试。我正在使用 MSTest 框架。
我的想法是在容器启动的地方运行TestInitialize,填充数据库并在测试方法中运行断言,然后在最后运行TestCleanup,这将停止容器并处理它。
然而,容器在 Docker 中启动并且测试挂起(我猜它永远不会因为某种原因结束运行)。此外,我不是 100% 确定如何填充数据库(我找不到任何用于初始化和 SQL 脚本的命令)。
这是代码:
[TestClass]
public class WithFixtureData
{
private static readonly TestcontainersContainer _dbContainer =
new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("mcr.microsoft.com/mssql/server")
.WithEnvironment("Database", "Master")
.WithEnvironment("User Id", "SA")
.WithEnvironment("Password", "YourSTRONG!Passw0rd")
.WithCleanUp(true)
.Build();
[TestInitialize]
public async Task StartContainer()
{
await _dbContainer.StartAsync();
///container starts
}
[TestMethod]
public async Task ShouldBringCorrectFixturesBack()
{
///populate db and run assertions. This code never seems to run
}
[TestCleanup]
public async Task DisposeContainer()
{
await _dbContainer.StopAsync();
///this part of the code never seems to be executed either
}
}
【问题讨论】:
-
您的容器是否正在构建和启动?如果您阅读了mcr.microsoft.com/mssql/server文档,您至少需要提供
.WithEnvironment("ACCEPT_EULA", "Y")和.WithEnvironment("MSSQL_SA_PASSWORD", "YourSTRONG!Passw0rd")。要从容器外部访问,您可能还需要.WithExposedPorts(1433)或类似名称,然后是.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(1433))以等待 SQL Server 服务侦听该端口并准备好接受连接。 -
您应该使用您通常使用的任何机制来填充您的数据库。测试容器的工作只是为您提供一个带有正在运行的数据库实例的容器。
-
@KevinWittek Yup 明白了......我只是在尝试设置测试,以便它在测试类初始化时启动一个容器,然后在所有方法运行后停止它。
-
你说它挂了。这是否意味着它会无限期挂起?因为你最终应该会遇到超时。共享容器日志也有助于调试。此外,请参阅之前关于使用正确的 WaitStrategy 的评论。您可以在这篇博文中找到一个工作示例:atomicjar.com/2022/10/hello-dotnet
标签: c# .net sql-server mstest testcontainers