-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,11 @@ | ||
"""Tests can be run as either unit tests or integration tests. | ||
By default, they are run as unit tests. When running as unit tests, recorded | ||
serial traffic from passing integration tests are played back during the test. | ||
By calling pytest with the --integration flag, the tests will instead be run as | ||
integration tests. In this mode, a real PSLab device must be connected. | ||
Additionally, certain pins must be connected in a specific manner. Refer to the | ||
individual test modules in the tests package. | ||
By calling pytest with the --record flag, the serial traffic generated by the | ||
integration tests will be recorded to JSON files, which are played back during | ||
unit testing. The --record flag implies --integration. | ||
""" | ||
|
||
import json | ||
import os | ||
"""Common fixtures for pslab tests.""" | ||
|
||
import pytest | ||
|
||
from pslab import serial_handler | ||
|
||
|
||
def pytest_addoption(parser): | ||
parser.addoption("--integration", action="store_true", default=False) | ||
parser.addoption("--record", action="store_true", default=False) | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def logdir(request): | ||
# First five chars are "test_", last three are ".py". | ||
return os.path.join("tests", "recordings", request.node.name[5:][:-3]) | ||
|
||
|
||
@pytest.fixture | ||
def handler(monkeypatch, request, logdir): | ||
"""Return a SerialHandler instance. | ||
When running unit tests, the SerialHandler is a MockHandler. | ||
""" | ||
record = request.config.getoption("--record") | ||
integration = request.config.getoption("--integration") or record | ||
logfile = os.path.join(logdir, request.node.name + ".json") | ||
|
||
if integration: | ||
H = serial_handler.SerialHandler() | ||
else: | ||
tx, rx = json.load(open(logfile, "r")) | ||
traffic = ((bytes(t), bytes(r)) for t, r in zip(tx, rx)) | ||
monkeypatch.setattr(serial_handler, "RECORDED_TRAFFIC", traffic) | ||
H = serial_handler.MockHandler() | ||
|
||
yield H | ||
|
||
if record: | ||
log = H._log.split(b"STOP")[:-1] | ||
record_traffic(log, logfile) | ||
|
||
|
||
def record_traffic(log: list, logfile: str): | ||
"""Record serial traffic to a JSON file. | ||
The file name is the test name + .json. | ||
""" | ||
tx = [] | ||
rx = [] | ||
|
||
for b in log: | ||
direction = b[:2] | ||
data = b[2:] | ||
if direction == b"TX": | ||
tx.append(list(data)) | ||
rx.append([]) | ||
elif direction == b"RX": | ||
rx[-1] += list(data) | ||
else: | ||
raise ValueError(f"Unknown direction: {direction}") | ||
|
||
print([tx, rx]) | ||
json.dump([tx, rx], open(logfile, "w")) | ||
def handler(): | ||
"""Return a SerialHandler instance.""" | ||
return serial_handler.SerialHandler() |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.