Skip to main content

Pytest fixture for recording and replaying serial port traffic.

Project description

pytest-reserial

build Codacy Badge codecov

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.

  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 --replay.

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 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 --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.py

Then after running pytest --record, the test/ directory will contain a new file, test_myproject.json, 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, instad 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-reserial-0.2.2.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

pytest_reserial-0.2.2-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file pytest-reserial-0.2.2.tar.gz.

File metadata

  • Download URL: pytest-reserial-0.2.2.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.0

File hashes

Hashes for pytest-reserial-0.2.2.tar.gz
Algorithm Hash digest
SHA256 99c94d43634503ea267da8617348cff562d5a8f89ed5666bc381e722443d2530
MD5 2cacd2b1c5a2de277d4457e69b0e5954
BLAKE2b-256 9b09a2ad5b97250ca15b634a8000b5a20980e2850834356504fe3dd27a9b7a6e

See more details on using hashes here.

Provenance

File details

Details for the file pytest_reserial-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_reserial-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9af69de235b986f14f5e09530f654b7d4688170933f18ea8e0ba8d496983dd2c
MD5 be9a1fb2e72a55968ec99f800e654785
BLAKE2b-256 eb6a4cd0e6a56c42303abb3579937d1964eb0d6a5367cfe73a28ccdcfbe60de3

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page