Skip to main content

pytest-reserial

build PyPI License

Pytest plugin for recording and replaying serial port traffic during tests.

Installation

pip install pytest-reserial

Usage

  1. Write your tests as if they would run with the device connected. While you iterate you can use pytest --disable-reserial to have your code interact with the device without any side effects.

  2. When your tests pass with the device connected, run pytest --record to record serial traffic from the passing tests.

  3. Now you can disconnect the device and run your tests with pytest.

A simple example:

# my_serial_app.py
from serial import Serial

def my_serial_app():
    with Serial(port=/dev/ttyUSB0) as s:
        # When we send '1' to the device, it responds with '2'.
        s.write(b"\x01")
        return s.read()

# Just use the 'reserial' fixture!
def test_my_serial_app(reserial):
    assert my_serial_app() == b"\x02"

Next:

  1. Connect the device.
  2. Run pytest my_serial_app.py --disable-reserial and verify that the test passes with the device connected.
  3. Run pytest --record my_serial_app.py. The test will run again, and the traffic will be recorded.
  4. Disconnect the device.
  5. Run pytest my_serial_app.py. The test will pass!

The logged traffic will be stored as JSON Lines, with one file per test file and one line per test, in the same directory as your test files. The files will have the same names as the test files except with a .jsonl extension instead of .py. For example, if your project layout is:

├── src
│   ├── myproject
│      ├── ...
├── tests
│   ├── test_myproject.py

Then after running pytest --record, the tests/ directory will contain a new file, test_myproject.jsonl, containing the recorded serial traffic from the tests.

Why

Have 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 in another state. 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 the reserial fixture to record the serial traffic from passing tests, and replay it when the device isn't connected.

Requirements

pytest-reserial depends on pytest and pyserial.

Copyright

MIT License, (C) 2022 Alexander Bessman

Release files for pytest-reserial 0.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-reserial 0.6.1
File Size Uploaded
pytest_reserial-0.6.1.tar.gz 14.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-reserial 0.6.1
File Interpreter ABI Platform
pytest_reserial-0.6.1-py3-none-any.whl Python 3 none any Details

Total release size: 23.1 kB

Release files / pytest_reserial-0.6.1.tar.gz

Download URL pytest_reserial-0.6.1.tar.gz
Size 14.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c69699ae4b219223bd81c0e3516523d3e8fd91cfc0d825668ace096248830423
BLAKE2b-256 checksum
How to use checksums
2b5f133606cf5b3b4468fcccdce18c57acd9f8a041179bf4708d85d67ba3f5a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 30, 2025.

Transparency log

Release files / pytest_reserial-0.6.1-py3-none-any.whl

Download URL pytest_reserial-0.6.1-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
da9a5a23a3f1e727674cfd9939d8d471711d6f81245461b54b8fd1cd299f5106
BLAKE2b-256 checksum
How to use checksums
7976aa8888c77ede5dbf39bf34be2ab880d61e44a4a06766314fbcddddbab880
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 30, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.6.1 This release

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page