[英]How to add and test against a template database in Azure DevOps
We have a C# project stored in Azure GIT with a Pipeline that currently builds, runs unit tests, verifies quality etc. Currently all our tests run with no database dependency.我们有一个 C# 项目存储在 Azure GIT 中,其中包含当前构建、运行单元测试、验证质量等的管道。目前我们所有的测试都在没有数据库依赖的情况下运行。
We have a template database backup file which can be used for some smoke tests.我们有一个模板数据库备份文件,可用于一些冒烟测试。 This is currently only used manually by starting our application against it, but it would be great to automate those tests.目前只能通过启动我们的应用程序来手动使用它,但是自动化这些测试会很棒。
Is it possible to use this database backup to perform integration tests against on Azure?是否可以使用此数据库备份对 Azure 执行集成测试? ie: IE:
If so, how (in Azure)?如果是这样,如何(在 Azure 中)?
We've struggled a bit trying to make a task in the pipeline restoring backup file, but we can't seem to get the agent to properly restore it (and have a sql server to restore on).我们在尝试在管道中恢复备份文件时遇到了一些困难,但我们似乎无法让代理正确恢复它(并且有一个 sql 服务器要恢复)。
(Yes, we know that unit tests without database dependencies are very preferable, since they are quicker and smaller, but in some situations integrations tests against a reproducable test database would be good...). (是的,我们知道没有数据库依赖的单元测试是非常可取的,因为它们更快更小,但在某些情况下,针对可重现测试数据库的集成测试会很好......)。
Thanks in advance提前致谢
Yes it is possible .是的,这是可能的。 You can create a docker-compose to do all and call it in your Azure Pipelines and run it locally as well.您可以创建一个 docker-compose 来完成所有工作,并在您的 Azure 管道中调用它并在本地运行它。
Below is an example of how to do it,下面是一个如何做到这一点的例子,
The docker-compose starts three containers: timesheets-api , sql-server-database , and integration-tests . docker-compose 启动三个容器:timesheets -api 、 sql-server-database和integration-tests 。 The first and third ones are the same image (dotnet/sdk:5.0), timesheets-api to run the API inside the container, and integration-tests to set up and run tests.第一个和第三个是相同的镜像(dotnet/sdk:5.0),timesheets-api 用于在容器内运行 API,integration-tests 用于设置和运行测试。 Both services have a configuration map to host volumes to be used inside the containers.这两个服务都有一个配置 map 来托管要在容器内使用的卷。 This setting allows the container to use the source code implemented on the host machine.此设置允许容器使用在主机上实现的源代码。
version: "3.4"
services:
timesheets-api:
container_name: timesheets-api
image: mcr.microsoft.com/dotnet/sdk:5.0
environment:
- ConnectionStrings__TimeSheetContext=Data Source=sql-server-database; Initial Catalog=TimeSheets; User Id=sa; Password=1AvenueCodePassword*
volumes:
- ./Timesheets:/api
working_dir: /api
depends_on:
- sql-server-database
command: "dotnet run"
ports:
- "8001:8081"
sql-server-database:
container_name: sql-server-database
image: mcr.microsoft.com/mssql/server
environment:
SA_PASSWORD: 1AvenueCodePassword*
ACCEPT_EULA: "Y"
ports:
- "1433:1433"
integration-tests:
container_name: integration-tests
image: mcr.microsoft.com/dotnet/sdk:5.0
environment:
- ConnectionStrings__TimeSheetContext=Data Source=sql-server-database; Initial Catalog=TimeSheets; User Id=sa; Password=1AvenueCodePassword*
volumes:
- .:/src
- ../test:/test
working_dir: /src
command:
[
"./Scripts/wait-for-it.sh",
"sql-server-database:1433",
"--",
"dotnet",
"test",
"../test/Timesheets.Tests.csproj"
]
depends_on:
- sql-server-database
More details can be found in How to Run Integration Tests Using Docker Compose and .NET 5 and dotnet-docker-integration-test github repository .更多详细信息可以在如何使用 Docker Compose 和 .NET 5和dotnet-docker-integration-test github 存储库运行集成测试中找到。
After writing your compose file, you should be testing it locally first and then you can add below task to your azure-pipeline.yaml
编写撰写文件后,您应该首先在本地对其进行测试,然后您可以将以下任务添加到您的azure-pipeline.yaml
- task: DockerCompose@0
inputs:
containerregistrytype: 'Container Registry'
dockerComposeFile: '**/docker-compose.yml'
action: 'Run a Docker Compose command'
dockerComposeCommand: 'up'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.