简体   繁体   中英

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

I want to run pytest inside a container, and I'm finding a different behavior when compared to running pytest in the host. Just a simple container, no orchestrators or anything else.

When I run pytest at the host level, all tests pass. Some need a couple of RERUN , but at the end, they pass.

For example, with a sample of 3 tests, the result of running just pytest is 3 passed, 2 rerun in 111.37 seconds .

Now, if instead of running this in the host, I build an image and run a container, the result is always something along the lines of 1 failed, 2 passed in 73.53 seconds , or actually any combination of 1 failed 2 passed , 2 failed 1 passed , 3 failed .

Notice how in this case there is no mention to any rerun operation?

The image doesn't have anything fancy, it's as simple as copying the requirements, tests and run pytest.

FROM python:3.7-slim

WORKDIR /apptests

COPY requirements requirements
COPY tests tests

RUN pip install -r requirements/tests.txt

CMD ["pytest"]

Any ideas about what might be happening? I'm not passing any flag or argument, in both cases (host or docker), it's just a raw pytest .

I thought that maybe when a test failed, it was reporting error and the container was exiting (even though I'm not using pytest -x ), but that's not the case. All tests are run.

You could store your .pytest_cache in a volume so that each time a new container is started, it has the knowledge of the previous run(s).

For example, using compose.

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

volumes:
  pytest_cache:

It was quite easy to solve. There was a missing file that needed to be copied to the container which, among other things, had:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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