Skip to content

Commit

Permalink
Pass program name to QApplication contructor (#515)
Browse files Browse the repository at this point in the history
QCommandLineParser needs QApplication to be initialized with at list one
argument: The name of the program.

Closes #483
  • Loading branch information
hakonhagland authored Sep 25, 2023
1 parent da33817 commit 81c249d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ repos:
- id: rst
name: rst
entry: rst-lint --encoding utf-8
files: ^(CHANGELOG.rst|HOWTORELEASE.rst|README.rst)$
files: ^(HOWTORELEASE.rst|README.rst)$
language: python
additional_dependencies: [pygments, restructuredtext_lint]
18 changes: 18 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
UNRELEASED
-----------

- ``qapp`` now sets up the ``QApplication`` instance with a command line argument like this
``QApplication([prog_name])`` instead of using an empty list ``QApplication([])``.
Here ``prog_name`` is the name of the app which defaults to ``pytest-qt-app``, but can
be redefined in the ``pytest.ini`` file, see :ref:`qapp fixture<setting-qapp-name>`.
Alternatively, the arguments that will be passed to ``QApplication`` can be defined
explicitly using the ``qapp_args`` fixture. This means that the default behavior of
the ``qapp_args`` fixture is now also changed accordingly: it now returns the list
``[prog_name]`` instead of an empty list. Thanks to `@The-Compiler`_ (`#483`_) and
`@hakonhagland`_ (`#515`_).

.. _#515: https://github.com/pytest-dev/pytest-qt/pull/515
.. _#483: https://github.com/pytest-dev/pytest-qt/issues/483


4.2.0 (2022-10-25)
------------------

Expand Down Expand Up @@ -731,6 +748,7 @@ First working version.
.. _@fabioz: https://github.com/fabioz
.. _@fogo: https://github.com/fogo
.. _@gqmelo: https://github.com/gqmelo
.. _@hakonhagland: https://github.com/hakonhagland
.. _@itghisi: https://github.com/itghisi
.. _@jdreaver: https://github.com/jdreaver
.. _@mitya57: https://github.com/mitya57
Expand Down
2 changes: 2 additions & 0 deletions docs/qapplication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ If your tests require access to app-level functions, like
The ``qapp`` fixture will then use the returned class instead of the default
``QApplication`` from ``QtWidgets``.

.. _setting-qapp-name:

Setting a QApplication name
---------------------------

Expand Down
16 changes: 13 additions & 3 deletions src/pytestqt/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


@pytest.fixture(scope="session")
def qapp_args():
def qapp_args(pytestconfig):
"""
Fixture that provides QApplication arguments to use.
Expand All @@ -23,9 +23,19 @@ def qapp_args():
@pytest.fixture(scope="session")
def qapp_args():
return ["--arg"]
return ["prog_name", "--arg=foo"]
Note that it can only be overridden once at session scope.
It is not possible to override this per unit test since a QApplication
cannot be destroyed and recreated within the same app.
The default value is a list with one element which is determined the same
way as for ``QApplication.applicationName()``,
see :ref:`qapp fixture<setting-qapp-name>` for more information.
"""
return []
return [pytestconfig.getini("qt_qapp_name")]


@pytest.fixture(scope="session")
Expand Down
18 changes: 17 additions & 1 deletion tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def test_qapp_args(testdir):
@pytest.fixture(scope='session')
def qapp_args():
return ['--test-arg']
return ['prog_name', '--test-arg']
"""
)
testdir.makepyfile(
Expand All @@ -559,6 +559,22 @@ def test_args(qapp):
result.stdout.fnmatch_lines(["*= 1 passed in *"])


def test_qapp_args_default(testdir):
"""
Test QApplication default arguments.
"""

testdir.makepyfile(
"""
def test_args(qapp):
args = qapp.arguments()
assert args[0] == 'pytest-qt-qapp'
"""
)
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(["*= 1 passed in *"])


def test_importerror(monkeypatch):
def _fake_import(name, *args):
raise ModuleNotFoundError(f"Failed to import {name}")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_wait_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ class TestCallback:
@staticmethod
def get_signal_from_code(signaller, code):
"""Converts a code such as 'A1' to a signal (signaller.signal_args for example)."""
assert type(code) == str and len(code) == 2
assert type(code) is str and len(code) == 2
signal = signaller.signal_args if code[0] == "A" else signaller.signal_args_2
return signal

Expand Down

0 comments on commit 81c249d

Please sign in to comment.