pytest-dev / pytest-qt

pytest plugin for Qt (PyQt5/PyQt6 and PySide2/PySide6) application testing
https://pytest-qt.readthedocs.io
MIT License
409 stars 70 forks source link

How to reset qapp_args for each test class? #514

Closed hakonhagland closed 1 year ago

hakonhagland commented 1 year ago

I would like to run each test class with a different set of command line arguments (qapp_args fixture), here is a minimal example:

import sys
import pytest
from PyQt6.QtCore import QCommandLineParser
from PyQt6.QtWidgets import QApplication

def get_positional_args(app):
    app.setApplicationName("MyApp")
    app.setApplicationVersion("0.1")
    parser = QCommandLineParser()
    parser.addHelpOption()
    parser.addVersionOption()
    parser.addPositionalArgument("my_pos_arg1", "description of my_pos_arg1")
    parser.addPositionalArgument("my_pos_arg2", "description of my_pos_arg2")
    parser.process(app)
    arguments = parser.positionalArguments()
    return arguments

if __name__ == '__main__':
    app = QApplication(sys.argv)
    args = get_positional_args(app)
    print(args)

class Test1:
    @pytest.fixture(scope="session")
    def qapp_args(self):
        return ["prog_name", "a1"]

    def test_pos_args(self, qapp):
        args = get_positional_args(qapp)
        assert len(args) == 1

class Test2:
    @pytest.fixture(scope="session")
    def qapp_args(self):
        return ["prog_name", "a1", "a2"]

    def test_pos_args(self, qapp):
        args = get_positional_args(qapp)
        assert len(args) == 2

But in the second test class Test2 overriding qapp_args() does not work. It seems like it is using the qapp_args() defined in the Test1 class instead. How can I solve this?

image

hakonhagland commented 1 year ago

It seems like it is using the qapp_args() defined in the Test1 class instead.

I think the fixtures should have scope class and not session? But if I change the scope to class I get another error: ScopeMismatch: You tried to access the class scoped fixture qapp_args with a session scoped request object:

image

hakonhagland commented 1 year ago

ScopeMismatch: You tried to access the class scoped fixture qapp_args with a session scoped request object

I think it might not be possible to change the qapp fixture per test class, since the fixture creates a QApplication instance

https://github.com/pytest-dev/pytest-qt/blob/0518ca0cdf489175385028c12546942700def481/src/pytestqt/plugin.py#L66

or retrieves a previously stored instance

https://github.com/pytest-dev/pytest-qt/blob/0518ca0cdf489175385028c12546942700def481/src/pytestqt/plugin.py#L63

As I understand, the command line arguments are passed to the QApplication constructor and cannot be modified thereafter, also it not possible to destroy a QApplication object within an app and create a new one (passing different command line parameters to the constructor), see https://forum.qt.io/topic/110418/how-to-destroy-a-singleton-and-then-create-a-new-one/11

hakonhagland commented 1 year ago

it might not be possible to change the qapp fixture per test class

If this is true, one should try do this differently. For example using the mocker fixture:

#  ... code above this line is the same as before
pytest.fixture(scope="session")
def qapp_args():
    # Needed this to avoid returning the emtpy "[]" default value which will cause QCommandLineParser.process(app)
    # to abort with error: argument list cannot be empty, it should contain at least the executable name
    return ["prog_name"]

class Test1:
    def test_pos_args(self, qapp, mocker):
        cmd_line_args = ["a1"]
        mocker.patch(
            __name__ + ".QCommandLineParser.positionalArguments",
            return_value=cmd_line_args,
        )
        args = get_positional_args(qapp)
        assert len(args) == 1

class Test2:
    def test_pos_args(self, qapp, mocker):
        cmd_line_args = ["a1", "a2"]
        mocker.patch(
            __name__ + ".QCommandLineParser.positionalArguments",
            return_value=cmd_line_args,
        )
        args = get_positional_args(qapp)
        assert len(args) == 2
nicoddemus commented 1 year ago

You got the gist of it: QApplication can only be instantiated once per process, after that you cannot really change its arguments.

If you need to test this somehow, you will probably need to restructure your code a bit and use mock.

Closing for now, I assume there is nothing actionable for pytest-qt here.

The-Compiler commented 1 year ago

it might not be possible to change the qapp fixture per test class

If this is true, one should try do this differently. For example using the mocker fixture:

Note that you can simplify this a bit by using monkeypatch which is built into pytest:

class Test1:
    def test_pos_args(self, qapp, monkeypatch):
        monkeypatch.setattr(QCommandLineParser, "positionalArguments", lambda: ["a1"])
        args = get_positional_args(qapp)
        assert len(args) == 1

class Test2:
    def test_pos_args(self, qapp, monkeypatch):
        monkeypatch.setattr(
            QCommandLineParser,
            "positionalArguments",
            lambda: ["a1"], ["a2"]
        )
        args = get_positional_args(qapp)
        assert len(args) == 2
hakonhagland commented 1 year ago

Note that you can simplify this a bit by using monkeypatch which is built into pytest

@The-Compiler Thanks for the tip!!