[TIP] pytest fixtures started after unittest.TestCase.setUpClass

Dan Nealschneider dan.nealschneider at schrodinger.com
Thu Jan 4 13:27:53 PST 2018

We use session-scoped pytest fixtures to create the test environment that
we want, and to help reduce certain types of problems in tests. However,
I've noticed that pytest fixtures are run after
unittest.TestCase.setUpClass. This causes a problem when a setUpClass
relies on our global setup, and happens to be the first test executed.

For instance:

import r_group_analysis_dir.sar as sar
import pytest

@pytest.fixture(scope='session', autouse=True)
def start_qapp():
     global _QAPP
     from schrodinger.Qt import QtCore, QtWidgets
     _QAPP = QtWidgets.QApplication.instance()
    if not _QAPP:
        _QAPP = QtWidgets.QApplication([])
    return _QAPP

class TestSar(unittest.TestCase):
     def setUpClass(cls):
         cls.panel = sar.sarPanel()

It seems to me that unittest's setUpClass should be behave as a
class-scoped fixture, and therefor be run after the session-scoped
start_qapp. I understand that this is not how the pytest's unittest
execution structure works, however. My workaround is triggering start_qapp
in the pytest_runtest_setup hook so that it will always be available, even
under xdist.

I guess I have two questions:
1. How should I ensure that start_qapp is run before any test or test setup?
2. Does this seem like a bug/RFE that I should report to pytest?

- dan nealschneider

(né wandschneider)

Senior Developer
Schr*ö*dinger, Inc
Portland, OR
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20180104/6799e5c8/attachment.htm>

More information about the testing-in-python mailing list