Package: pytest-reserial
pytest-reserialPytest plugin for recording and replaying serial port traffic during tests.Installationpip install pytest-reserialUsageWrite your tests as if they would run with the device connected.When your tests pass with the device connected, runpytest --recordto record serial traffic
from the passing tests.Now you can disconnect the device and run your tests withpytest --replay.A simple example:# my_serial_app.pyfromserialimportSerialdefmy_serial_app():withSerial(port=/dev/ttyUSB0)ass:# When we send '1' to the device, it responds with '2'.s.write(b"\x01")returns.read()# Just use the 'reserial' fixture!deftest_my_serial_app(reserial):assertmy_serial_app()==b"\x02"Next:Connect the device.Runpytest my_serial_app.pyand verify that the test passes with the device connected.Runpytest --record my_serial_app.py. The test will run again, and the traffic will be recorded.Disconnect the device.Runpytest --replay my_serial_app.py. The test will pass!The logged traffic will be stored in JSON files in the same directory as your test files, and will have the same names as the test files except with a .json extension instead of .py. For example, if your project layout is:├──src
│├──myproject
││├──...
├──tests
│├──test_myproject.pyThen after runningpytest --record, the test/ directory will contain a new file, test_myproject.json, containing the recorded serial traffic from the tests.WhyHave you ever tried to write tests for a program that talks to an external device over serial (like an Arduino or something)? You probably wrote the tests assuming that the device is question would always be connected when running the tests, right? And later you got bit by one or more of the pitfalls of that approach:You wanted to run the tests when the device wasn't connected. Perhaps you were travelling, or
someone had borrowed it. Whatever the reason, you found yourself unable to run the tests, and
therefore unable to continue development, until you could connect the device again.You made a change to your program, and one of your tests failed. So far so good, right? That's
what tests are for, after all. Only, you can't figure out why the test is failing. You spend
several hours trying to fix it, but eventually give up and revert your changes.
But the test still fails.
So you try another device, and sure enough, now it passes. Turns out, what you thought was a
problem with your code was actually a hardware failure.Some of the tests depend on the device being in a certain state, and some of the tests depend on
the device being inanotherstate. So you can't run the entire test suite all at once, instead
being forced to stop it halfway through and mess with a bunch of wires and buttons before you can
run the rest of the tests.And then you asked yourself, 'How do I write my tests so that the device doesn't need to be connected?' You may have gone down the rabbit hole that is mocking, and then replaced large parts of pyserial with mock interfaces, and ultimately ended up with a test suite that was significantly more complex than the program it was meant to test.With pytest-reserial, you don't have to worry about any of that. Just write your tests as if the device is always connected. Then, simply use thereserialfixture to record the serial traffic from passing tests, and replay it when the device isn't connected.Requirementspytest-reserial depends on pytest and pyserial.If jsbeautifier is installed, each test's recorded traffic is written on a single line in the log files. Otherwise, each recorded byte is written on a separate line.CopyrightMIT License, (C) 2022 Alexander Bessman