Giter Club home page Giter Club logo

Comments (6)

The-Compiler avatar The-Compiler commented on September 7, 2024 1

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

from pytest-qt.

hakonhagland avatar hakonhagland commented on September 7, 2024

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

from pytest-qt.

hakonhagland avatar hakonhagland commented on September 7, 2024

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

from pytest-qt.

hakonhagland avatar hakonhagland commented on September 7, 2024

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

from pytest-qt.

nicoddemus avatar nicoddemus commented on September 7, 2024

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.

from pytest-qt.

hakonhagland avatar hakonhagland commented on September 7, 2024

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

@The-Compiler Thanks for the tip!!

from pytest-qt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.