gpiozero / gpiozero

A simple interface to GPIO devices with Raspberry Pi
https://gpiozero.readthedocs.io/
Other
1.92k stars 304 forks source link

MockFactory does not release pins on close() #1087

Open jornj opened 1 year ago

jornj commented 1 year ago

Operating system: Ubuntu 22.04.3 LTS Python version: 3.9.7 Pi model: None. GPIO Zero version: 1.6.2 Pin factory used: MockFactory

It appears that the MockFactory does not release pins on close(). Is there a better way to release pins in a pytest teardown_function?

from gpiozero import Device, DigitalInputDevice
from gpiozero.pins.mock import MockFactory

def test_mockfactory_releases_pins_on_close():
    Device.pin_factory = MockFactory()
    device = DigitalInputDevice("J8:10")
    Device.pin_factory.close()
    device = DigitalInputDevice("J8:10")

test_mockfactory_releases_pins_on_close()

This fails with gpiozero.exc.GPIOPinInUse: pin 15 is already in use by <gpiozero.DigitalInputDevice object on pin GPIO15, pull_up=False, is_active=False>

MusicalNinjaDad commented 9 months ago

An example of how to do this is in the mock_factory fixture in tests/conftest.py I've also raises PR #1107 to make these fixtures available for import in the future.