Skip to content

Commit

Permalink
Increase test coverage from 70% to 93%
Browse files Browse the repository at this point in the history
Note that Travis doesn't have access to camera device.
Also see discovered camera release bug while testing, refs #14
  • Loading branch information
AndreMiras committed Oct 1, 2019
1 parent e069870 commit 149b2f9
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 13 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ venv/
.buildozer/
.pytest_cache/
.tox/
htmlcov/
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ PYTHON_VERSION=$(PYTHON_MAJOR_VERSION).$(PYTHON_MINOR_VERSION)
PYTHON_MAJOR_MINOR=$(PYTHON_MAJOR_VERSION)$(PYTHON_MINOR_VERSION)
PYTHON_WITH_VERSION=python$(PYTHON_VERSION)

ifndef CI
DEVICE=--device=/dev/video0:/dev/video0
endif


all: system_dependencies virtualenv

Expand Down Expand Up @@ -94,7 +98,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 All @@ -106,10 +110,10 @@ docker/build:
docker build --tag=xcamera-linux --file=dockerfiles/Dockerfile-linux .

docker/run/test:
docker run --env-file dockerfiles/env.list -v /tmp/.X11-unix:/tmp/.X11-unix xcamera-linux 'make test'
docker run --env-file dockerfiles/env.list -v /tmp/.X11-unix:/tmp/.X11-unix $(DEVICE) xcamera-linux 'make test'

docker/run/app:
docker run --env-file dockerfiles/env.list -v /tmp/.X11-unix:/tmp/.X11-unix --device=/dev/video0:/dev/video0 xcamera-linux 'make run'
docker run --env-file dockerfiles/env.list -v /tmp/.X11-unix:/tmp/.X11-unix $(DEVICE) xcamera-linux 'make run'

docker/run/shell:
docker run --env-file dockerfiles/env.list -v /tmp/.X11-unix:/tmp/.X11-unix --device=/dev/video0:/dev/video0 -it --rm xcamera-linux
docker run --env-file dockerfiles/env.list -v /tmp/.X11-unix:/tmp/.X11-unix $(DEVICE) -it --rm xcamera-linux
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
11 changes: 6 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ skipsdist = True
setenv =
PYTHONPATH = {toxinidir}/src/
SOURCES = src/ tests/ setup.py setup_meta.py
passenv = DISPLAY
passenv =
CI
DISPLAY
deps =
-r{toxinidir}/requirements.txt
-r{toxinidir}/requirements/requirements-test.txt
commands =
pytest --cov src/ tests/
-r{toxinidir}/requirements.txt
-r{toxinidir}/requirements/requirements-test.txt
commands = pytest --cov src/ tests/

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

0 comments on commit 149b2f9

Please sign in to comment.