pyapp-kit / psygnal

Python observer pattern (callback/event system). Modeled after Qt Signals & Slots (but independent of Qt)
https://psygnal.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
84 stars 13 forks source link

Testing psygnal with pytest-qt #319

Closed jacopoabramo closed 3 months ago

jacopoabramo commented 3 months ago

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 commented 3 months ago

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

tlambert03 commented 3 months ago

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 commented 3 months ago

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

jacopoabramo commented 3 months ago

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 commented 3 months ago

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

https://github.com/pyapp-kit/psygnal/blob/8f4d781a57c85755a26c169608305e5525cbec05/tests/test_psygnal.py#L109-L120

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

jacopoabramo commented 3 months ago

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 commented 3 months ago

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