Best Python code snippet using autotest_python
UTSendingDataRtM.py
Source:UTSendingDataRtM.py  
1# UT to test MCU ability to communicate2from mcuserial_python import SerialClient3from mcuserial_python import mainTranslator as mt4import time5mcu_serial_interface = SerialClient('/dev/ttyUSB0', 115200, 0.2, 2.0) # 0.2 2.06def send_my_dat(data):7    recv = []8    for element in data:9        mcu_serial_interface.send(element)10        msg_bytes = mcu_serial_interface.receive()11        recv.append(msg_bytes)12    return recv13#comerr_regNotFound = 0x1, comerr_illegalAccess = 0x2, comerr_badFunction = 0x3, comerr_dataMissing = 0x414# WHEN WRITING DATA, MINIMUM IS ALWAYS 4 BYTES, BECAUSE REGISTERS (TO VALIDATE NOW)15def test_mcu_min_bytes():16    test_success = True17    packet = bytearray()18    packet.append(0x77)19    packet.append(7)20    packet.append(0x04)21    packet.append(0x50)22    packet.append(0x0)23    packet.append(0x1)24    packet.append(0x1)25    packet.append(0x7B)26    packet.append(0x87)27    recv = send_my_dat([packet])28    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])29    _, nack_code = mt.extract_data_and_convert(0x4, data_out)30    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == False and is_nack == True and nack_code[0] == 0x431    if test_success:32        print("[SUCCESS] : MCU handles bad data formatting (1 byte instead of 4)")33    else:34        print("[ERROR] : MCU doesn't handle bad data formatting (1 byte instead of 4)")35    return test_success36# TEST BAD CRC37def test_bad_crc():38    test_success = True39    packet = bytearray()40    packet.append(0x77)41    packet.append(0x0a)42    packet.append(0x04)43    packet.append(0x50)44    packet.append(0x0)45    packet.append(0x1)46    packet.append(0x1)47    packet.append(0x0)48    packet.append(0x0)49    packet.append(0x0)50    packet.append(0xf4)51    packet.append(0xc6)52    recv = send_my_dat([packet])53    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])54    test_success = test_success and is_correct == 055    if test_success:56        print("[SUCCESS] : MCU handles bad crc from request")57    else:58        print("[ERROR] : MCU doesn't handle bad crc from request")59    return test_success60# TEST DATA MISSING61def test_mcu_data_missing():62    test_success = True63    data = [mt.message_constructor(0x10, 0, 0x1, [1], None, None, True)]64    recv = send_my_dat(data)65    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])66    _, nack_code = mt.extract_data_and_convert(0x9, data_out)67    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == False and is_nack == True and nack_code[0] == 0x468    data = [mt.message_constructor(0x50, 0, 0x4, [], 0x0, 0x1, True)]69    recv = send_my_dat(data)70    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])71    _, nack_code = mt.extract_data_and_convert(0x4, data_out)72    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == False and is_nack == True and nack_code[0] == 0x473    data = [mt.message_constructor(0x50, 0, 0x4, [1], 0x0, 0x2, True)]74    recv = send_my_dat(data)75    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])76    _, nack_code = mt.extract_data_and_convert(0x4, data_out)77    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == False and is_nack == True and nack_code[0] == 0x478    if test_success:79        print("[SUCCESS] : MCU handles missing data from request")80    else:81        print("[ERROR] : MCU doesn't handle missing data from request")82    return test_success83def test_mcu_fct_number():84    test_success = True85    data = [mt.message_constructor(0x10, 0, 0x1, [], None, None, False)]86    recv = send_my_dat(data)87    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])88    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False89    data = [mt.message_constructor(0x10, 0, 0x2, [1], None, None, True)]90    recv = send_my_dat(data)91    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])92    _, nack_code = mt.extract_data_and_convert(0x9, data_out)93    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == False and is_nack == True and nack_code[0] == 0x294    data = [mt.message_constructor(0x10, 0, 0x3, [], 0x0, 0x1, False)]95    recv = send_my_dat(data)96    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])97    _, nack_code = mt.extract_data_and_convert(0x4, data_out)98    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == False and is_nack == True and nack_code[0] == 0x299    data = [mt.message_constructor(0x10, 0, 0x4, [10], 0x0, 0x1, True)]100    recv = send_my_dat(data)101    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])102    _, nack_code = mt.extract_data_and_convert(0x4, data_out)103    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == False and is_nack == True and nack_code[0] == 0x2104    data = [mt.message_constructor(0x30, 0, 0x1, [], None, None, False)]105    recv = send_my_dat(data)106    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])107    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False108    data = [mt.message_constructor(0x30, 0, 0x2, [1], None, None, True)]109    recv = send_my_dat(data)110    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])111    _, nack_code = mt.extract_data_and_convert(0x9, data_out)112    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == False and is_nack == True and nack_code[0] == 0x2113    data = [mt.message_constructor(0x30, 0, 0x3, [], 0x0, 0x1, False)]114    recv = send_my_dat(data)115    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])116    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False117    data = [mt.message_constructor(0x30, 0, 0x4, [10], 0x0, 0x1, True)]118    recv = send_my_dat(data)119    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])120    _, nack_code = mt.extract_data_and_convert(0x4, data_out)121    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == False and is_nack == True and nack_code[0] == 0x2122    data = [mt.message_constructor(0x50, 0, 0x1, [], None, None, False)]123    recv = send_my_dat(data)124    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])125    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False126    data = [mt.message_constructor(0x50, 0, 0x2, [1], None, None, True)]127    recv = send_my_dat(data)128    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])129    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False130    data = [mt.message_constructor(0x50, 0, 0x3, [], 0x0, 0x1, False)]131    recv = send_my_dat(data)132    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])133    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False134    data = [mt.message_constructor(0x50, 0, 0x4, [1], 0x0, 0x1, True)]135    recv = send_my_dat(data)136    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])137    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False138    data = [mt.message_constructor(0x70, 0, 0x1, [], None, None, False)]139    recv = send_my_dat(data)140    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])141    _, nack_code = mt.extract_data_and_convert(0x9, data_out)142    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == False and is_nack == True and nack_code[0] == 0x2143    data = [mt.message_constructor(0x70, 0, 0x2, [mt.rmt.password_for_admin_fct_mcu], None, None, True)]144    recv = send_my_dat(data)145    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])146    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False147    data = [mt.message_constructor(0x70, 0, 0x3, [], 0x0, 0x1, False)]148    recv = send_my_dat(data)149    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])150    _, nack_code = mt.extract_data_and_convert(0x4, data_out)151    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == False and is_nack == True and nack_code[0] == 0x2152    data = [mt.message_constructor(0x70, 0, 0x4, [1], 0x0, 0x1, True)]153    recv = send_my_dat(data)154    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])155    _, nack_code = mt.extract_data_and_convert(0x4, data_out)156    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == False and is_nack == True and nack_code[0] == 0x2157    data = [mt.message_constructor(0x10, 0, 0x5, [], None, None, False)]158    recv = send_my_dat(data)159    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])160    _, nack_code = mt.extract_data_and_convert(0x9, data_out)161    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == False and is_nack == True and nack_code[0] == 0x3162    data = [mt.message_constructor(0x10, 0, 0xb, [], None, None, False)]163    recv = send_my_dat(data)164    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])165    _, nack_code = mt.extract_data_and_convert(0x9, data_out)166    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == False and is_nack == True and nack_code[0] == 0x3167    data = [mt.message_constructor(0x10, 0, 0xc, [], None, None, False)]168    recv = send_my_dat(data)169    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])170    _, nack_code = mt.extract_data_and_convert(0x9, data_out)171    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == False and is_nack == True and nack_code[0] == 0x3172    data = [mt.message_constructor(0x10, 0, 0xa, [], None, None, False)]173    recv = send_my_dat(data)174    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])175    _, nack_code = mt.extract_data_and_convert(0x9, data_out)176    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == False and is_nack == True and nack_code[0] == 0x3177    if test_success:178        print("[SUCCESS] : MCU handles right and wrong function numbers")179    else:180        print("[ERROR] : MCU doesn't handle right and wrong function numbers")181    return test_success182def test_mcu_reg_number(): 183    test_success = True184    data = [mt.message_constructor(0x10, 0, 0x1, [], None, None, False)]185    recv = send_my_dat(data)186    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])187    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False188    data = [mt.message_constructor(0x01, 0, 0x1, [], None, None, False)]189    recv = send_my_dat(data)190    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])191    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False192    data = [mt.message_constructor(0x02, 0, 0x1, [], None, None, False)]193    recv = send_my_dat(data)194    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])195    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False196    data = [mt.message_constructor(0x03, 0, 0x1, [], None, None, False)]197    recv = send_my_dat(data)198    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])199    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False200    data = [mt.message_constructor(0x30, 0, 0x3, [], 0x0, 0x1, False)]201    recv = send_my_dat(data)202    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])203    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False204    data = [mt.message_constructor(0x50, 0, 0x3, [], 0x0, 0x1, False)]205    recv = send_my_dat(data)206    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])207    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False208    data = [mt.message_constructor(0x51, 0, 0x3, [], 0x0, 0x1, False)]209    recv = send_my_dat(data)210    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])211    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False212    data = [mt.message_constructor(0x60, 0, 0x3, [], 0x0, 0x1, False)]213    recv = send_my_dat(data)214    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])215    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False216    data = [mt.message_constructor(0x74, 0, 0x1, [], None, None, False)]217    recv = send_my_dat(data)218    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])219    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False220    data = [mt.message_constructor(0x35, 1, 0x1, [], None, None, False)]221    recv = send_my_dat(data)222    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])223    _, nack_code = mt.extract_data_and_convert(0x9, data_out)224    test_success = test_success and is_correct == 1 and seq_num == 1 and is_ack == False and is_nack == True and nack_code[0] == 0x1225    data = [mt.message_constructor(0x22, 2, 0x1, [], None, None, False)]226    recv = send_my_dat(data)227    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])228    _, nack_code = mt.extract_data_and_convert(0x9, data_out)229    test_success = test_success and is_correct == 1 and seq_num == 2 and is_ack == False and is_nack == True and nack_code[0] == 0x1230    if test_success:231        print("[SUCCESS] : MCU handles right and wrong register numbers")232    else:233        print("[ERROR] : MCU doesn't handle right and wrong register numbers")234    return test_success235# TEST IF MCU_NUM_SEQ IS CONSIDERED236def test_mcu_num_seq():237    test_success = True238    data = [mt.message_constructor(0x10, 0, 0x1, [], None, None, False)]239    recv = send_my_dat(data)240    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])241    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False242    data = [mt.message_constructor(0x10, 9, 0x1, [], None, None, False)]243    recv = send_my_dat(data)244    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])245    test_success = test_success and is_correct == 1 and seq_num == 9 and is_ack == True and is_nack == False246    data = [mt.message_constructor(0x10, 15, 0x1, [], None, None, False)]247    recv = send_my_dat(data)248    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv[0])249    test_success = test_success and is_correct == 1 and seq_num == 15 and is_ack == True and is_nack == False250    if test_success:251        print("[SUCCESS] : MCU handles different sequence numbers")252    else:253        print("[ERROR] : MCU doesn't handle different sequence numbers")254    return test_success255# TEST NOISE ON LINE256def test_noise_on_line():257    test_success = True258    data = [bytearray(b'0000w\x04\x01\x03H\n0000'), bytearray(b'w\x04\x01\x03H\n0000'), bytearray(b'0000w\x04\x01\x03H\n'), bytearray(b'00w\x04\x0100000000000000w\x04\x01\x03H\n0000')]259    recv = send_my_dat(data)260    for dat in recv:261        is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(dat)262        test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False263    data1 = bytearray(b'w\x04\x01')264    data2 = bytearray(b'\x03H\n')265    mcu_serial_interface.send(data1)266    mcu_serial_interface.send(data2)267    recv = mcu_serial_interface.receive()268    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv)269    test_success = test_success and is_correct == 1 and seq_num == 0 and is_ack == True and is_nack == False270    mcu_serial_interface.send(data1)271    time.sleep(0.1)272    mcu_serial_interface.send(data2)273    recv = mcu_serial_interface.receive()274    is_correct, seq_num, is_ack, is_nack, data_out = mt.parse_correct_data(recv)275    test_success = test_success and is_correct == 0276    if test_success:277        print("[SUCCESS] : MCU handles noise on line")278    else:279        print("[ERROR] : MCU doesn't handle correctly noise on line")280    return test_success281number_of_total_fct = 7282number_of_fct_success = 0283number_of_fct_success = number_of_fct_success + int(test_noise_on_line())284number_of_fct_success = number_of_fct_success + int(test_mcu_num_seq())285number_of_fct_success = number_of_fct_success + int(test_mcu_reg_number())286number_of_fct_success = number_of_fct_success + int(test_mcu_fct_number())287number_of_fct_success = number_of_fct_success + int(test_mcu_data_missing())288number_of_fct_success = number_of_fct_success + int(test_bad_crc())289number_of_fct_success = number_of_fct_success + int(test_mcu_min_bytes())290print("\nIn summary:")291print(str(number_of_fct_success) + " functions have succeeded out of " + str(number_of_total_fct) + " functions.")...TestFile.py
Source:TestFile.py  
1import Contact2import ContactManager3def Test_Auto(name, address, phoneNumber, birthday):4    """Automatically tests all the ContactManager functions, using the input name, address, phoneNumber, birthday"""5    test_success = True6    test_name = name7    test_address = address8    test_phonenumber = phoneNumber9    test_birthday = birthday10################ Testing AddContact #################11    print("Adding test contact "+ test_name +" : "+ test_address +" : "+ test_phonenumber + " : "+ test_birthday)12    test_contact_manager = ContactManager.ContactManager()13    test_contact_manager.Add_New_Contact(test_name, test_address, test_phonenumber, test_birthday)14    test_ID = test_contact_manager.Get_ContactList_Length() - 115    if test_contact_manager.Get_Contact(test_ID).Get_Name() == test_name:16        print("Last contact added is "+ test_contact_manager.Get_Contact(test_ID).Get_Name())17        print("Test contact added successfully...")18    else:19        print("! Last contact added is named "+test_contact_manager.Get_Contact(test_ID).Get_Name())20        print("! Test contact was not added successfully!..")21        test_success = False22################ Search_Contact_Name ###################23    print("Searching for the test contact "+test_name+" by each detail...")24    test_search = test_contact_manager.Search_Contact_Name(test_name)25    if len(test_search) == 1:26        if test_search[0][1] == test_name:27            print("successfully found the test contact when searching by name, it is: " + test_search[0][1] + "...")28        else:29            print("! Searching by name returned a contact, but not the test contact!..")30            test_success = False31    else:32        print("! Wrong number of contacts found when searching by name, should only be 1!..")33        test_success = False34############### Search_Contact_Address ##################35    test_search = test_contact_manager.Search_Contact_Address(test_address)36    if len(test_search) == 1:37        if test_search[0][2] == test_address:38            print("successfully found the test contact when searching by address, it is: " + test_search[0][2] + "...")39        else:40            print("! Searching by address returned a contact, but not the test contact!..")41            test_success = False42    else:43        print("! Wrong number of contacts found when searching by address, should only be 1!..")44        test_success = False45############### Search_Contact_PhoneNumber ##################46    test_search = test_contact_manager.Search_Contact_PhoneNumber(test_phonenumber)47    if len(test_search) == 1:48        if test_search[0][3] == test_phonenumber:49            print("successfully found the test contact when searching by phoneNumber, it is: " + test_search[0][3] + "...")50        else:51            print("! Searching by phone number returned a contact, but not the test contact!..")52            test_success = False53    else:54        print("! Wrong number of contacts found when searching by phone number, should only be 1!..")55        test_success = False56############### Search_Contact_Birthday ##################57    test_search = test_contact_manager.Search_Contact_Birthday(test_birthday)58    if len(test_search) == 1:59        if test_search[0][4] == test_birthday:60            print("successfully found the test contact when searching by birthday, it is: " + test_search[0][4] + "...")61        else:62            print("! Searching by birthday returned a contact, but not the test contact!..")63            test_success = False64    else:65        print("! Wrong number of contacts found when searching by birthday, should only be 1!..")66        test_success = False67################ Testing EditContact #################68    edit_name = test_name+"Test"69    test_contact_manager.Edit_Contact_Name(test_ID, edit_name)70    if test_contact_manager.Get_Contact(test_ID).Get_Name() == edit_name:71        print("Test contact name has been correctly edited to ("+edit_name+"), it is now (" +test_contact_manager.Get_Contact(test_ID).Get_Name()+ ")...")72    else:73        print("! Test contact name has not been correctly edited!..")74        print(test_contact_manager.Get_Contact(test_ID).Get_Name())75        test_success = False76    edit_address = test_address+"Test"77    test_contact_manager.Edit_Contact_Address(test_ID, edit_address)78    if test_contact_manager.Get_Contact(test_ID).Get_Address() == edit_address:79        print("Test contact address has been correctly edited to ("+edit_address+"), it is now (" +test_contact_manager.Get_Contact(test_ID).Get_Address()+ ")...")80    else:81        print("! Test contact address has not been correctly edited!..")82        print(test_contact_manager.Get_Contact(test_ID).Get_Address())83        test_success = False84    edit_phonenumber = "0102030405"85    test_contact_manager.Edit_Contact_PhoneNumber(test_ID, edit_phonenumber)86    if test_contact_manager.Get_Contact(test_ID).Get_PhoneNumber() == edit_phonenumber:87        print("Test contact phone number has been correctly edited to ("+edit_phonenumber+"), it is now (" +test_contact_manager.Get_Contact(test_ID).Get_PhoneNumber()+ ")...")88    else:89        print("! Test contact phone number has not been correctly edited!..")90        print(test_contact_manager.Get_Contact(test_ID).Get_PhoneNumber())91        test_success = False92    edit_birthday = "01/02/0304"93    test_contact_manager.Edit_Contact_Birthday(test_ID, edit_birthday)94    if test_contact_manager.Get_Contact(test_ID).Get_Birthday() == edit_birthday:95        print("Test contact birthday has been correctly edited to " + edit_birthday + ", it is now (" +test_contact_manager.Get_Contact(test_ID).Get_Birthday()+ ")...")96    else:97        print("! Test contact birthday has not been correctly edited!..")98        print(test_contact_manager.Get_Contact(test_ID).Get_Birthday())99        test_success = False100    ############ Deleting a Contact ############101    test_contact_manager.Delete_Contact(test_contact_manager.Get_ContactList_Length()-1)102    if test_contact_manager.Get_Contact(test_contact_manager.Get_ContactList_Length()-1).Get_Name() != edit_name: ##Is the last contact the same as the edited test one?103        print("Contact successfully deleted, the contact at the end of the list is now " + test_contact_manager.Get_Contact(test_contact_manager.Get_ContactList_Length()-1).Get_Name())104    else:105        print("! Contact has not been deleted successfully!..")106        test_success = False107    print("End of test.")108    print("")109    return test_success110if __name__ == "__main__":111    test_success = True112    try:113        print("This test should pass...")114        if not Test_Auto("David Tester", "4 Test Rd, Teston", "0192837465", "01/23/1986"): # Normal115            test_success = False116    except:117        print("An error was encountered during the normal test...")118        test_success = False119    try:120        print("This test should pass...")121        if not Test_Auto("MY NAME IS NUMBERS ¬¬¬¬ 9999999999999999999", "999999999999999999999999999999999999rfrv99gr99999999999999999", "9999999999", "99/99/9999"): # Upper bound122            test_success = False123    except:124        print("An error was encountered during the upper test...")125        test_success = False126    try:127        print("This test should pass...")128        if not Test_Auto("", "", "0000000000", "00/00/0000"): # Lower bound129            test_success = False130    except:131        print("An error was encountered during the lower test...")132        test_success = False133    try:134        print("This test should fail...")135        if Test_Auto("'-1'", "-'2", "phone", "secret"): # Outer bound136            test_success = False137    except:138        print("An error was encountered during the outer test...")139        #test_success = False140    if test_success:141        print("Tests all went as expected...")142    else:143        print("A test has failed at some point...")...UTSendingDataMtR_R.py
Source:UTSendingDataMtR_R.py  
1import rospy2import struct3from mcuserial_msgs.srv import alim_serial_com_srv4# Send read command5rospy.init_node("mcuserial_node_test")6rospy.wait_for_service('alim_serial_com')7def test_bad_data(alim_serial_service):8    test_success = True9    resp = alim_serial_service(5, [0], [])10    test_success = test_success and resp.is_error == 1 and resp.error_code == 511    resp = alim_serial_service(5, [0], [])12    test_success = test_success and resp.floats[0] == 0x1 and resp.is_error == 0 and resp.error_code == 013    if test_success:14        print("[SUCCESS] : ROS handles bad data")15    else:16        print("[ERROR] : ROS doesn't handle bad data")17    return test_success18def test_missing_data(alim_serial_service):19    test_success = True20    resp = alim_serial_service(3, [], [])21    test_success = test_success and resp.is_error == 1 and resp.error_code == 522    resp = alim_serial_service(5, [0], [])23    test_success = test_success and resp.floats[0] == 0x1 and resp.is_error == 0 and resp.error_code == 024    resp = alim_serial_service(5, [0], [])25    test_success = test_success and resp.is_error == 1 and resp.error_code == 526    resp = alim_serial_service(5, [0], [])27    test_success = test_success and resp.is_error == 1 and resp.error_code == 528    resp = alim_serial_service(5, [0], [])29    test_success = test_success and resp.is_error == 1 and resp.error_code == 530    if test_success:31        print("[SUCCESS] : ROS handles missing data")32    else:33        print("[ERROR] : ROS doesn't handle missing data")34    return test_success35def test_bad_crc(alim_serial_service):36    test_success = True37    resp = alim_serial_service(3, [], [])38    test_success = test_success and resp.is_error == 1 and resp.error_code == 0x539    if test_success:40        print("[SUCCESS] : ROS handles bad crc")41    else:42        print("[ERROR] : ROS doesn't handle bad crc")43    return test_success44def test_noise_on_line(alim_serial_service):45    test_success = True46    for i in range(6):47        resp = alim_serial_service(3, [], [])48        test_success = test_success and struct.pack("f", resp.floats[0]) == b'\x9a\x19@C' and resp.is_error == 0 and resp.error_code == 049    resp = alim_serial_service(3, [], [])50    test_success = test_success and resp.is_error == 1 and resp.error_code == 0x551    if test_success:52        print("[SUCCESS] : ROS handles noise on line")53    else:54        print("[ERROR] : ROS doesn't handle correctly noise on line")55    return test_success56try:57        58        alim_serial_service = rospy.ServiceProxy('alim_serial_com', alim_serial_com_srv)59        60        number_of_total_fct = 461        number_of_fct_success = 062        number_of_fct_success = number_of_fct_success + int(test_noise_on_line(alim_serial_service))63        number_of_fct_success = number_of_fct_success + int(test_bad_crc(alim_serial_service))64        number_of_fct_success = number_of_fct_success + int(test_missing_data(alim_serial_service))65        number_of_fct_success = number_of_fct_success + int(test_bad_data(alim_serial_service))66        print("\nIn summary:")67        print(str(number_of_fct_success) + " functions have succeeded out of " + str(number_of_total_fct) + " functions.")68        print("Check prints for errors or successes.")69except rospy.ServiceException as e:...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
