Printer tests rework #574
Replies: 3 comments 3 replies
-
I've been curious about how 'pyserial' and 'pyusb' perform their tests and I've found some interesting stuff: The The We could inject or mock one of this fake devices into a |
Beta Was this translation helpful? Give feedback.
-
Following your suggestion I reworked the previous tests to make use of the mocker fixture and also adapted them to the Serial printer: Note that I don't know how to force the mock device to raise a valid exception that triggers the DeviceNotFoundError, so I don't mock the device on those tests. Maybe some tests are not necessary. def test_device_not_initialized(serial):
"""
GIVEN a serial printer object
WHEN it is not initialized
THEN check the device property is False
"""
assert serial._device is False
def test_open_raise_exception(serial, devicenotfounderror):
"""
GIVEN a serial printer object
WHEN open() is set to raise a DeviceNotFoundError on error
THEN check the exception is raised
"""
serial.devfile = "fake/device"
with pytest.raises(devicenotfounderror):
serial.open(raise_not_found=True)
def test_open_not_raise_exception(serial, caplog):
"""
GIVEN a serial printer object
WHEN open() is set to not raise on error but simply cancel
THEN check the error is logged and open() canceled
"""
serial.devfile = "fake/device"
with caplog.at_level(logging.ERROR):
serial.open(raise_not_found=False)
assert "not found" in caplog.text
assert serial.device is None
def test_open(serial, caplog, mocker):
"""
GIVEN a serial printer object and a mocked pyserial device
WHEN a valid connection to a device is opened
THEN check the success is logged and the device property is set
"""
mocker.patch("serial.Serial")
with caplog.at_level(logging.INFO):
serial.open()
assert "enabled" in caplog.text
assert serial.device
def test_close(serial, caplog, mocker):
"""
GIVEN a serial printer object and a mocked pyserial device
WHEN a connection is opened and closed
THEN check the closing is logged and the device property is False
"""
mocker.patch("serial.Serial")
serial.open()
with caplog.at_level(logging.INFO):
serial.close()
assert "Closing" in caplog.text
assert serial._device is False All this tests are actually passing OK. |
Beta Was this translation helpful? Give feedback.
-
If no objections, I'm going to open a new PR adding the reworked printer tests. |
Beta Was this translation helpful? Give feedback.
-
This is an attempt to rework the tests battery for the File printer that I think it could be extended (in essence) to the rest of the printer connectors.
I've tried in my local machine and seems to work, all this tests are passing.
Downloadable version: test_printer_file.py.zip
The
file
anddevicenotfounderror
fixtures would be found in conftest.py with other printer connector fixtures:As the
File
printer points to a file object it is easy to make it write to/dev/null
or to atemp_file
to simulate a printer, but obviously it is not as easy to do on the other printer connectors.What do you think about this approach?
Beta Was this translation helpful? Give feedback.
All reactions