Description
I'm seeing an issue with pytest-qt (4.4.0) and I am not sure if it is a thread issue on my end or an issue with pytest-qt. Most of the time, the test will pass, but this issue will cause the test to fail about 10% to 20% of the time. Below is code that reproduces the issue:
from PySide6.QtCore import QThread, QObject, Signal
import traceback
class MyRelay(QObject):
sig_to_relay = Signal()
class MyWorker(QObject):
my_signal = Signal()
def __init__(self, relay):
super().__init__()
self._relay = relay
self._relay.setParent(self)
self._relay.sig_to_relay.connect(self.my_signal)
class MyThread(QObject):
def __init__(self):
self._workers = None
self._thread = QThread()
def add_worker(self, worker):
self._workers = worker
def start(self):
self._workers.moveToThread(self._thread)
self._thread.start()
class TestThread:
def test_error_1(self, qtbot):
relay = MyRelay()
mw = MyWorker(relay)
my_thread = MyThread()
my_thread.add_worker(mw)
my_thread.start()
try:
with qtbot.waitSignal(
mw.my_signal, timeout=500) as blocker:
relay.sig_to_relay.emit()
print(blocker)
except Exception as e:
traceback.print_exc()
raise e
my_thread._thread.quit()
my_thread._thread.wait()
In this example, the try
block is to catch the exception before it fails the test so that I can check the traceback. During failures, the traceback is:
Traceback (most recent call last):
File "~python_work/pytest_qt_qthread/test_pytest_qt.py", line 41, in test_warning_message
with qtbot.waitSignal(
File "~python_work/pytest_qt_qthread/.venv/lib/python3.12/site-packages/pytestqt/wait_signal.py", line 151, in __exit__
self.wait()
File "~python_work/pytest_qt_qthread/.venv/lib/python3.12/site-packages/pytestqt/wait_signal.py", line 48, in wait
self._timer.start()
^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'start'
It appears that the a timer in pytest-qt is getting through the timer is None
check but then is None
when self._timer.start()
is called a few lines later.
Also, on every run of the test (in debug mode), the following message is reported QObject::killTimer: Timers cannot be stopped from another thread
, which may be related.
Any idea on what may be wrong here?
Thanks.
(edited by @The-Compiler to add syntax highlighting)