如何在 Azure DevOps(服务器)管道中测试 dockerized 应用程序?

Dav*_*cco 8 python pytest docker azure-devops fastapi

我有一个简单的 python dockerized 应用程序,其结构是

/src
 - server.py
 - test_server.py
Dockerfile
requirements.txt
Run Code Online (Sandbox Code Playgroud)

其中server.pydocker基础映像是基于 Linux 的,并公开了一个 FastAPI 端点。
为了完整起见,server.py看起来像这样:

/src
 - server.py
 - test_server.py
Dockerfile
requirements.txt
Run Code Online (Sandbox Code Playgroud)

测试应该使用 pytest (遵循https://fastapi.tiangolo.com/tutorial/testing/)完成,带有test_server.py

from fastapi import FastAPI
from pydantic import BaseModel

class Item(BaseModel):
    number: int

app = FastAPI(title="Sum one", description="Get a number, add one to it", version="0.1.0")

@app.post("/compute")
async def compute(input: Item):
    return {'result': input.number + 1}
Run Code Online (Sandbox Code Playgroud)

Dockerfile 看起来像这样:

FROM tiangolo/uvicorn-gunicorn:python3.7

COPY . /app

RUN pip install -r requirements.txt

WORKDIR /app/src

EXPOSE 8000

CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
Run Code Online (Sandbox Code Playgroud)

目前,如果我想在容器内的本地机器上运行测试,一种方法是

  1. 构建 Docker 容器
  2. 运行容器,通过获取它的名字 docker ps
  3. 运行docker exec -it <mycontainer> bash并执行pytest以查看测试通过。

现在,我想在将映像推送到我的 Docker 注册表并触发发布管道之前在 Azure DevOps(服务器)中运行测试。如果这听起来不错,那么正确的做法是什么?

到目前为止,我希望在构建管道中添加一个“PyTest”步骤的东西会神奇地工作:

在此处输入图片说明

我目前正在使用 Linux 代理,但该步骤失败

在此处输入图片说明

失败并不奇怪,因为(我认为)容器在构建后没有运行,因此 pytest 也无法在其中运行:(

解决此问题的另一种方法是在 Dockerfile 中包含 pytest 命令并在发布管道中处理测试。但是,我想将测试与最终推送到注册表并部署的容器分离。

是否有一种标准方法可以在 Azure DevOps 的 Docker 容器中运行 pytest 并获得图形报告?

JPG*_*JPG 5

azure-pipelines.yml按如下方式更新文件以在 Azure Pipelines 中运行测试

方法 1(使用 docker)

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'
steps:
- task: Docker@2
  inputs:
    command: 'build'
    Dockerfile: '**/Dockerfile'
    arguments: '-t fast-api:$(Build.BuildId)'
- script: |
    docker run fast-api:$(Build.BuildId) python -m pytest
  displayName: 'Run PyTest'
Run Code Online (Sandbox Code Playgroud)

管道成功截图

方法 2(无 docker)

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'
strategy:
  matrix:
    Python37:
      python.version: '3.7'

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '$(python.version)'
  displayName: 'Use Python $(python.version)'

- script: |
    python -m pip install --upgrade pip
    pip install -r requirements.txt
  displayName: 'Install dependencies'

- script: |
    pip install pytest pytest-azurepipelines
    python -m pytest
  displayName: 'pytest'
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我有一个简单的 FastAPI 项目,您可以根据需要参考。


Gus*_*sen 3

使用以下命令测试您的 docker 脚本pytest-azurepipelines

- script: |
    python -m pip install --upgrade pip
    pip install pytest pytest-azurepipelines
    pip install -r requirements.txt
    pip install -e .
  displayName: 'Install dependencies'

- script: |
    python -m pytest /src/test_server.py
  displayName: 'pytest'
Run Code Online (Sandbox Code Playgroud)

使用该插件运行 pytestpytest-azurepipelines将让您在 Azure Pipelines UI 中看到测试结果。

https://pypi.org/project/pytest-azurepipelines/