Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass program name to QApplication contructor #515

Merged
merged 5 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
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")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in #483, this should be sys.argv[0] instead. The QApplication.applicationName() isn't necessarily the same thing.



@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