1#!/usr/bin/env python 2# 3# This file is part of pySerial - Cross platform serial port support for Python 4# (C) 2016 Chris Liechti <cliechti@gmx.net> 5# 6# SPDX-License-Identifier: BSD-3-Clause 7""" 8Test PTY related functionality. 9""" 10 11import os 12import sys 13 14try: 15 import pty 16except ImportError: 17 pty = None 18import unittest 19import serial 20 21DATA = b'Hello\n' 22 23@unittest.skipIf(pty is None, "pty module not supported on platform") 24class Test_Pty_Serial_Open(unittest.TestCase): 25 """Test PTY serial open""" 26 27 def setUp(self): 28 # Open PTY 29 self.master, self.slave = pty.openpty() 30 31 def test_pty_serial_open_slave(self): 32 with serial.Serial(os.ttyname(self.slave), timeout=1) as slave: 33 pass # OK 34 35 def test_pty_serial_write(self): 36 with serial.Serial(os.ttyname(self.slave), timeout=1) as slave: 37 with os.fdopen(self.master, "wb") as fd: 38 fd.write(DATA) 39 fd.flush() 40 out = slave.read(len(DATA)) 41 self.assertEqual(DATA, out) 42 43 def test_pty_serial_read(self): 44 with serial.Serial(os.ttyname(self.slave), timeout=1) as slave: 45 with os.fdopen(self.master, "rb") as fd: 46 slave.write(DATA) 47 slave.flush() 48 out = fd.read(len(DATA)) 49 self.assertEqual(DATA, out) 50 51if __name__ == '__main__': 52 sys.stdout.write(__doc__) 53 # When this module is executed from the command-line, it runs all its tests 54 unittest.main() 55