I'm working on a python project where I'll be pulling in an external dependency called neopixel.
import time
import board
from rainbowio import colorwheel
import neopixel # here's our module's import
class Lane:
def __init__(self, pin, total_pixels):
self.pin = pin
self.total_pixels = total_pixels
self.leds = []
self.initialize_leds()
def initialize_leds(self, brightness=0.3, auto_write=False):
# and here's how we use it
self.leds = neopixel.NeoPixel(self.pin, self.total_pixels, brightness=brightness, auto_write=auto_write)
I'm trying to write out unit tests for this project, so I want to mock out the dependency. Moreover I need to mock out the dependency because when you import it, it imports a couple of other transient dependencies that error out the code because it's expected to run on a microcontroller.
I tried patching the NeoPixel class, but kept running into this error and then realized that even though I'm patching the class, the entire file (including it's imports) is still being imported in an running during the code so those top imports are still being pulled in.
Is there a way of mocking an entire file so that the imports aren't brought in as well?