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

How to reset qapp_args for each test class? #514

Closed
hakonhagland opened this issue Aug 28, 2023 · 6 comments
Closed

How to reset qapp_args for each test class? #514

hakonhagland opened this issue Aug 28, 2023 · 6 comments

Comments

@hakonhagland
Copy link
Contributor

hakonhagland commented Aug 28, 2023

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
Copy link
Contributor Author

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
Copy link
Contributor Author

hakonhagland commented Aug 28, 2023

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

_qapp_instance = qapp_cls(qapp_args)

or retrieves a previously stored instance

app = qt_api.QtWidgets.QApplication.instance()

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
Copy link
Contributor Author

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
Copy link
Member

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.

@nicoddemus nicoddemus closed this as not planned Won't fix, can't repro, duplicate, stale Aug 29, 2023
@The-Compiler
Copy link
Member

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
Copy link
Contributor Author

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

@The-Compiler Thanks for the tip!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants