I am working on software for a robot, which is normally run on the Raspberry Pi. Let's consider the imports of two files:
motor.py
(runs the motors):
from RPi import GPIO as gpio
and client.py
(communicates with the server and relays commands to the motors):
from rpi.motor import Motor
Both files are in a directory called rpi
, which contains a __init__.py
and a __main__.py
. The RPi
package cannot be installed on non-RPi devices. However, I still want to test the functionality of client.py
.
import unittest
from unittest import mock
# Location A
class TestClient(unittest.TestCase):
# Location B
setUp(self):
# Location C
pass
Originally, I tried from rpi.client import Client
at LocA, but that failed because it tried to import Motor, and then import GPIO from RPi, which doesn't exist. I also tried mock.patch("rpi.client.Motor")
at LocB (including adding mock_motor
after self
, and imported Client
at LocC, but that failed as well. I tried mocking RPi
at LocA, too, but it didn't work either.
How do you mock out a library that is not installed on your system?