Skip to content

Commit

Permalink
Increase test coverage from 70% to 93%
Browse files Browse the repository at this point in the history
Also see discovered camera release bug while testing, refs #14
  • Loading branch information
AndreMiras committed Oct 1, 2019
1 parent e069870 commit 65be5a6
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ release/upload:
$(TWINE) upload dist/*

clean: release/clean docs/clean
py3clean src/
py3clean .
find . -type d -name "__pycache__" -exec rm -r {} +
find . -type d -name "*.egg-info" -exec rm -r {} +
rm -rf htmlcov/
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Notes:
[xcamera is available on PyPI](https://pypi.org/project/xcamera/).
Therefore it can be installed via `pip`.
```sh
pip3 install --user xcamera
pip3 install xcamera
```
Once installed, the demo should be available in your `PATH` and can be ran from the command line.
```sh
Expand Down
Empty file added tests/__init__.py
Empty file.
Empty file.
38 changes: 38 additions & 0 deletions tests/kivy_garden/xcamera/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from threading import Thread
from unittest import mock

from kivy.app import App
from kivy.clock import Clock

from kivy_garden.xcamera.main import CameraApp, main
from tests.test_main import camera_release_workaround, patch_core_camera


def patch_picture_taken():
return mock.patch('kivy_garden.xcamera.main.CameraApp.picture_taken')


class TestMain:
"""
Tests the `main` module.
"""

def test_picture_taken(self):
"""
Checks the `picture_taken()` listener gets called on the running app.
"""
app_thread = Thread(target=main)
app_thread.start()
app = App.get_running_app()
filename = mock.sentinel
Clock.schedule_once(
lambda dt: app.root.ids.xcamera.dispatch(
'on_picture_taken', filename))
# makes sure app thread is gracefully stopped before asserting
app.stop()
with patch_picture_taken() as m_picture_taken, patch_core_camera():
app_thread.join()
camera_release_workaround(app)
assert type(app) == CameraApp
assert m_picture_taken.mock_calls == [
mock.call(app.root.ids.xcamera, filename)]
44 changes: 41 additions & 3 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,52 @@
import os
from threading import Thread
from unittest import mock

from kivy.app import App

from kivy_garden.xcamera.main import CameraApp
from main import main


def camera_release_workaround(app):
"""
Upstream bug workaround, refs:
https://github.com/kivy-garden/xcamera/issues/14
"""
app.root.ids.xcamera._camera._device.release()


def get_camera_class():
"""
Continuous integration providers don't have a camera available.
"""
if os.environ.get('CI', False):
Camera = None
else:
from kivy.core.camera import Camera
return Camera


def patch_core_camera():
Camera = get_camera_class()
return mock.patch('kivy.uix.camera.CoreCamera', wraps=Camera)


class TestMain:
"""
Tests the `main` module.
"""

def test_main(self):
with mock.patch('kivy_garden.xcamera.main.CameraApp.run') as m_play:
main()
assert m_play.mock_calls == [mock.call()]
"""
Checks the main starts the app properly.
"""
app_thread = Thread(target=main)
app_thread.start()
app = App.get_running_app()
# makes sure app thread is gracefully stopped before asserting
app.stop()
with patch_core_camera():
app_thread.join()
camera_release_workaround(app)
assert type(app) == CameraApp
3 changes: 1 addition & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ passenv = DISPLAY
deps =
-r{toxinidir}/requirements.txt
-r{toxinidir}/requirements/requirements-test.txt
commands =
pytest --cov src/ tests/
commands = pytest --cov src/ tests/

[testenv:pep8]
commands = flake8 {env:SOURCES}
Expand Down

0 comments on commit 65be5a6

Please sign in to comment.