I am working on software for a robot that usually runs on a Raspberry Pi. Consider importing two files:
motor.py (starts engines):
from RPi import GPIO as gpio
and client.py (communicates with the server and sends commands to the engines):
from rpi.motor import Motor
Both files are located in a directory named rpi , which contains __init__.py and __main__.py . The rpi package cannot be installed on devices other than RPi. However, I still want to test the functionality of client.py .
import unittest from unittest import mock
I initially tried from rpi.client import Client in LocA, but this did not succeed because it tried to import Motor and then import GPIO from RPi, which does not exist. I also tried mock.patch("rpi.client.Motor") in LocB (including adding mock_motor after self and imported Client into LocC, but that also failed). I also tried to taunt rpi in LocA, but that did not work either.
How do you mock a library that is not installed on your system?
source share