Skip to content

Commit 7f5fbe2

Browse files
committed
Use session-scoped fixture to create QApplication
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
1 parent 8d845a9 commit 7f5fbe2

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,6 @@ Many thanks to:
101101
- John David Reaver (`@jdreaver <https://github.com/jdreaver>`_);
102102
- Benjamin Hedrich (`@bh <https://github.com/bh>`_);
103103
- Benjamin Audren (`@baudren <https://github.com/baudren>`_);
104+
- Fabio Zadrozny (`@fabioz <https://github.com/fabioz>`_);
104105

105106
.. _tox: http://tox.readthedocs.org

pytestqt/plugin.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -321,25 +321,6 @@ def __exit__(self, type, value, traceback):
321321
self.wait()
322322

323323

324-
def pytest_configure(config):
325-
"""
326-
PyTest plugin API. Called before the start of each test session, used
327-
to instantiate the qApplication object that will be used for the session.
328-
329-
:param config.Config config:
330-
"""
331-
qt_app_instance = QtGui.QApplication([])
332-
config.qt_app_instance = qt_app_instance
333-
334-
def exit_qapp():
335-
'''
336-
Makes sure to exit the application after all tests finish running.
337-
'''
338-
qt_app_instance.exit()
339-
340-
config._cleanup.append(exit_qapp)
341-
342-
343324
@contextmanager
344325
def capture_exceptions():
345326
"""
@@ -374,15 +355,30 @@ def format_captured_exceptions(exceptions):
374355
return message
375356

376357

358+
@pytest.yield_fixture(scope='session')
359+
def qapp():
360+
"""
361+
fixture that instantiates the QApplication instance that will be used by
362+
the tests.
363+
"""
364+
app = QtGui.QApplication.instance()
365+
if app is None:
366+
app = QtGui.QApplication([])
367+
yield app
368+
app.exit()
369+
else:
370+
yield app
371+
372+
377373
@pytest.yield_fixture
378-
def qtbot(request):
374+
def qtbot(qapp):
379375
"""
380376
Fixture used to create a QtBot instance for using during testing.
381377
382378
Make sure to call addWidget for each top-level widget you create to ensure
383379
that they are properly closed after the test ends.
384380
"""
385-
result = QtBot(request.config.qt_app_instance)
381+
result = QtBot(qapp)
386382
with capture_exceptions() as exceptions:
387383
yield result
388384

0 commit comments

Comments
 (0)