Skip to content

Commit 81c249d

Browse files
authored
Pass program name to QApplication contructor (#515)
QCommandLineParser needs QApplication to be initialized with at list one argument: The name of the program. Closes #483
1 parent da33817 commit 81c249d

File tree

6 files changed

+52
-6
lines changed

6 files changed

+52
-6
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ repos:
4343
- id: rst
4444
name: rst
4545
entry: rst-lint --encoding utf-8
46-
files: ^(CHANGELOG.rst|HOWTORELEASE.rst|README.rst)$
46+
files: ^(HOWTORELEASE.rst|README.rst)$
4747
language: python
4848
additional_dependencies: [pygments, restructuredtext_lint]

CHANGELOG.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
UNRELEASED
2+
-----------
3+
4+
- ``qapp`` now sets up the ``QApplication`` instance with a command line argument like this
5+
``QApplication([prog_name])`` instead of using an empty list ``QApplication([])``.
6+
Here ``prog_name`` is the name of the app which defaults to ``pytest-qt-app``, but can
7+
be redefined in the ``pytest.ini`` file, see :ref:`qapp fixture<setting-qapp-name>`.
8+
Alternatively, the arguments that will be passed to ``QApplication`` can be defined
9+
explicitly using the ``qapp_args`` fixture. This means that the default behavior of
10+
the ``qapp_args`` fixture is now also changed accordingly: it now returns the list
11+
``[prog_name]`` instead of an empty list. Thanks to `@The-Compiler`_ (`#483`_) and
12+
`@hakonhagland`_ (`#515`_).
13+
14+
.. _#515: https://github.com/pytest-dev/pytest-qt/pull/515
15+
.. _#483: https://github.com/pytest-dev/pytest-qt/issues/483
16+
17+
118
4.2.0 (2022-10-25)
219
------------------
320

@@ -731,6 +748,7 @@ First working version.
731748
.. _@fabioz: https://github.com/fabioz
732749
.. _@fogo: https://github.com/fogo
733750
.. _@gqmelo: https://github.com/gqmelo
751+
.. _@hakonhagland: https://github.com/hakonhagland
734752
.. _@itghisi: https://github.com/itghisi
735753
.. _@jdreaver: https://github.com/jdreaver
736754
.. _@mitya57: https://github.com/mitya57

docs/qapplication.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ If your tests require access to app-level functions, like
7575
The ``qapp`` fixture will then use the returned class instead of the default
7676
``QApplication`` from ``QtWidgets``.
7777

78+
.. _setting-qapp-name:
79+
7880
Setting a QApplication name
7981
---------------------------
8082

src/pytestqt/plugin.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
@pytest.fixture(scope="session")
15-
def qapp_args():
15+
def qapp_args(pytestconfig):
1616
"""
1717
Fixture that provides QApplication arguments to use.
1818
@@ -23,9 +23,19 @@ def qapp_args():
2323
2424
@pytest.fixture(scope="session")
2525
def qapp_args():
26-
return ["--arg"]
26+
return ["prog_name", "--arg=foo"]
27+
28+
29+
Note that it can only be overridden once at session scope.
30+
It is not possible to override this per unit test since a QApplication
31+
cannot be destroyed and recreated within the same app.
32+
33+
The default value is a list with one element which is determined the same
34+
way as for ``QApplication.applicationName()``,
35+
see :ref:`qapp fixture<setting-qapp-name>` for more information.
36+
2737
"""
28-
return []
38+
return [pytestconfig.getini("qt_qapp_name")]
2939

3040

3141
@pytest.fixture(scope="session")

tests/test_basics.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ def test_qapp_args(testdir):
546546
547547
@pytest.fixture(scope='session')
548548
def qapp_args():
549-
return ['--test-arg']
549+
return ['prog_name', '--test-arg']
550550
"""
551551
)
552552
testdir.makepyfile(
@@ -559,6 +559,22 @@ def test_args(qapp):
559559
result.stdout.fnmatch_lines(["*= 1 passed in *"])
560560

561561

562+
def test_qapp_args_default(testdir):
563+
"""
564+
Test QApplication default arguments.
565+
"""
566+
567+
testdir.makepyfile(
568+
"""
569+
def test_args(qapp):
570+
args = qapp.arguments()
571+
assert args[0] == 'pytest-qt-qapp'
572+
"""
573+
)
574+
result = testdir.runpytest_subprocess()
575+
result.stdout.fnmatch_lines(["*= 1 passed in *"])
576+
577+
562578
def test_importerror(monkeypatch):
563579
def _fake_import(name, *args):
564580
raise ModuleNotFoundError(f"Failed to import {name}")

tests/test_wait_signal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ class TestCallback:
617617
@staticmethod
618618
def get_signal_from_code(signaller, code):
619619
"""Converts a code such as 'A1' to a signal (signaller.signal_args for example)."""
620-
assert type(code) == str and len(code) == 2
620+
assert type(code) is str and len(code) == 2
621621
signal = signaller.signal_args if code[0] == "A" else signaller.signal_args_2
622622
return signal
623623

0 commit comments

Comments
 (0)