繁体   English   中英

看起来 Pytest 在容器中运行时无法重新运行测试

[英]It looks like Pytest can't rerun tests when running in a container

我想在容器中运行pytest ,与在主机中运行pytest相比,我发现了不同的行为。 只是一个简单的容器,没有编排器或其他任何东西。

当我在主机级别运行pytest时,所有测试都通过了。 有些需要几个RERUN ,但最后,他们通过了。

例如,对于 3 次测试的样本,仅运行pytest的结果是3 passed, 2 rerun in 111.37 seconds

现在,如果不是在主机中运行它,而是构建一个映像并运行一个容器,结果总是类似于1 failed, 2 passed in 73.53 seconds ,或者实际上是1 failed 2 passed , 2 failed 1 passed的任何组合2 failed 1 passed3 failed

请注意在这种情况下如何没有提及任何rerun操作?

该图像没有任何花哨的东西,它就像复制需求,测试并运行 pytest 一样简单。

FROM python:3.7-slim

WORKDIR /apptests

COPY requirements requirements
COPY tests tests

RUN pip install -r requirements/tests.txt

CMD ["pytest"]

关于可能发生的事情有什么想法吗? 我没有传递任何标志或参数,在这两种情况下(主机或码头工人),它只是一个原始pytest

我认为也许当测试失败时,它会报告错误并且容器正在退出(即使我没有使用pytest -x ),但事实并非如此。 所有测试都运行。

您可以将.pytest_cache存储在一个卷中,以便每次启动一个新容器时,它都知道之前的运行情况。

例如,使用撰写。

services:
  app:
    image: myapp
    command: python -m pytest -v --failed-first
    volumes: [ pytest_cache:/app/.pytest_cache ]

volumes:
  pytest_cache:

这很容易解决。 有一个丢失的文件需要复制到容器中,其中包括:

[tool:pytest]
testpaths=tests
python_files=*.py
addopts = --junitxml=./reports/junit.xml
          --verbose
          --capture=no
          --ignore=setup.py
          --reruns 2                <----------------
          --exitfirst

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM