0

I have fixture that launches sub-process during testing.
It causes some issues, because, apparently process is killed prematurely.

from multiprocessing import Process
from uvicorn import run
from main import app

# -- snip --
@pytest.fixture(scope="session")
def external_client():
    """Launch application as an independent process"""
    config = dict(app=app, host="127.0.0.1", port=7001, workers=1)
    p = Process(target=run, kwargs=config)
    p.start()
    yield
    p.kill()
# -- snip --

This process creates postgresql connections. Which are used in other fixtures for teardown/ cleaning database. (yeah...that sounds not right on paper)

When I extended tests I faced to errors like below, which occured inconsitenly.
sqlalchemy.exc.DatabaseError: (psycopg2.DatabaseError) error with status PGRES_TUPLES_OK and no message from the libpq

I tried to follow answer here, and dispose engines explicitly. But it did not help. I am currently tyring to solve this, and one of my ideas is to kill process only in last fixture, but I do not know how to order them(only if manually trying to track fixture chain and add to last triggered fixture)
So I would like to to something like that:

# -- snip --
@pytest.fixture(scope="session")
def external_client():
    """Launch application as an independent process"""
    config = dict(app=app, host="127.0.0.1", port=7001, workers=1)
    p = Process(target=run, kwargs=config)
    p.start()
    yield p

@pytest.fixture()
def kill_process(external_client):
    yield
    # I want that this code as the last triggered code of all test fixtures.
    external_client.kill()
# -- snip --

Is there a way to do it? Or better way to avoid closing process prematurely?

Archirk
  • 427
  • 7
  • 25
  • if the only reason you are creating a process is for a Postgres connection, i would recommend using a fixture an using [testing.postgresql](https://pypi.org/project/testing.postgresql/) – Shod Apr 15 '22 at 16:13
  • No, I launch apllication which use psql connection – Archirk Apr 16 '22 at 17:34

0 Answers0