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

Testing psygnal with pytest-qt #319

Closed
jacopoabramo opened this issue Jul 23, 2024 · 7 comments
Closed

Testing psygnal with pytest-qt #319

jacopoabramo opened this issue Jul 23, 2024 · 7 comments

Comments

@jacopoabramo
Copy link

  • psygnal version: 0.11.1
  • Python version: 3.10
  • Operating System: Windows

For a project I'm working on, I'm using psygnal as signaling mechanism within my application. I would like to run unit tests on some internal parts of my project to ensure everything works properly. Is it possible to use pytest-qt together with psygnal to run unit tests and ensure that signals are correctly emitted?

@jacopoabramo
Copy link
Author

Just answered myself by checking the local unit tests and the answer seems yes. Closing.

@tlambert03
Copy link
Member

Yep! As you found, it works. I'll also note the long standing #46 "todo"... but that hasn't been urgent for me since I too just use pytest-qt in most cases

@tlambert03
Copy link
Member

Another test pattern for synchronous applications is to connect your signal to a Mock(), perform your action, and then use mock.assert_called...

@jacopoabramo
Copy link
Author

Another test pattern for synchronous applications is to connect your signal to a Mock(), perform your action, and then use mock.assert_called...

Do you have an example I can look at?

@tlambert03
Copy link
Member

yeah, that pattern is used all throughout the tests in this repo, for example:

def test_basic_signal():
"""standard Qt usage, as class attribute"""
emitter = Emitter()
mock = MagicMock()
emitter.one_int.connect(mock)
emitter.one_int.emit(1)
mock.assert_called_once_with(1)
mock.reset_mock()
# calling directly also works
emitter.one_int(1)
mock.assert_called_once_with(1)

(note, it really doesn't need to be MagicMock there... a simple Mock() would also work fine)

@jacopoabramo
Copy link
Author

Ah so you're mixing both unittest and pytest together. Clever, I might pick up on that since in the end I'm also using mock objects to test my logic. Thanks!

@tlambert03
Copy link
Member

yeah, one doesn't need to be using unittest as a test runner to take advantage of patch and mock. Those are just generally useful objects regardless of your test runner

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

No branches or pull requests

2 participants