Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: Failed to import test module (in docker). #32

Open
tkl5 opened this issue Sep 23, 2019 · 1 comment
Open

ImportError: Failed to import test module (in docker). #32

tkl5 opened this issue Sep 23, 2019 · 1 comment

Comments

@tkl5
Copy link

tkl5 commented Sep 23, 2019

Running tests works in Windows Subsystem for Linux but not in a Docker container. Is my general test layout correct? Looks like it's not able to find initdb. In other words, is it having conflict issues with the database created in Docker-compose.yml or does it have anything to do with that?

My test file testMyProject.py:

import unittest
import psycopg2
import testing.postgresql
from sqlalchemy import create_engine

class TestPostgresqlInteraction(unittest.TestCase):

    Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)

    with testing.postgresql.Postgresql() as postgresql:
        engine = create_engine(postgresql.url())
        dbConn = psycopg2.connect(**postgresql.dsn())
        cursor = dbConn.cursor()
        def setUp(self):
            self.postgresql = self.Postgresql()

        def tearDown(self):
            self.postgresql.stop()

        def testMethod(self)
            conn = psycopg2.connect(**self.postgresql.dsn())
            cursor = conn.cursor()
            # execute cursor and do tests... 
        
        def tearDownModule(self):
            self.Postgresql.clear_cache()

docker-compose.yml:

version: "3"

services:
    myproject:
        build:
            context: .
            dockerfile: ./myproject/Dockerfile
        depends_on:
            - database
        volumes:
            - ./myproject:/code
        stdin_open: true
        tty: true
    database:
        build:
            context: .
            dockerfile: ./database/Dockerfile
        environment:
            POSTGRES_USER_FILE: /secrets/dbUser.txt
            POSTGRES_PASSWORD_FILE: /secrets/dbPassword.txt
        ports:
            - "8765:5432"
        volumes:
            - otherdata:/var/lib/postgresql/data

I run docker-compose exec myproject python3 -m unittest
Traceback:

ERROR: tests.testMyProject (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests.testMyProject 
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/usr/local/lib/python3.7/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/code/tests/testMyproject.py", line 40, in <module>
    class TestPostgresqlInteraction(unittest.TestCase):
  File "/code/tests/testMyproject.py", line 46, in TestPostgresqlInteraction
    Postgresql = testing.postgresql.Postgresql(copy_data_from='/database/Dockerfile')
  File "/usr/local/lib/python3.7/site-packages/testing/common/database.py", line 92, in __init__
    self.initialize()
  File "/usr/local/lib/python3.7/site-packages/testing/postgresql.py", line 50, in initialize
    self.initdb = find_program('initdb', ['bin'])
  File "/usr/local/lib/python3.7/site-packages/testing/postgresql.py", line 144, in find_program
    raise RuntimeError("command not found: %s" % name)
RuntimeError: command not found: initdb
@tkl5 tkl5 changed the title ImportError: Failed to import test module in docker. ImportError: Failed to import test module (in docker). Sep 23, 2019
@panttojo
Copy link

panttojo commented Oct 22, 2022

It worked for me

Install postgres in the docker image:

RUN apt update && apt install -y postgresql postgresql-contrib

Create a user in the just before to run the tests

RUN useradd -u 8877 testing
USER testing

CMD pytest -s -v

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants