Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MagicMock if GPIO import fails #86

Open
alxwrd opened this issue Feb 6, 2018 · 1 comment
Open

Use MagicMock if GPIO import fails #86

alxwrd opened this issue Feb 6, 2018 · 1 comment

Comments

@alxwrd
Copy link
Collaborator

alxwrd commented Feb 6, 2018

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?

@jmsv
Copy link
Owner

jmsv commented Feb 7, 2018

@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 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants