Skip to content

Commit

Permalink
RFC: Set application name
Browse files Browse the repository at this point in the history
When an application name is set, Qt uses it internally for e.g. getting
data/config locations:

    >>> QStandardPaths.writableLocation(QStandardPaths.DataLocation)
    '/home/user/.local/share'
    >>> qapp.setApplicationName('pytest-qt-qapp')
    >>> QStandardPaths.writableLocation(QStandardPaths.DataLocation)
    '/home/user/.local/share/pytest-qt-qapp'

That way, when an application accidentally writes into a data/cache/config
directory, it will be easier to find out (rather than those files just landing
somewhere outside an application directory).
  • Loading branch information
The-Compiler committed May 8, 2020
1 parent e808006 commit fc1faad
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
12 changes: 12 additions & 0 deletions docs/qapplication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,15 @@ If your tests require access to app-level functions, like
@pytest.fixture(scope="session")
def qapp():
yield CustomQApplication([])
Setting a QApplication name
---------------------------

By default, pytest-qt set's the ``QApplication.applicationName()`` to
``pytest-qt-qapp``. To use a custom name, you can set the ``qt_qapp_name``
option in ``pytest.ini``:

.. code-block:: ini
[pytest]
qt_qapp_name = frobnicate-tests
7 changes: 6 additions & 1 deletion src/pytestqt/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def qapp_args():


@pytest.fixture(scope="session")
def qapp(qapp_args):
def qapp(qapp_args, pytestconfig):
"""
Fixture that instantiates the QApplication instance that will be used by
the tests.
Expand All @@ -55,6 +55,8 @@ def qapp(qapp_args):
if app is None:
global _qapp_instance
_qapp_instance = qt_api.QApplication(qapp_args)
name = pytestconfig.getini("qt_qapp_name")
_qapp_instance.setApplicationName(name)
return _qapp_instance
else:
return app # pragma: no cover
Expand Down Expand Up @@ -109,6 +111,9 @@ def pytest_addoption(parser):
"qt_wait_signal_raising",
"Default value for the raising parameter of qtbot.waitSignal (legacy alias)",
)
parser.addini(
"qt_qapp_name", "The Qt application name to use", default="pytest-qt-qapp"
)

default_log_fail = QtLoggingPlugin.LOG_FAIL_OPTIONS[0]
parser.addini(
Expand Down
21 changes: 21 additions & 0 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ def test_basics(qtbot):
assert widget.windowTitle() == "W1"


def test_qapp_default_name(qapp):
assert qapp.applicationName() == "pytest-qt-qapp"


def test_qapp_name(testdir):
testdir.makepyfile(
"""
def test_name(qapp):
assert qapp.applicationName() == "frobnicator"
"""
)
testdir.makeini(
"""
[pytest]
qt_qapp_name = frobnicator
"""
)
res = testdir.runpytest_subprocess()
res.stdout.fnmatch_lines("*1 passed*")


def test_key_events(qtbot, event_recorder):
"""
Basic key events test.
Expand Down

0 comments on commit fc1faad

Please sign in to comment.