File: test_advanced.py

package info (click to toggle)
pyserial 3.5~b0-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 848 kB
  • sloc: python: 7,979; makefile: 107; sh: 32
file content (161 lines) | stat: -rw-r--r-- 6,037 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
#
# This file is part of pySerial - Cross platform serial port support for Python
# (C) 2001-2015 Chris Liechti <cliechti@gmx.net>
#
# SPDX-License-Identifier:    BSD-3-Clause
"""\
Some tests for the serial module.
Part of pyserial (http://pyserial.sf.net)  (C)2002 cliechti@gmx.net

Intended to be run on different platforms, to ensure portability of
the code.

These tests open a serial port and change all the settings on the fly.
If the port is really correctly configured cannot be determined - that
would require external hardware or a null modem cable and an other
serial port library... Thus it mainly tests that all features are
correctly implemented and that the interface does what it should.

"""

import unittest
import serial

# on which port should the tests be performed:
PORT = 'loop://'


class Test_ChangeAttributes(unittest.TestCase):
    """Test with timeouts"""

    def setUp(self):
        # create a closed serial port
        self.s = serial.serial_for_url(PORT, do_not_open=True)

    def tearDown(self):
        self.s.close()

    def test_PortSetting(self):
        self.s.port = PORT
        self.assertEqual(self.s.portstr.lower(), PORT.lower())
        # test internals
        self.assertEqual(self.s._port, PORT)
        # test on the fly change
        self.s.open()
        self.assertTrue(self.s.isOpen())

    def test_DoubleOpen(self):
        self.s.open()
        # calling open for a second time is an error
        self.assertRaises(serial.SerialException, self.s.open)

    def test_BaudrateSetting(self):
        self.s.open()
        for baudrate in (300, 9600, 19200, 115200):
            self.s.baudrate = baudrate
            # test get method
            self.assertEqual(self.s.baudrate, baudrate)
            # test internals
            self.assertEqual(self.s._baudrate, baudrate)
        # test illegal values
        for illegal_value in (-300, -1, 'a', None):
            self.assertRaises(ValueError, setattr, self.s, 'baudrate', illegal_value)

    # skip this test as pyserial now tries to set even non standard baud rates.
    # therefore the test can not choose a value that fails on any system.
    def disabled_test_BaudrateSetting2(self):
        # test illegal values, depending on machine/port some of these may be valid...
        self.s.open()
        for illegal_value in (500000, 576000, 921600, 92160):
            self.assertRaises(ValueError, setattr, self.s, 'baudrate', illegal_value)

    def test_BytesizeSetting(self):
        for bytesize in (5, 6, 7, 8):
            self.s.bytesize = bytesize
            # test get method
            self.assertEqual(self.s.bytesize, bytesize)
            # test internals
            self.assertEqual(self.s._bytesize, bytesize)
        # test illegal values
        for illegal_value in (0, 1, 3, 4, 9, 10, 'a', None):
            self.assertRaises(ValueError, setattr, self.s, 'bytesize', illegal_value)

    def test_ParitySetting(self):
        for parity in (serial.PARITY_NONE, serial.PARITY_EVEN, serial.PARITY_ODD):
            self.s.parity = parity
            # test get method
            self.assertEqual(self.s.parity, parity)
            # test internals
            self.assertEqual(self.s._parity, parity)
        # test illegal values
        for illegal_value in (0, 57, 'a', None):
            self.assertRaises(ValueError, setattr, self.s, 'parity', illegal_value)

    def test_StopbitsSetting(self):
        for stopbits in (1, 2):
            self.s.stopbits = stopbits
            # test get method
            self.assertEqual(self.s.stopbits, stopbits)
            # test internals
            self.assertEqual(self.s._stopbits, stopbits)
        # test illegal values
        for illegal_value in (0, 3, 2.5, 57, 'a', None):
            self.assertRaises(ValueError, setattr, self.s, 'stopbits', illegal_value)

    def test_TimeoutSetting(self):
        for timeout in (None, 0, 1, 3.14159, 10, 1000, 3600):
            self.s.timeout = timeout
            # test get method
            self.assertEqual(self.s.timeout, timeout)
            # test internals
            self.assertEqual(self.s._timeout, timeout)
        # test illegal values
        for illegal_value in (-1, 'a'):
            self.assertRaises(ValueError, setattr, self.s, 'timeout', illegal_value)

    def test_XonXoffSetting(self):
        for xonxoff in (True, False):
            self.s.xonxoff = xonxoff
            # test get method
            self.assertEqual(self.s.xonxoff, xonxoff)
            # test internals
            self.assertEqual(self.s._xonxoff, xonxoff)
        # no illegal values here, normal rules for the boolean value of an
        # object are used thus all objects have a truth value.

    def test_RtsCtsSetting(self):
        for rtscts in (True, False):
            self.s.rtscts = rtscts
            # test get method
            self.assertEqual(self.s.rtscts, rtscts)
            # test internals
            self.assertEqual(self.s._rtscts, rtscts)
        # no illegal values here, normal rules for the boolean value of an
        # object are used thus all objects have a truth value.

    # this test does not work anymore since serial_for_url that is used
    # now, already sets a port
    def disabled_test_UnconfiguredPort(self):
        # an unconfigured port cannot be opened
        self.assertRaises(serial.SerialException, self.s.open)

    def test_PortOpenClose(self):
        for i in range(3):
            # open the port and check flag
            self.assertTrue(not self.s.isOpen())
            self.s.open()
            self.assertTrue(self.s.isOpen())
            self.s.close()
            self.assertTrue(not self.s.isOpen())


if __name__ == '__main__':
    import sys
    sys.stdout.write(__doc__)
    if len(sys.argv) > 1:
        PORT = sys.argv[1]
    sys.stdout.write("Testing port: {!r}\n".format(PORT))
    sys.argv[1:] = ['-v']
    # When this module is executed from the command-line, it runs all its tests
    unittest.main()