Home > Back-end >  Runnnig Pytest while building docker compose up
Runnnig Pytest while building docker compose up

Time:05-06

My projects structure looks as following.

app/
    docker-compose.yml
    test_some_pytest.py # this have some pytest code.
    tests.Dockerfile

my tests.Dockerfile looks as following.

from python:3.4-alpine
RUN python --version
RUN pip --version
COPY . /APP
WORKDIR /APP
RUN pip install pytest
RUN ["pytest"]

and docker-compose.yml as following.

services
  tests:
    build:
      context: .
      dockerfile: tests.Dockerfile

When I run docker-compose up --build tests. the pytest also run but probably at some other place. it shows the following output.

.
.
.
Removing intermediate container 96f9a8ba43d2
 ---> 82c89715d4c0
Step 7/7 : RUN ["pytest"]
 ---> Running in c30ee497e5f5
============================= test session starts ==============================
platform linux -- Python 3.4.10, pytest-4.6.11, py-1.10.0, pluggy-0.13.1
rootdir: /python-test-calculator
collected 0 items

========================= no tests ran in 0.00 seconds =========================
The command 'pytest' returned a non-zero code: 5
ERROR: Service 'tests' failed to build : Build failed

CodePudding user response:

If I use your tests.Dockerfile exactly as written, the following docker-compose.yaml:

version: "3"

services:
  tests:
    build:
      context: .
      dockerfile: tests.Dockerfile

And the following test_some_pytest.py:

def test_something():
    assert True

It successfully runs pytest when I run docker-compose build:

$ docker-compose build
Building tests

[...]

Step 7/7 : RUN ["pytest"]
 ---> Running in 8d8a1f44913f
============================= test session starts ==============================
platform linux -- Python 3.4.10, pytest-4.6.11, py-1.10.0, pluggy-0.13.1
rootdir: /APP
collected 1 item

test_some_pytest.py .                                                    [100%]

=========================== 1 passed in 0.01 seconds ===========================
Removing intermediate container 8d8a1f44913f
 ---> 055afd5b1f8d
Successfully built 055afd5b1f8d
Successfully tagged docker_tests:latest

You can see from the above output that pytest discovered and successfully ran 1 test.

  • Related