Last active
April 28, 2023 13:57
-
-
Save dustinfarris/8851078 to your computer and use it in GitHub Desktop.
Stubbing out Kivy windows for your unit tests (using py.test)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :(