Closed hakonhagland closed 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
:
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
or retrieves a previously stored 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
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
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.
it might not be possible to change the
qapp
fixture per test classIf 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
Note that you can simplify this a bit by using monkeypatch which is built into pytest
@The-Compiler Thanks for the tip!!
I would like to run each test class with a different set of command line arguments (
qapp_args
fixture), here is a minimal example:But in the second test class
Test2
overridingqapp_args()
does not work. It seems like it is using theqapp_args()
defined in theTest1
class instead. How can I solve this?