【问题标题】:Pytest generate html in Docker container?Pytest 在 Docker 容器中生成 html?
【发布时间】:2021-03-29 15:52:07
【问题描述】:

我正在尝试开发一个用于生成 html 报告的 Web 自动化测试应用程序。它可以在本地工作,但是在尝试对其进行 dockerize 时我遇到了麻烦。

这是 dockerfile 中最重要的一行(我认为)

CMD ["pytest", "--html=report.html", "tests/test_your_info_page.py"]

最终目标是在创建容器时开始测试。但似乎没有生成“report.html”文件

docker 文件的其余部分如下所示

FROM python:3.9 AS webautomation

# install google chrome
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
RUN apt-get -y update
RUN apt-get install -y google-chrome-stable

# install chromedriver
RUN apt-get install -yqq unzip
RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/

# set display port to avoid crash
ENV DISPLAY=:99

COPY requirements.txt .

RUN pip install -r requirements.txt

WORKDIR .
COPY . .

CMD ["pytest", "--html=report.html", "tests/test_your_info_page.py"]

FROM nginx:alpine

COPY --from=webautomation . /usr/share/nginx/html

而 requirements.txt 看起来像

pytest==6.2.2
selenium==3.141.0
pytest-html==3.1.1

项目结构

-tests
      -test_your_info_page.py

【问题讨论】:

  • 你能分享一下你的项目结构吗? test_your_info_page.py 是否在测试文件下?
  • 我在那里添加了一个截图。希望这会有所帮助
  • 首先您需要指定您将复制的文件不是全部​​。如果您使用 CMD 或 ENTRYPOINT,它们将在容器启动时运行。所以你需要在构建时使用RUN来创建报告文件。但是如果它退出并出现错误构建失败。
  • 我希望它在创建容器时运行。不是在构建 docker 映像时。
  • 但是当容器启动时,dockerfile 的第一步将不再存在。只有第二部分会运行。因此,当您启动容器时,您不能在第一部分运行 cmd 命令并在另一个部分中使用。您需要在构建时使用 RUN 创建报告文件。

标签: python html docker containers pytest


【解决方案1】:

问题似乎与多阶段构建有关。删除 FROM nginx 我现在可以在 docker 容器中运行 pytest。

更新

使用 docker cp 命令我可以复制本地机器上生成的报告并在浏览器中查看

【讨论】:

    猜你喜欢
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    相关资源
    最近更新 更多