Skip to content

Commit 133be1a

Browse files
committed
MCU8MASS-1290 Correct output for pytest
1 parent 9bbecdb commit 133be1a

File tree

2 files changed

+151
-18
lines changed

2 files changed

+151
-18
lines changed

examples/mqtt_low_power/mqtt_low_power.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void setup() {
4141
LedCtrl.begin();
4242
LedCtrl.startupCycle();
4343

44-
Log.info("Starting MQTT with low power\r\n");
44+
Log.info("Starting MQTT with low power");
4545

4646
// First we retrieve the topics we're going to publish to, here using the
4747
// ECC thingname with AWS
@@ -100,7 +100,7 @@ void loop() {
100100
delay(500);
101101
}
102102

103-
Log.raw(" OK!\r\n");
103+
Log.raw(" OK!");
104104
#endif
105105

106106
char message_to_publish[8] = {0};

test/test_examples.py

Lines changed: 149 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ def example_test_data():
8484
"expectation": "\\[INFO\\] Connecting to operator.{0,}OK!"
8585
},
8686
{
87-
"expectation": "\\[ERROR\\] Error writing command, the response was:"
87+
"expectation": "\\[ERROR\\] Error writing command, the response was:",
88+
"timeout": 20
8889
},
8990
{
90-
"expectation": "+CME ERROR: invalid characters in text string"
91+
"expectation": "\\+CME ERROR: invalid characters in text string"
9192
},
9293
{
9394
"expectation": ""
@@ -96,7 +97,7 @@ def example_test_data():
9697
"expectation": "\\[INFO\\] Received the following ping response:"
9798
},
9899
{
99-
"expectation": "\\d{1},\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3},\\d{1,},\\d{1,}",
100+
"expectation": "\\d{1,},\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3},\\d{1,},\\d{1,}",
100101
"repeat": 4
101102
},
102103
{
@@ -106,7 +107,7 @@ def example_test_data():
106107
"expectation": "\\[INFO\\] Command written successfully, the response was:"
107108
},
108109
{
109-
"expectation": "\\+CEREG: 5,5,\"[a-zA-Z0-9]{1,}\",\"[a-zA-Z0-9]{1,}\",\\d{1,},,,\"[a-zA-Z0-9]{1,}\",\"[a-zA-Z0-9]{1,}\""
110+
"expectation": "\\+CEREG: 5,5,\"[a-zA-Z0-9]{1,}\",\"[a-zA-Z0-9]{1,}\",\\d{1,}"
110111
},
111112
{
112113
"expectation": ""
@@ -142,7 +143,7 @@ def example_test_data():
142143
"expectation": "\\[INFO\\] Initialized ECC"
143144
},
144145
{
145-
"repeat": 3,
146+
"repeat": 4,
146147
"expectation": "\r\n"
147148
},
148149
{
@@ -162,7 +163,7 @@ def example_test_data():
162163
"expectation": "-----END CERTIFICATE-----"
163164
},
164165
{
165-
"repeat": 2,
166+
"repeat": 3,
166167
"expectation": "\r\n"
167168
},
168169
{
@@ -182,6 +183,11 @@ def example_test_data():
182183
"expectation": "-----END CERTIFICATE-----"
183184
}
184185
],
186+
"gpio": [
187+
{
188+
"expectation": "The voltage supplied is: 4\\.[0-9]+"
189+
}
190+
],
185191
"http": [
186192
{
187193
"expectation": "\\[INFO\\] Starting HTTP example"
@@ -278,6 +284,48 @@ def example_test_data():
278284
"expectation": "\\[INFO\\] Response: {"
279285
}
280286
],
287+
"mqtt_low_power": [
288+
{
289+
"expectation": "\\[INFO\\] Starting MQTT with low power"
290+
},
291+
{
292+
"expectation": "\\[INFO\\] Connecting to operator.{0,}OK!"
293+
},
294+
{
295+
"timeout": 90,
296+
"expectation": "\\[INFO\\] Connecting to broker.{0,}OK!"
297+
},
298+
{
299+
"expectation": "\\[INFO\\] Published message: \\d{1,}"
300+
},
301+
{
302+
"expectation": "\\[INFO\\] Entering low power"
303+
},
304+
{
305+
"timeout": 90,
306+
"expectation": "\\[INFO\\] Connecting to operator.{0,}OK!"
307+
},
308+
{
309+
"expectation": "\\[INFO\\] Woke up!"
310+
},
311+
{
312+
"timeout": 90,
313+
"expectation": "\\[INFO\\] Connecting to broker.{0,}OK!"
314+
},
315+
{
316+
"expectation": "\\[INFO\\] Published message: \\d{1,}"
317+
},
318+
{
319+
"expectation": "\\[INFO\\] Entering low power"
320+
},
321+
{
322+
"timeout": 90,
323+
"expectation": "\\[INFO\\] Connecting to operator.{0,}OK!"
324+
},
325+
{
326+
"expectation": "\\[INFO\\] Woke up!"
327+
},
328+
],
281329
"mqtt_password_authentication": [
282330
{
283331
"expectation": "\\[INFO\\] Starting MQTT with username and password example"
@@ -459,6 +507,9 @@ def example_test_data():
459507
{
460508
"expectation": "\\[INFO\\] Board name: [a-z0-9]+"
461509
},
510+
{
511+
"expectation": "\\[INFO\\] Will now connect to the operator. If the board hasn't previously connected to the operator/network, establishing the connection the first time might take some time."
512+
},
462513
{
463514
"expectation": "\\[INFO\\] Connecting to operator.{0,}OK!"
464515
},
@@ -476,6 +527,71 @@ def example_test_data():
476527
"expectation": "\\[INFO\\] Sending heartbeat"
477528
}
478529
],
530+
"serial": [
531+
{
532+
"expectation": "Hello world from Serial3"
533+
},
534+
{
535+
"expectation": "\\[INFO\\] Hello world from Log"
536+
},
537+
{
538+
"expectation": "This is a message without a prefix"
539+
},
540+
{
541+
"expectation": "\\[DEBUG\\] A debug message"
542+
},
543+
{
544+
"expectation": "\\[INFO\\] An info message"
545+
},
546+
{
547+
"expectation": "\\[WARN\\] A warning message"
548+
},
549+
{
550+
"expectation": "\\[ERROR\\] An error message"
551+
},
552+
{
553+
"expectation": ""
554+
},
555+
{
556+
"expectation": "\\[INFO\\] An info message"
557+
},
558+
{
559+
"expectation": "\\[WARN\\] A warning message"
560+
},
561+
{
562+
"expectation": "\\[ERROR\\] An error message"
563+
},
564+
{
565+
"expectation": ""
566+
},
567+
{
568+
"expectation": "\\[WARN\\] A warning message"
569+
},
570+
{
571+
"expectation": "\\[ERROR\\] An error message"
572+
},
573+
{
574+
"expectation": ""
575+
},
576+
{
577+
"expectation": "\\[ERROR\\] An error message"
578+
},
579+
{
580+
"expectation": ""
581+
},
582+
{
583+
"expectation": ""
584+
},
585+
{
586+
"expectation": "\\[INFO\\] This is a number: 10"
587+
},
588+
{
589+
"expectation": "\\[INFO\\] This is a string: Hello world"
590+
},
591+
{
592+
"expectation": "\\[INFO\\] This is a hexadecimal and a string: 1F - Hello world"
593+
}
594+
],
479595
}
480596

481597

@@ -545,25 +661,24 @@ def run_example(request, backend, example_test_data):
545661

546662
assert expectation != None, "Missing expectation in test data"
547663

548-
for i in range(0, repeat):
664+
for _ in range(0, repeat):
549665
if command != None:
550666
command_stripped = command.strip("\r")
551667
print(f"\tTesting command: {command_stripped}")
552668

553669
serial_handle.write(str.encode(command))
554670
serial_handle.flush()
555671

556-
# Read until line feed or timeout
557-
start = time.time()
558-
output = ""
559-
560-
while time.time() - start < timeout and output == "":
561-
output = serial_handle.read_until().decode("utf-8")
672+
serial_handle.timeout = timeout
673+
output = serial_handle.read_until().decode("utf-8")
562674

563-
response = re.search(expectation, output)
675+
try:
676+
response = re.search(expectation, output)
677+
except Exception as exception:
678+
pytest.fail(f"\tRegex error for string \"{expectation}\". The error was: {str(exception)}")
564679

565680
formatted_output = output.replace("\r", "\\r").replace("\n", "\\n")
566-
assert response != None, f"\tDid not get the expected response \"{expectation}\", got: \"{formatted_output}\""
681+
assert response != None, f"\tDid not get the expected response \"{expectation}\" within the timeout of {timeout}, got: \"{formatted_output}\""
567682

568683
formatted_response = response.group(0).replace("\r", "\\r").replace("\n", "\\n")
569684

@@ -578,6 +693,11 @@ def run_test(request, backend, session_config, example_test_data):
578693

579694
# ------------------------------- TESTS -----------------------------------
580695

696+
697+
def test_custom_at_commands(request, backend, session_config, example_test_data):
698+
run_test(request, backend, session_config, example_test_data)
699+
700+
581701
def test_debug_modem(request, backend, session_config, example_test_data):
582702
run_test(request, backend, session_config, example_test_data)
583703

@@ -586,6 +706,10 @@ def test_extract_certificates(request, backend, session_config, example_test_dat
586706
run_test(request, backend, session_config, example_test_data)
587707

588708

709+
def test_gpio(request, backend, session_config, example_test_data):
710+
run_test(request, backend, session_config, example_test_data)
711+
712+
589713
def test_http(request, backend, session_config, example_test_data):
590714
run_test(request, backend, session_config, example_test_data)
591715

@@ -602,7 +726,7 @@ def test_https_with_header(request, backend, session_config, example_test_data):
602726
run_test(request, backend, session_config, example_test_data)
603727

604728

605-
def test_https_configure_ca(request, backend, session_config, example_test_data):
729+
def test_mqtt_low_power(request, backend, session_config, example_test_data):
606730
run_test(request, backend, session_config, example_test_data)
607731

608732

@@ -618,6 +742,10 @@ def test_mqtt_polling_aws(request, backend, session_config, example_test_data):
618742
run_test(request, backend, session_config, example_test_data)
619743

620744

745+
def test_mqtt_with_connection_loss_handling(request, backend, session_config, example_test_data):
746+
run_test(request, backend, session_config, example_test_data)
747+
748+
621749
def test_power_down(request, backend, session_config, example_test_data):
622750
run_test(request, backend, session_config, example_test_data)
623751

@@ -632,3 +760,8 @@ def test_power_save(request, backend, session_config, example_test_data):
632760

633761
def test_sandbox(request, backend, session_config, example_test_data):
634762
run_test(request, backend, session_config, example_test_data)
763+
764+
765+
def test_serial(request, backend, session_config, example_test_data):
766+
run_test(request, backend, session_config, example_test_data)
767+

0 commit comments

Comments
 (0)