Michael Jack on Nostr: I have a question about Python libraries and testing scope. If I'm importing 'serial' ...
I have a question about Python libraries and testing scope.
If I'm importing 'serial' in my library, and use it like the following to create a connection to a sensor:
--- start code ---
import serial
class Sensor:
def __init__(self, serial_device):
self.__serial_device = serial_device
try:
self.__connection = serial.Serial(
port=serial_device,
baudrate=9600,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
)
except serial.SerialException:
print("Could not establish serial connection to sensor")
--- end code ---
how much testing should I do around the serial connection? Just mock up a few buffers (byte streams), and see how my class handles unexpected input?
One the one hand, I want to make the library as solid as possible. On the other hand, I don't want to run tests on code I don't control (the library module). I know of the 'mock-serial' utility, but haven't used it.
The aim is to make a Python version of my Arduino library for the CozIR Ambient CO2 sensor:
https://codeberg.org/mjack/ambientCO2/src/branch/develop#python #unittest #testing #sensors
Published at
2026-03-03 11:55:21 UTCEvent JSON
{
"id": "b45535f195098a5865d8c2cb18376dd683673e1f7368fe975ffd9848591e5c23",
"pubkey": "d30f85b8de09477e9e999b4f90bdd6e189d78cd3bf130df273e183d4775a6758",
"created_at": 1772538921,
"kind": 1,
"tags": [
[
"t",
"python"
],
[
"t",
"unittest"
],
[
"t",
"testing"
],
[
"t",
"sensors"
],
[
"proxy",
"https://mastodon.bsd.cafe/users/mjack/statuses/116165110767717833",
"activitypub"
],
[
"client",
"Mostr",
"31990:6be38f8c63df7dbf84db7ec4a6e6fbbd8d19dca3b980efad18585c46f04b26f9:mostr",
"wss://relay.ditto.pub"
]
],
"content": "I have a question about Python libraries and testing scope.\n\nIf I'm importing 'serial' in my library, and use it like the following to create a connection to a sensor:\n\n--- start code ---\n\nimport serial\n\nclass Sensor:\n def __init__(self, serial_device):\n\n self.__serial_device = serial_device\n\n try:\n self.__connection = serial.Serial(\n port=serial_device,\n baudrate=9600, \n bytesize=serial.EIGHTBITS,\n parity=serial.PARITY_NONE,\n stopbits=serial.STOPBITS_ONE,\n )\n\n except serial.SerialException:\n print(\"Could not establish serial connection to sensor\")\n\n--- end code ---\n\nhow much testing should I do around the serial connection? Just mock up a few buffers (byte streams), and see how my class handles unexpected input?\n\nOne the one hand, I want to make the library as solid as possible. On the other hand, I don't want to run tests on code I don't control (the library module). I know of the 'mock-serial' utility, but haven't used it.\n\nThe aim is to make a Python version of my Arduino library for the CozIR Ambient CO2 sensor:\n\nhttps://codeberg.org/mjack/ambientCO2/src/branch/develop\n\n#python #unittest #testing #sensors",
"sig": "342bf5e554a92770f56ffbff500699773c1c33bde7b95f9d7cb880de4d7b398bbba065a2057eceada6147dade46206684d963d8cec176acc4bc6c12cfa3faea9"
}