Comments (6)
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
from pytest-qt.
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
:
from pytest-qt.
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
pytest-qt/src/pytestqt/plugin.py
Line 66 in 0518ca0
or retrieves a previously stored instance
pytest-qt/src/pytestqt/plugin.py
Line 63 in 0518ca0
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.
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.
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.
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)
- Segfault on Ubuntu 22.04 HOT 4
- [BUG] raise TypeError("Need to pass a QWidget to addWidget!") HOT 2
- Problem when testing QHttpServer HOT 4
- Setup for github actions on linux, macos, and windows HOT 3
- Dialog test fail in GitHub Action HOT 1
- How to use qapp_args() ? HOT 2
- macos 14.0 (sonoma) doesn't pass control to qt HOT 16
- Tests using qtbot hang indefinitely on Mac OS Sonoma HOT 1
- How to test the following code...? HOT 1
- pytest-qt fails when snake_case feature is used in a test HOT 1
- How to use QAbstractItemModelTester? HOT 8
- test_exceptions_dont_leak fails with Python 3.12 HOT 7
- Intermittently missing property of object under test HOT 3
- More verbose tracebacks on exceptions in event loop HOT 1
- QDBusConnection.systemBus() leaks thread after qapp teardown HOT 3
- Clarification about methods like keyEvent, etc.? HOT 1
- Segfault on Linux with PyQt6 HOT 2
- "RuntimeError: Failed to disconnect" with `PySide6==6.7.0` HOT 19
- Crash on macOs Sonoma HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytest-qt.