l293d icon indicating copy to clipboard operation
l293d copied to clipboard

Use MagicMock if GPIO import fails

Open alxwrd opened this issue 7 years ago • 1 comments

I had a thought (when commenting on #62) that when trying to import GPIO, on an import error we could switch it out for MagicMock. This would mean there wouldn't be any need to set Config.test_mode = True as the mock object would just silently 'run' GPIO methods.

from unittest.mock import MagicMock

try:
    import RPi.GPIO as GPIO
except ImportError:
    GPIO = MagicMock()

I'm not 100% on this as it feels a bit hacky to use a testing mock object in the main module. Thoughts?

alxwrd avatar Feb 06 '18 21:02 alxwrd

@alxwrd Seems like an excellent idea, I didn't know this existed!

I'd quite like to keep the test mode config option because it could be handy when RPi.GPIO is installed, for if you wanted to just run a test script or something. Although this could just be done with something like:

if Config.test_mode:
    GPIO = MagicMock()

Feel free to implement this 😃

jmsv avatar Feb 07 '18 19:02 jmsv