-
-
Notifications
You must be signed in to change notification settings - Fork 69
assertSignal #92
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
Comments
For the API, wouldn't it be better to return a list of lists? The outer list containing |
That's basically how emissions = []
for elem in spy:
args = [extract_from_variant(arg) for arg in elem]
emissions.append(args) (where |
Hi guys!
Hmm since signals are asynchronous by nature, not waiting for the signal seems to be error prone. For example, on linux widget events are asynchronous due to the client/server nature of the X window design. Triggering a signal sometimes doesn't mean it will be processed right away, depending on the signal or the system. If I understand correctly, this is very similar to what is possible already with with qtbot.waitSignal(signal, count=2) as blocker:
foo.do_stuff()
assert blocker.args[0] == ("first signal args",)
assert blocker.args[1] == ("second signal args",) |
That might indeed be better for the "make sure a signal was received" usecase - that's basically #64 plus the The more important thing, IMHO, (and my original usecase) is a way to make sure a signal is not emitted. Consider something like this (from the qutebrowser testsuite): def test_get_version_success(qtbot):
http_stub = HTTPGetStub(success=True)
client = autoupdate.PyPIVersionClient(client=http_stub)
# Use a spy to inspect the signal
error_spy = QSignalSpy(client.error)
with qtbot.waitSignal(client.success, raising=True):
client.get_version('test')
assert len(error_spy) == 0
assert http_stub.url == QUrl('https://pypi.python.org/pypi/test/json') or this: def test_set_cookies_never_accept(config_stub):
"""Test setCookiesFromUrl when cookies are not accepted."""
config_stub.data = CONFIG_NEVER_COOKIES
ram_jar = cookies.RAMCookieJar()
changed_signal_spy = QSignalSpy(ram_jar.changed)
assert not ram_jar.setCookiesFromUrl('test', 'test')
assert not changed_signal_spy here I know I'd get the signal during So maybe it should be something like this instead, like my original thought: with qtbot.assertNotEmitted(ram_jar.changed):
assert not ram_jar.setCookiesFromUrl('test', 'test') |
Hmm I see, thanks. 😄 How would the def assertNotEmitted(self, signal, max_wait=1.0):
"""
Asserts that the given signal was not emitted within the given ``max_wait`` seconds has
elapsed.
""" ? (BTW, thinking about it a little more, I'm not sure Applying it to your examples: def test_get_version_success(qtbot):
http_stub = HTTPGetStub(success=True)
client = autoupdate.PyPIVersionClient(client=http_stub)
with qtbot.waitSignal(client.success, raising=True), qtbot.assertNotEmitted(client.error):
client.get_version('test')
assert http_stub.url == QUrl('https://pypi.python.org/pypi/test/json') def test_set_cookies_never_accept(config_stub, qtbot):
"""Test setCookiesFromUrl when cookies are not accepted."""
config_stub.data = CONFIG_NEVER_COOKIES
ram_jar = cookies.RAMCookieJar()
with qtbot.assertNotEmitted(ram_jar.changed):
assert not ram_jar.setCookiesFromUrl('test', 'test') The first example seems OK, but to be honest I'm not sure if the second example is much of an improvement. What do you think? |
Sorry for not getting back to this earlier! I think the second example is an improvement as well, I like to see things indented in context managers 😉 I'll probably work on this soon(tm) too. |
I'd like to add a
qtbot.assertSignal
to pytest-qt/cc @acogneau
Motivation
Something I need to do regularily is to make sure a given code snippet calls a signal, or doesn't - but I don't want to wait for the signal.
The current way to do that works, but is unpythonic:
API
After some thinking, I think a context manager with this signature would be best:
signal
: The signal it should listen tocount
: EitherNone
(in which case it expects the signal to be emitted >= 1 times), or an int. By settingcount
to0
, it can be ensured the signal is not emitted.QSignalSpy
which can be used to check the signal arguments.Examples
Alternatives
I came up with some other ideas while thinking about this, but I think all of them are worse:
qtbot.assertSignal(signal, emitted=True)
: Doesn't give you the possibility to check a signal was emitted exactly once, and isn't better in any way.qtbot.assertSignal(signal, calls=[("signal arg 1", "signal arg 2")])
: Too complicated, and can be done easier by checking theQSignalSpy
qtbot.assertNotEmitted(signal)
- less functionality, and I think passingcount=0
is okay if you want that (that was the usecase I had in mind originally)waitSignal
- it's complex enough already, andqtbot.waitSignal(signal, do_not_actually_wait=True)
just sounds wrong 😉with qtbot.assertSignal(foo), qtbot.assertSignal(bar):
instead - that isn't an issue because it doesn't wait for the signal.The text was updated successfully, but these errors were encountered: