66from common import running_on_ci
77message = []
88
9+
910def test_ws_connection (socketio ):
1011 print ('my sid is' , socketio .sid )
1112 assert socketio .sid is not None
1213
13- def test_list (socketio ):
14- global message
15- socketio .on ('message' , message_handler )
14+
15+ def test_list (socketio , message ):
1616 socketio .emit ('command' , 'list' )
1717 time .sleep (.2 )
1818 print (message )
1919 assert any ("list" in i for i in message )
2020 assert any ("Ports" in i for i in message )
2121 assert any ("Network" in i for i in message )
2222
23+
2324# NOTE run the following tests with a board connected to the PC
2425@pytest .mark .skipif (
2526 running_on_ci (),
2627 reason = "VMs have no serial ports" ,
2728)
28- def test_open_serial_default (socketio , serial_port , baudrate ):
29- general_open_serial (socketio , serial_port , baudrate , "default" )
29+ def test_open_serial_default (socketio , serial_port , baudrate , message ):
30+ general_open_serial (socketio , serial_port , baudrate , message , "default" )
3031
3132
3233@pytest .mark .skipif (
3334 running_on_ci (),
3435 reason = "VMs have no serial ports" ,
3536)
36- def test_open_serial_timed (socketio , serial_port , baudrate ):
37- general_open_serial (socketio , serial_port , baudrate , "timed" )
37+ def test_open_serial_timed (socketio , serial_port , baudrate , message ):
38+ general_open_serial (socketio , serial_port , baudrate , message , "timed" )
3839
3940
4041@pytest .mark .skipif (
4142 running_on_ci (),
4243 reason = "VMs have no serial ports" ,
4344)
44- def test_open_serial_timedraw (socketio , serial_port , baudrate ):
45- general_open_serial (socketio , serial_port , baudrate , "timedraw" )
45+ def test_open_serial_timedraw (socketio , serial_port , baudrate , message ):
46+ general_open_serial (socketio , serial_port , baudrate , message , "timedraw" )
4647
4748
4849# NOTE run the following tests with a board connected to the PC and with the sketch found in test/testdata/SerialEcho.ino on it be sure to change serial_address in conftest.py
4950@pytest .mark .skipif (
5051 running_on_ci (),
5152 reason = "VMs have no serial ports" ,
5253)
53- def test_send_serial_default (socketio , close_port , serial_port , baudrate ):
54- general_send_serial (socketio , close_port , serial_port , baudrate , "default" )
54+ def test_send_serial_default (socketio , close_port , serial_port , baudrate , message ):
55+ general_send_serial (socketio , close_port , serial_port , baudrate , message , "default" )
5556
5657
5758@pytest .mark .skipif (
5859 running_on_ci (),
5960 reason = "VMs have no serial ports" ,
6061)
61- def test_send_serial_timed (socketio , close_port , serial_port , baudrate ):
62- general_send_serial (socketio , close_port , serial_port , baudrate , "timed" )
62+ def test_send_serial_timed (socketio , close_port , serial_port , baudrate , message ):
63+ general_send_serial (socketio , close_port , serial_port , baudrate , message , "timed" )
6364
6465
6566@pytest .mark .skipif (
6667 running_on_ci (),
6768 reason = "VMs have no serial ports" ,
6869)
69- def test_send_serial_timedraw (socketio , close_port , serial_port , baudrate ):
70- general_send_serial (socketio , close_port , serial_port , baudrate , "timedraw" )
70+ def test_send_serial_timedraw (socketio , close_port , serial_port , baudrate , message ):
71+ general_send_serial (socketio , close_port , serial_port , baudrate , message , "timedraw" )
7172
7273
73- def general_open_serial (socketio , serial_port , baudrate , buffertype ):
74- global message
75- message = []
76- # in message var we will find the "response"
77- socketio .on ('message' , message_handler )
78- socketio .emit ('command' , 'open ' + serial_port + ' ' + baudrate + ' ' + buffertype )
79- # give time to the message var to be filled
80- time .sleep (.5 )
81- print (message )
82- # the serial connection should be open now
83- assert any ("\" IsOpen\" : true" in i for i in message )
74+ @pytest .mark .skipif (
75+ running_on_ci (),
76+ reason = "VMs have no serial ports" ,
77+ )
78+ def test_send_emoji_serial_default (socketio , close_port , serial_port , baudrate , message ):
79+ general_send_emoji_serial (socketio , close_port , serial_port , baudrate , message , "default" )
80+
81+
82+ @pytest .mark .skipif (
83+ running_on_ci (),
84+ reason = "VMs have no serial ports" ,
85+ )
86+ def test_send_emoji_serial_timed (socketio , close_port , serial_port , baudrate , message ):
87+ general_send_emoji_serial (socketio , close_port , serial_port , baudrate , message , "timed" )
88+
89+
90+ @pytest .mark .skipif (
91+ running_on_ci (),
92+ reason = "VMs have no serial ports" ,
93+ )
94+ def test_send_emoji_serial_timedraw (socketio , close_port , serial_port , baudrate , message ):
95+ general_send_emoji_serial (socketio , close_port , serial_port , baudrate , message , "timedraw" )
8496
85- # close the serial port
97+
98+ def general_open_serial (socketio , serial_port , baudrate , message , buffertype ):
99+ open_serial_port (socketio , serial_port , baudrate , message , buffertype )
100+ # test the closing of the serial port, we are gonna use close_port for the other tests
86101 socketio .emit ('command' , 'close ' + serial_port )
87102 time .sleep (.2 )
88103 print (message )
89104 #check if port has been closed
90105 assert any ("\" IsOpen\" : false," in i for i in message )
91106
92107
93-
94- def general_send_serial (socketio , close_port , serial_port , baudrate , buffertype ):
95- global message
96- message = []
97- #in message var we will find the "response"
98- socketio .on ('message' , message_handler )
99- #open a new serial connection with the specified buffertype, if buffertype is empty it will use the default one
100- socketio .emit ('command' , 'open ' + serial_port + ' ' + baudrate + ' ' + buffertype )
101- # give time to the message var to be filled
102- time .sleep (.5 )
103- print (message )
104- # the serial connection should be open now
105- assert any ("\" IsOpen\" : true" in i for i in message )
106-
107- #test with string
108+ def general_send_serial (socketio , close_port , serial_port , baudrate , message , buffertype ):
109+ open_serial_port (socketio , serial_port , baudrate , message , buffertype )
108110 # send the string "ciao" using the serial connection
109- socketio .emit ('command' , 'send ' + serial_port + ' /" ciao/" ' )
111+ socketio .emit ('command' , 'send ' + serial_port + ' ciao' )
110112 time .sleep (1 )
111113 print (message )
112114 # check if the send command has been registered
113- assert any ("send " + serial_port + " / \" ciao/ \" " in i for i in message )
115+ assert any ("send " + serial_port + " ciao" in i for i in message )
114116 #check if message has been sent back by the connected board
115117 if buffertype == "timedraw" :
116118 output = decode_output (extract_serial_data (message ))
117119 elif buffertype in ("default" , "timed" ):
118120 output = extract_serial_data (message )
119121 assert "ciao" in output
122+ # the serial connection is closed by close_port() fixture: even if in case of test failure
120123
121- #test with emoji
122- message = [] # reinitialize the message buffer to have a clean situation
124+
125+ def general_send_emoji_serial (socketio , close_port , serial_port , baudrate , message , buffertype ):
126+ open_serial_port (socketio , serial_port , baudrate , message , buffertype )
123127 # send a lot of emoji: they can be messed up
124128 socketio .emit ('command' , 'send ' + serial_port + ' /"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/"' )
125- time .sleep (.5 )
129+ time .sleep (1 )
126130 print (message )
127131 # check if the send command has been registered
128132 assert any ("send " + serial_port + " /\" 🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\" " in i for i in message )
@@ -133,23 +137,23 @@ def general_send_serial(socketio, close_port, serial_port, baudrate, buffertype)
133137 assert "/\" 🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\" " in output
134138 # the serial connection is closed by close_port() fixture: even if in case of test failure
135139
136- @pytest .mark .skipif (
137- running_on_ci (),
138- reason = "VMs have no serial ports" ,
139- )
140- def test_sendraw_serial (socketio , close_port , serial_port , baudrate ):
141- global message
142- message = []
143- #in message var we will find the "response"
144- socketio .on ('message' , message_handler )
145- #open a new serial connection with the specified buffertype, if buffertype is empty it will use the default one
146- socketio .emit ('command' , 'open ' + serial_port + ' ' + baudrate + ' timedraw' )
140+
141+ def open_serial_port (socketio , serial_port , baudrate , message , buffertype ):
142+ #open a new serial connection with the specified buffertype
143+ socketio .emit ('command' , 'open ' + serial_port + ' ' + baudrate + ' ' + buffertype )
147144 # give time to the message var to be filled
148145 time .sleep (.5 )
149146 print (message )
150147 # the serial connection should be open now
151148 assert any ("\" IsOpen\" : true" in i for i in message )
152149
150+
151+ @pytest .mark .skipif (
152+ running_on_ci (),
153+ reason = "VMs have no serial ports" ,
154+ )
155+ def test_sendraw_serial (socketio , close_port , serial_port , baudrate , message ):
156+ open_serial_port (socketio , serial_port , baudrate , message , "timedraw" )
153157 #test with bytes
154158 integers = [1 , 2 , 3 , 4 , 5 ]
155159 bytes_array = bytearray (integers )
@@ -165,12 +169,6 @@ def test_sendraw_serial(socketio, close_port, serial_port, baudrate):
165169 assert encoded_integers in output
166170
167171
168- # callback called by socketio when a message is received
169- def message_handler (msg ):
170- # print('Received message: ', msg)
171- global message
172- message .append (msg )
173-
174172# helper function used to extract serial data from its JSON representation
175173# NOTE make sure to pass a clean message (maybe reinitialize the message global var before populating it)
176174def extract_serial_data (msg ):
0 commit comments