class TestGPIO(unittest.TestCase): def __init__(self, name): super(TestGPIO, self).__init__(name) self._isRoot = os.getuid() == 0 def setUp(self): self.gpio = GPIO() def tearDown(self): self.gpio.cleanup() def test_isRootUser(self): self.assertTrue(self.gpio.isRootUser() == self._isRoot) def test__getGpioId(self): validPins = ('p8.7', 'p8_7', 'P8_7', 'gpio2_2', 'GPIO2.2', 'gpio_66',) validPin = 66 msg = u"Invalid pin value found: {}, should have been: {}." for pin in validPins: gpioId = self.gpio._getGpioId(pin) self.assertTrue(gpioId == validPin, msg.format(gpioId, validPin)) invalidPins = ('q8_7', 'gpio_125', '',) for pin in invalidPins: with self.assertRaises(InvalidPinNomenclatureException): gpioId = self.gpio._getGpioId(pin) print("Invalid result, should have failed, " "gpioId: {}".format(gpioId)) def test_setMode(self): # Test setMode with only first argument. pin = u'gpio_44' self.gpio.setMode(pin) result = self.gpio.getDirection(pin) self.assertTrue(result == u'in', msg=u"Invalid pin direction, found: " u"'{}', should have been: 'out'".format(result)) result = self.gpio.getEdge(pin) self.assertTrue(result == u'none', msg=u"Invalid pin edge, found: " u"'{}', should have been: 'both'".format(result)) result = self.gpio.getValue(pin) self.assertTrue(result == 0, msg=u"Invalid pin value, found: " u"'{}', should have been: '0'".format(result)) # Test setMode with all arguments. pin = u'gpio_67' directions = (GPIO.IN, GPIO.OUT,) edges = (GPIO.RISING, GPIO.FALLING, GPIO.BOTH,) for direction in directions: for edge in edges: kwargs = {u'direction': direction, u'edge': edge} self.gpio.setMode(pin, **kwargs) d = self.gpio.getDirection(pin) self.assertTrue(d == direction, msg=u"Invalid direction, " u"found: {}, should have been: {}.".format( d, direction)) e = self.gpio.getEdge(pin) self.assertTrue(e == edge, msg=u"Invalid edge, found: {}, " u"should have been: {}.".format(e, edge)) self.gpio.cleanup(pin) def test_setDirection(self): pin = u'gpio_30' self.gpio.setMode(pin) self.gpio.setDirection(pin, GPIO.IN) d = self.gpio.getDirection(pin) self.assertTrue(d == GPIO.IN, msg=u"Invalid direction, found: {}, " u"should have been: {}.".format(d, GPIO.IN)) def test_setEdge(self): pin = u'gpio_30' self.gpio.setMode(pin) self.gpio.setEdge(pin, GPIO.FALLING) e = self.gpio.getEdge(pin) self.assertTrue(e == GPIO.FALLING, msg=u"Invalid edge, found: {}, " u"should have been: {}.".format(e, GPIO.FALLING)) def test_setValue(self): pin = u'gpio_30' self.gpio.setMode(pin, direction=GPIO.OUT) self.gpio.setValue(pin, GPIO.LOW) e = self.gpio.getValue(pin) self.assertTrue(e == GPIO.LOW, msg=u"Invalid value, found: {}, " u"should have been: {}.".format(e, GPIO.LOW))
class TestGPIO(unittest.TestCase): def __init__(self, name): super(TestGPIO, self).__init__(name) self._isRoot = os.getuid() == 0 def setUp(self): self.gpio = GPIO() def tearDown(self): self.gpio.cleanup() def test_isRootUser(self): self.assertTrue(self.gpio.isRootUser() == self._isRoot) def test__getGpioId(self): validPins = ( 'p8.7', 'p8_7', 'P8_7', 'gpio2_2', 'GPIO2.2', 'gpio_66', ) validPin = 66 msg = u"Invalid pin value found: {}, should have been: {}." for pin in validPins: gpioId = self.gpio._getGpioId(pin) self.assertTrue(gpioId == validPin, msg.format(gpioId, validPin)) invalidPins = ( 'q8_7', 'gpio_125', '', ) for pin in invalidPins: with self.assertRaises(InvalidPinNomenclatureException): gpioId = self.gpio._getGpioId(pin) print("Invalid result, should have failed, " "gpioId: {}".format(gpioId)) def test_setMode(self): # Test setMode with only first argument. pin = u'gpio_44' self.gpio.setMode(pin) result = self.gpio.getDirection(pin) self.assertTrue(result == u'in', msg=u"Invalid pin direction, found: " u"'{}', should have been: 'out'".format(result)) result = self.gpio.getEdge(pin) self.assertTrue(result == u'none', msg=u"Invalid pin edge, found: " u"'{}', should have been: 'both'".format(result)) result = self.gpio.getValue(pin) self.assertTrue(result == 0, msg=u"Invalid pin value, found: " u"'{}', should have been: '0'".format(result)) # Test setMode with all arguments. pin = u'gpio_67' directions = ( GPIO.IN, GPIO.OUT, ) edges = ( GPIO.RISING, GPIO.FALLING, GPIO.BOTH, ) for direction in directions: for edge in edges: kwargs = {u'direction': direction, u'edge': edge} self.gpio.setMode(pin, **kwargs) d = self.gpio.getDirection(pin) self.assertTrue(d == direction, msg=u"Invalid direction, " u"found: {}, should have been: {}.".format( d, direction)) e = self.gpio.getEdge(pin) self.assertTrue(e == edge, msg=u"Invalid edge, found: {}, " u"should have been: {}.".format(e, edge)) self.gpio.cleanup(pin) def test_setDirection(self): pin = u'gpio_30' self.gpio.setMode(pin) self.gpio.setDirection(pin, GPIO.IN) d = self.gpio.getDirection(pin) self.assertTrue(d == GPIO.IN, msg=u"Invalid direction, found: {}, " u"should have been: {}.".format(d, GPIO.IN)) def test_setEdge(self): pin = u'gpio_30' self.gpio.setMode(pin) self.gpio.setEdge(pin, GPIO.FALLING) e = self.gpio.getEdge(pin) self.assertTrue(e == GPIO.FALLING, msg=u"Invalid edge, found: {}, " u"should have been: {}.".format(e, GPIO.FALLING)) def test_setValue(self): pin = u'gpio_30' self.gpio.setMode(pin, direction=GPIO.OUT) self.gpio.setValue(pin, GPIO.LOW) e = self.gpio.getValue(pin) self.assertTrue(e == GPIO.LOW, msg=u"Invalid value, found: {}, " u"should have been: {}.".format(e, GPIO.LOW))