Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Runnnig Pytest while building docker compose up

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading