What to do when a py.test hangs silently? What to do when a py.test hangs silently? sqlite sqlite

What to do when a py.test hangs silently?


I ran into the same SQLite/Postgres problem with Flask and SQLAlchemy, similar to Gordon Fierce. However, my solution was different. Postgres is strict about table locks and connections, so explicitly closing the session connection on teardown solved the problem for me.

My working code:

@pytest.yield_fixture(scope='function')def db(app):    # app is an instance of a flask app, _db a SQLAlchemy DB    _db.app = app    with app.app_context():        _db.create_all()    yield _db    # Explicitly close DB connection    _db.session.close()    _db.drop_all()

Reference: SQLAlchemy


To answer the question "How would I go about debugging something like that?"

  1. Run with py.test -m trace --trace to get trace of python calls.

  2. One option (useful for any stuck unix binary) is to attach to process using strace -p <PID>. See what system call it might be stuck on or loop of system calls. e.g. stuck calling gettimeofday

  3. For more verbose py.test output install pytest-sugar. pip install pytest-sugar And run test with pytest.py --verbose . . .https://pypi.python.org/pypi/pytest-sugar


I had a similar problem with pytest and Postgresql while testing a Flask app that used SQLAlchemy. It seems pytest has a hard time running a teardown using its request.addfinalizer method with Postgresql.

Previously I had:

@pytest.fixturedef db(app, request):    def teardown():        _db.drop_all()    _db.app = app    _db.create_all()    request.addfinalizer(teardown)    return _db

( _db is an instance of SQLAlchemy I import from extensions.py )But if I drop the database every time the database fixture is called:

@pytest.fixturedef db(app, request):    _db.app = app    _db.drop_all()    _db.create_all()    return _db

Then pytest won't hang after your first test.