tk0miya / testing.postgresql

Apache License 2.0
290 stars 46 forks source link

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

Open tkl5 opened 4 years ago

tkl5 commented 4 years ago

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
panttojo commented 1 year ago

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