Я пытаюсь использовать pytest для запуска простого теста интеграции с postgresql db.Я должен использовать python 2.7 , поэтому testcontainers не подходит.
my conftest.py выглядит так:
import pytest
import docker # his is a official system docker package
@pytest.fixture(scope="session")
def finalizer_function(container_id):
container_id.stop()
@pytest.fixture(scope="session", autouse=True)
def db_init():
"""
This db_init() method will start up docker container
which will be used throughout all the tests and once
all the tests are completed, containers will be
automatically removed.
:return:
"""
client = docker.from_env()
con_id = client.containers.run(
image="postgres:9.6",
name="postgres",
detach=True,
environment={
'POSTGRES_DB': 'postgres',
'POSTGRES_PASSWORD': 'test',
'POSTGRES_USER': 'postgres',
},
remove=True,
ports={"5432/tcp": 5432}
)
# yield con_id.stop()
и вот очень простой тест, который я не могу запустить test_postgres.py :
import docker
import socket
import psycopg2
from postgres import Postgres
def test_connection_to_db(db_init):
"""
:param db_init:
:return:
"""
print(socket.gethostname())
dbo = Postgres(
default_dbuser='postgres',
default_db='postgres',
host='127.0.0.1',
password='test',
port='5432'
)
_superuser = 'userx'
dbo.create_user(user=_superuser, password='start123', superuser=True)
считаю, что from postgres import Postgres
- это мой пользовательский postgres модуль, который работает без проблем.
PYTHONPATH=lib pytest -s -vv tests/test_postgres.py
...
E OperationalError: server closed the connection unexpectedly
E This probably means the server terminated abnormally
E before or while processing the request.
Однако, когда я запускаю Docker-контейнер с помощью fixture db_init , и я пытаюсь подключиться к этому контейнеру с помощью psql Я могу подключиться к БД без проблем:
psql -h 127.0.0.1 -p 5432 -U postgres -d postgres
Password for user postgres:
psql (10.6, server 9.6.12)
Type "help" for help.
postgres=#
Пожалуйста, сообщите, я уверен, что это просто глупая ошибка.
Thx