craftbeerpi4-pione/tests/test_gpio.py

50 lines
1.3 KiB
Python
Raw Normal View History

2019-01-04 09:29:09 +01:00
import unittest
from unittest import mock
from unittest.mock import MagicMock, patch
try:
import RPi.GPIO as GPIO
except Exception:
print("Error importing RPi.GPIO!")
MockRPi = MagicMock()
modules = {
"RPi": MockRPi,
"RPi.GPIO": MockRPi.GPIO
}
patcher = patch.dict("sys.modules", modules)
patcher.start()
import RPi.GPIO as GPIO
class HelloWorld(object):
def test(self, a):
return a
class TestSwitch(unittest.TestCase):
GPIO_NUM = 22
@patch("RPi.GPIO.setup")
def test_switch_inits_gpio(self, patched_setup):
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.GPIO_NUM, GPIO.OUT)
patched_setup.assert_called_once_with(self.GPIO_NUM, GPIO.OUT)
@patch("RPi.GPIO.output")
def test_switch_without_scheduler_starts_disabled(self, patched_output):
GPIO.output(self.GPIO_NUM, GPIO.LOW)
patched_output.assert_called_once_with(self.GPIO_NUM, GPIO.LOW)
def test_hello_world(self):
h = HelloWorld()
with mock.patch.object(HelloWorld, 'test', wraps=h.test) as fake_increment:
#print(h.test("HALLO"))
print(h.test("ABC"))
print(fake_increment.call_args)
print(h.test("HALLO"))
print(fake_increment.call_args_list)