Skip to content
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

Provide message of expected exception when timeout happens #12

Open
abotalov opened this issue Sep 27, 2018 · 8 comments
Open

Provide message of expected exception when timeout happens #12

abotalov opened this issue Sep 27, 2018 · 8 comments

Comments

@abotalov
Copy link

Currently if both timeout_seconds and expected_exception are used, only information that timeout expired is printed.
It would be good to add exception message of expected exception to TimeoutExpired message.

@vmalloc
Copy link
Owner

vmalloc commented Sep 27, 2018

Can you please provide an example (not necessarily working) code snippet and what you expect to happen with it that currently does not happen? I'm not sure I'm following correctly...

@abotalov
Copy link
Author

abotalov commented Sep 27, 2018

Well, I'd like to perform assert until it succeeds or timeout happens.

def assertion()
  assert window.url() == 'some_string'
wait(assertion, timeout_seconds=10, expected_exceptions=AssertionError)

Similar feature exists in pytest-qt (https://pytest-qt.readthedocs.io/en/latest/wait_until.html). There, if timeout happens, an expected exception is raised instead of TimeoutError (https://github.com/pytest-dev/pytest-qt/blob/master/pytestqt/qtbot.py#L520). The benefit is that a user sees a message that helps to understand why timeout has happened.

waiting library may however still raise TimeoutExpired but a message can be improved to include a message of happened expected exception.

Currently I see only something like:

TimeoutExpired: Timeout of 10 seconds expired waiting for <function assertion at 0x1031bdca2>

@vmalloc
Copy link
Owner

vmalloc commented Sep 27, 2018

expected_exceptions in this case means "exceptions that are allowed to happen while polling", so they are ignored... I don't see the logic behind raising them on timeout....

@abotalov
Copy link
Author

abotalov commented Sep 27, 2018

The use case is described above. Could you propose a better way to do it?
Yes, these exceptions are allowed to happen until timeout. But once timeout happened, the message is useful for debugging.

@vmalloc
Copy link
Owner

vmalloc commented Sep 27, 2018

I see...

What if you had some adapter to achieve this, like:

waiting.wait(waiting.succeeds(func, expected_exceptions=(...)))

Then in this use case I can make the "default" message formatting consider this object type and print the last exception encountered on it

@abotalov
Copy link
Author

abotalov commented Sep 27, 2018

Ah, I just got that a solution I proposed before wouldn't fully work. As successful assertion returns None rather than True. But it could be worked around by returning True from a func.
Will the mechanism that you wrote about work by raising a specific exception that is catched in wait?

@vmalloc
Copy link
Owner

vmalloc commented Sep 27, 2018

I think it will be a specific subclass of a new class which waiting recognized, and which can be queried for the exact reason for not succeeding

@abotalov
Copy link
Author

abotalov commented Sep 27, 2018

The following appears to work fine for me:

def assertion()
  assert window.url() == 'some_string'
  return True
wait(assertion, timeout_seconds=10, expected_exceptions=AssertionError)

together with a code change like abotalov@0d43a52 appears to work.
The code change only outlines idea - it would be better to pass a message as a third parameter and to build a better message in TimeoutExpired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants