diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b772c9ed..500190d5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 ----- diff --git a/docs/reference.rst b/docs/reference.rst index 58a78edf..e1a5b09d 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -3,6 +3,7 @@ Reference QtBot ----- + .. module:: pytestqt.qtbot .. autoclass:: QtBot @@ -32,3 +33,10 @@ Record .. module:: pytestqt.logging .. autoclass:: Record + +qapp fixture +------------ + +.. module:: pytestqt.plugin +.. autofunction:: qapp +.. autofunction:: qapp_args diff --git a/pytestqt/plugin.py b/pytestqt/plugin.py index 6fa179dc..c8721566 100644 --- a/pytestqt/plugin.py +++ b/pytestqt/plugin.py @@ -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 diff --git a/tests/test_basics.py b/tests/test_basics.py index e0af5613..6bc72cad 100644 --- a/tests/test_basics.py +++ b/tests/test_basics.py @@ -1,4 +1,5 @@ import os +import sys import weakref import pytest from pytestqt.qt_compat import qt_api @@ -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 *' + ])