Skip to content

Commit 9dfa8e8

Browse files
committed
more tests, using mock for hardware modules
1 parent 84db777 commit 9dfa8e8

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

src/conftest.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python
12
# -*- coding: utf-8 -*-
23
from lock import RPiLock
34

src/test_lock.py

100644100755
Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,56 @@
1+
#!/usr/bin/env python
12
# -*- coding: utf-8 -*-
2-
from lock import RPiLock
3+
import unittest
4+
try:
5+
from unittest.mock import patch, MagicMock
6+
except ImportError:
7+
from mock import patch, MagicMock
38

49

5-
def test_init():
6-
"""Test instantiate a RPiLock object."""
7-
lock = RPiLock('localhost', 8000)
8-
assert False
10+
class LockTestCase(unittest.TestCase):
11+
"""Test the lock class."""
12+
def setUp(self):
13+
class Pi(object):
14+
def __init__(self):
15+
self.pulsewidth = 0
16+
17+
def set_servo_pulsewidth(self, pin_num, pulsewidth):
18+
self.pulsewidth = pulsewidth
19+
20+
def get_servo_pulsewidth(self, pin_num, pulsewidth):
21+
return self.pulsewidth
22+
23+
modules = {
24+
'pigpio': MagicMock(),
25+
'pigpio.pi': Pi,
26+
'socketIO_client': MagicMock(),
27+
}
28+
self.fake_imports = patch.dict('sys.modules', modules)
29+
self.fake_imports.start()
30+
31+
def tearDown(self):
32+
self.fake_imports.stop()
33+
34+
def test_init_lock(self):
35+
"""Test instantiate a RPiLock object."""
36+
from lock import RPiLock
37+
lock = RPiLock('localhost', 8000)
38+
self.assertTrue('lock' and 'unlock' in lock.avail_actions)
39+
40+
def test_action_not_permitted(self):
41+
from lock import RPiLock
42+
lock = RPiLock('localhost', 8000)
43+
self.assertRaises(ValueError, lock.control, 'randomaction')
44+
45+
def test_action_unlock(self):
46+
from lock import RPiLock
47+
lock = RPiLock('localhost', 8000)
48+
self.assertTrue(lock.control('unlock'), 600)
49+
50+
def test_action_lock(self):
51+
from lock import RPiLock
52+
lock = RPiLock('localhost', 8000)
53+
self.assertTrue(lock.control('lock'), 2400)
54+
55+
if __name__ == '__main__':
56+
unittest.main()

tox.ini

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[tox]
22
envlist = py27, py35
33
[testenv]
4-
commands = py.test tests --cov=src --cov-report term-missing
4+
commands = python src/test_lock.py
55
deps =
6-
pytest
7-
pytest-cov
6+
mock

0 commit comments

Comments
 (0)