Skip to content

Instantly share code, notes, and snippets.

@dustinfarris
Last active April 28, 2023 13:57
Show Gist options
  • Save dustinfarris/8851078 to your computer and use it in GitHub Desktop.
Save dustinfarris/8851078 to your computer and use it in GitHub Desktop.
Stubbing out Kivy windows for your unit tests (using py.test)
"""
Unit testing Kivy is easy, but requires a little boiler plate to keep the
window quiet while you test your code. This is one way to set up py.test,
but the same technique can probably be used with other test runners as well.
More information on how these py.test hooks work:
http://pytest.org/latest/plugins.html#generic-runtest-hooks
"""
import mock
from kivy.base import EventLoopBase
def pytest_runtest_setup(item):
item.mock_patches = [
mock.patch('kivy.uix.widget.Builder'),
mock.patch.object(EventLoopBase, 'ensure_window', lambda x: None),
]
for patch in item.mock_patches:
patch.start()
def pytest_runtest_teardown(item, nextitem):
for patch in item.mock_patches:
patch.stop()
@Albert-Gao
Copy link

Hi, thanks sharing for this, how to get this running? I put the conftest.py there, but still doesn't work, I need to initalize an object which needs to interact with the UI to fethc some value :(

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