Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2.3.0
-----

- New ``qapp_args`` fixture which can be used to pass custom arguments to
``QApplication``.
Thanks `@The-Compiler`_ for the PR.

2.2.1
-----

Expand Down
8 changes: 8 additions & 0 deletions docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Reference

QtBot
-----

.. module:: pytestqt.qtbot
.. autoclass:: QtBot

Expand Down Expand Up @@ -32,3 +33,10 @@ Record

.. module:: pytestqt.logging
.. autoclass:: Record

qapp fixture
------------

.. module:: pytestqt.plugin
.. autofunction:: qapp
.. autofunction:: qapp_args
26 changes: 23 additions & 3 deletions pytestqt/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,36 @@
assert format_captured_exceptions


@pytest.fixture(scope='session')
def qapp_args():
"""
Fixture that provides QApplication arguments to use.

You can override this fixture to pass different arguments to
``QApplication``:

.. code-block:: python

@pytest.fixture(scope='session')
def qapp_args():
return ['--arg']
"""
return []


@pytest.yield_fixture(scope='session')
def qapp():
def qapp(qapp_args):
"""
fixture that instantiates the QApplication instance that will be used by
Fixture that instantiates the QApplication instance that will be used by
the tests.

You can use the ``qapp`` fixture in tests which require a ``QApplication``
to run, but where you don't need full ``qtbot`` functionality.
"""
app = qt_api.QApplication.instance()
if app is None:
global _qapp_instance
_qapp_instance = qt_api.QApplication([])
_qapp_instance = qt_api.QApplication(qapp_args)
yield _qapp_instance
else:
yield app # pragma: no cover
Expand Down
26 changes: 26 additions & 0 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import weakref
import pytest
from pytestqt.qt_compat import qt_api
Expand Down Expand Up @@ -376,3 +377,28 @@ def test_foo(qtbot):
monkeypatch.setenv('PYTEST_QT_API', 'piecute')
result = testdir.runpytest_subprocess()
result.stderr.fnmatch_lines(['* Invalid value for $PYTEST_QT_API: piecute'])


@pytest.mark.skipif(qt_api.pytest_qt_api in ['pyqt4', 'pyqt4v2', 'pyside'],
reason="QApplication.arguments() doesn't return custom arguments with Qt4 and Windows")
def test_qapp_args(testdir):
"""
Test customizing of QApplication arguments.
"""
testdir.makeconftest(
'''
import pytest

@pytest.fixture(scope='session')
def qapp_args():
return ['--test-arg']
'''
)
testdir.makepyfile('''
def test_args(qapp):
assert '--test-arg' in list(qapp.arguments())
''')
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines([
'*= 1 passed in *'
])