简体   繁体   English

如何在 Azure DevOps 中添加和测试模板数据库

[英]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:

  • Add the backup file somewhere in Azure在 Azure 某处添加备份文件
  • So that the pipeline could restore the backup file to a server以便管道可以将备份文件恢复到服务器
  • Run some integration tests that access this restored database运行一些访问这个恢复的数据库的集成测试
  • Reproduce the same (fetch backup, restore and run tests) locally?在本地复制相同的内容(获取备份、恢复和运行测试)?

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 -apisql-server-databaseintegration-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 5dotnet-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.

相关问题 如何将测试计划许可证用户添加到 Azure DevOps 服务组织? - How to add Test plan license users to Azure DevOps service Organization? 如何在 azure devops 模板任务中参数化 azureSubscription - How to parametrize azureSubscription in azure devops template task 如何修改 Azure Devops 邮件通知模板? - How to modify Azure Devops email notification template? 如何在 Azure Devops 构建或发布期间通过 C# 应用程序添加测试结果附件? - How to add test result attachments during Azure Devops build or release via a C# application? Azure Devops,如何在 devops backlog 中添加“工作项类型” - Azure Devops, how to add “Work Item type” in devops backlog 如何在Azure DevOps XML中添加ESLint设置? - How to add ESLint Settings in Azure DevOps XML? 如何将环境变量添加到 Azure Devops 管道中 - How to add env vars into Azure Devops pipeline 如何使用 Azure DevOps 为 PostgreSQL 自动部署 Azure 数据库 - How to Automate Deployment of Azure Database for PostgreSQL using Azure DevOps 针对 azure devops 服务连接授权管道 - authorising pipelines against azure devops service connection 如何使用 Azure Devops 服务更新 Azure Devops 中测试用例的“区域路径” Rest API - How to update "Area Path" of a test case in Azure Devops using Azure Devops Services Rest API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM