If you trust the RPi.GPIO library, I think this is a good formulation, you can patch it within unittest.mock , patch RPi.GPIO.output give you the opportunity to break the dependency on HW and feel the calls of this function.
It could be your test class.
import unittest from unittest.mock import patch, call from my_module import MoterManager @patch("RPi.GPIO.output", autospec=True) class TestMoterManager(unittest.TestClass): in1Pin=67 in2Pin=68 def test_init(self, mock_output): """If you would MoterManager() stop motor when you build it your test looks like follow code""" mm = MoterManager(self.in1Pin,self.in1Pin) mock_output.assert_has_calls([call(self.in1Pin, False),call(self.in2Pin, False)],any_order=True) def test_stop(self, mock_output): mm = MoterManager(self.in1Pin,self.in1Pin) mock_output.reset_mock mm.stop() mock_output.assert_has_calls([call(self.in1Pin, False),call(self.in2Pin, False)],any_order=True) def test_brake(self, mock_output): mm = MoterManager(self.in1Pin,self.in1Pin) mock_output.reset_mock mm.stop() mock_output.assert_has_calls([call(self.in1Pin, True),call(self.in2Pin, True)],any_order=True) def test_rotateClockwise(self, mock_output): mm = MoterManager(self.in1Pin,self.in1Pin) mock_output.reset_mock mm.stop() mock_output.assert_has_calls([call(self.in1Pin, True),call(self.in2Pin, False)],any_order=True) def test_rotateCounterClockwise(self, mock_output): mm = MoterManager(self.in1Pin,self.in1Pin) mock_output.reset_mock mm.stop() mock_output.assert_has_calls([call(self.in1Pin, False),call(self.in2Pin, True)],any_order=True)
A few notes:
- In python-2.7, use the
mock available by pip instead of unittest.mock - I use
autospec=True almost every test, if you are wondering why look at this - My tests should cause almost 1 error in your code: typo in
brake method
source share