0

I get this error while running pytest inside the container. What can be done to fix this error? I am able to run the same package from pycharm and from my Windows shell. That is after I set the PYTHONPATH.

ImportError while importing test module '/testsuite/comments_posts_api/test_put_comments_post_byid.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
comments_posts_api/test_put_comments_post_byid.py:6: in <module>
    from service_layer.post import Post
E   ModuleNotFoundError: No module named 'service_layer'
# Pull base image
From ubuntu:18.04

# Install dependencies
RUN apt-get update -y
RUN apt-get install -y build-essential python3.6 python3.6-dev python3-pip python3.6-venv
RUN apt-get install -y vim
RUN python3.6 -m pip install pip --upgrade
RUN pip3 install pylint
RUN pip3 install requests
RUN pip3 install pytest pytest-cache

#install dos2unix
RUN apt-get update && \
    apt-get install dos2unix && \
    apt-get clean

# Create working directory
RUN mkdir /testsuite

# Copy project
COPY comments_categories_api  /testsuite/comments_categories_api/
COPY comments_posts_api  /testsuite/comments_posts_api/
COPY service_layer /testsuite/service_layer/
RUN chmod -R a+rwX testsuite/
# Set working directory

WORKDIR /testsuite
# Set Python version
RUN echo alias python='/usr/bin/python3' >> ~/.bashrc
# RUN echo cd testsuite/ >> ~/.bashrc

COPY ./docker-entrypoint.sh /testsuite/docker-entrypoint.sh
RUN dos2unix /testsuite/docker-entrypoint.sh && apt-get --purge remove -y dos2unix
# Define ENTRYPOINT
RUN ["chmod", "+x", "/testsuite/docker-entrypoint.sh"]
ENTRYPOINT ["sh", "/testsuite/docker-entrypoint.sh"] 
In docker-entrypoint.sh file
#!/bin/bash
pytest -v

$@
RSSregex
  • 179
  • 7
  • Can you run a shell into the container (you might have to remove the entrypoint) and make sure that the structure under /testsuite is what you expect it to be? If service_layer is a directory you might have to put another "/" after it in case you only want the files inside copied – Mihai May 06 '19 at 18:54
  • The directories are created properly. I ran pytest from the container prompt.This seems be some sort of a path issues. If you move the dependencies in the same folder, pytest works fine inside the container. – RSSregex May 06 '19 at 19:15
  • Maybe omit the /testsuite/ in the entrypoint. You are already in that directory because of WORKDIR. Maybe then the paths are picked up correctly – Mihai May 06 '19 at 20:20
  • ENTRYPOINT ["sh", "/docker-entrypoint.sh"] ? Like this. Got this error. PS C:\Users\test> docker run -it myapp:v0.1 /bin/bash sh: 0: Can't open /docker-entrypoint.sh – RSSregex May 06 '19 at 20:45
  • ENTRYPOINT ["sh", "docker-entrypoint.sh"] . This works as far as making it to the directory. But the pytest issue is not resolved. – RSSregex May 06 '19 at 20:47
  • What is the command in `docker-entrypoint` that runs the tests? Can you share the script contents? Also, check out answers to [this question](https://stackoverflow.com/questions/10253826). – hoefling May 06 '19 at 21:12
  • Adding this in my test folder solved. it. I got it from one of the postings here. __init__.py file import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) – RSSregex May 06 '19 at 21:57

0 Answers0