Skip to content

Commit

Permalink
Use session-scoped fixture to create QApplication
Browse files Browse the repository at this point in the history
Also, only create an instance if one doesn't exist yet,
to play nice with other code that may already have
instantiated a QApplication object

fixes #21
  • Loading branch information
nicoddemus committed Oct 8, 2014
1 parent 8d845a9 commit 7f5fbe2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,6 @@ Many thanks to:
- John David Reaver (`@jdreaver <https://github.com/jdreaver>`_);
- Benjamin Hedrich (`@bh <https://github.com/bh>`_);
- Benjamin Audren (`@baudren <https://github.com/baudren>`_);
- Fabio Zadrozny (`@fabioz <https://github.com/fabioz>`_);

.. _tox: http://tox.readthedocs.org
38 changes: 17 additions & 21 deletions pytestqt/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,25 +321,6 @@ def __exit__(self, type, value, traceback):
self.wait()


def pytest_configure(config):
"""
PyTest plugin API. Called before the start of each test session, used
to instantiate the qApplication object that will be used for the session.
:param config.Config config:
"""
qt_app_instance = QtGui.QApplication([])
config.qt_app_instance = qt_app_instance

def exit_qapp():
'''
Makes sure to exit the application after all tests finish running.
'''
qt_app_instance.exit()

config._cleanup.append(exit_qapp)


@contextmanager
def capture_exceptions():
"""
Expand Down Expand Up @@ -374,15 +355,30 @@ def format_captured_exceptions(exceptions):
return message


@pytest.yield_fixture(scope='session')
def qapp():
"""
fixture that instantiates the QApplication instance that will be used by
the tests.
"""
app = QtGui.QApplication.instance()
if app is None:
app = QtGui.QApplication([])
yield app
app.exit()
else:
yield app


@pytest.yield_fixture
def qtbot(request):
def qtbot(qapp):
"""
Fixture used to create a QtBot instance for using during testing.
Make sure to call addWidget for each top-level widget you create to ensure
that they are properly closed after the test ends.
"""
result = QtBot(request.config.qt_app_instance)
result = QtBot(qapp)
with capture_exceptions() as exceptions:
yield result

Expand Down

0 comments on commit 7f5fbe2

Please sign in to comment.