Best Python code snippet using stestr_python
gps_classXML_save_results.py
Source:gps_classXML_save_results.py  
1import json2import os3import matplotlib.pyplot as plt4import time5def save_results_test_get_tipo(tipo,expected_tipo,case):6    results = {7        case: {8            "expected_results": {9                "tipo": expected_tipo10            },11            "obtained_results": {12                "tipo": tipo13            }14        }15    }16    if os.path.isdir('./output_tests') == False:17        os.mkdir('./output_tests')18    out_file = open("./output_tests/results_test_get_tipo.json", "a")19    json.dump(results, out_file, indent=4)20    out_file.close()21def save_results_test_set_tipo(tipo,expected_tipo,case):22    results = {23        case: {24            "expected_results": {25                "tipo": expected_tipo26            },27            "obtained_results": {28                "tipo": tipo29            }30        }31    }32    if os.path.isdir('./output_tests') == False:33        os.mkdir('./output_tests')34    out_file = open("./output_tests/results_test_set_tipo.json", "a")35    json.dump(results, out_file, indent=4)36    out_file.close()37def save_results_test_get_autonomia(autonomia, expected_autonomia, case):38    results = {39        case: {40            "expected_results": {41                "autonomia": expected_autonomia42            },43            "obtained_results": {44                "autonomia": autonomia45            }46        }47    }48    if os.path.isdir('./output_tests') == False:49        os.mkdir('./output_tests')50    out_file = open("./output_tests/results_test_get_autonomia.json", "a")51    json.dump(results, out_file, indent=4)52    out_file.close()53def save_results_test_set_autonomia(autonomia, expected_autonomia, case):54    results = {55        case: {56            "expected_results": {57                "autonomia": expected_autonomia58            },59            "obtained_results": {60                "autonomia": autonomia61            }62        }63    }64    if os.path.isdir('./output_tests') == False:65        os.mkdir('./output_tests')66    out_file = open("./output_tests/results_test_set_autonomia.json", "a")67    json.dump(results, out_file, indent=4)68    out_file.close()69def save_results_test_get_provincia(provincia,expected_provincia,case):70    results = {71        case: {72            "expected_results": {73                "provincia": expected_provincia74            },75            "obtained_results": {76                "provincia": provincia77            }78        }79    }80    if os.path.isdir('./output_tests') == False:81        os.mkdir('./output_tests')82    out_file = open("./output_tests/results_test_get_provincia.json", "a")83    json.dump(results, out_file, indent=4)84    out_file.close()85def save_results_test_set_provincia(provincia, expected_provincia, case):86    results = {87        case: {88            "expected_results": {89                "provincia": expected_provincia90            },91            "obtained_results": {92                "provincia": provincia93            }94        }95    }96    if os.path.isdir('./output_tests') == False:97        os.mkdir('./output_tests')98    out_file = open("./output_tests/results_test_set_provincia.json", "a")99    json.dump(results, out_file, indent=4)100    out_file.close()101def save_results_test_get_matricula(matricula,expected_matricula,case):102    results = {103        case: {104            "expected_results": {105                "matricula": expected_matricula106            },107            "obtained_results": {108                "matricula": matricula109            }110        }111    }112    if os.path.isdir('./output_tests') == False:113        os.mkdir('./output_tests')114    out_file = open("./output_tests/results_test_get_matricula.json", "a")115    json.dump(results, out_file, indent=4)116    out_file.close()117def save_results_test_set_matricula(matricula, expected_matricula, case):118    results = {119        case: {120            "expected_results": {121                "matricula": expected_matricula122            },123            "obtained_results": {124                "matricula": matricula125            }126        }127    }128    if os.path.isdir('./output_tests') == False:129        os.mkdir('./output_tests')130    out_file = open("./output_tests/results_test_set_matricula.json", "a")131    json.dump(results, out_file, indent=4)132    out_file.close()133def save_results_test_get_causa(causa,expected_causa,case):134    results = {135        case: {136            "expected_results": {137                "causa": expected_causa138            },139            "obtained_results": {140                "causa": causa141            }142        }143    }144    if os.path.isdir('./output_tests') == False:145        os.mkdir('./output_tests')146    out_file = open("./output_tests/results_test_get_causa.json", "a")147    json.dump(results, out_file, indent=4)148    out_file.close()149def save_results_test_set_causa(causa,expected_causa,case):150    results = {151        case: {152            "expected_results": {153                "causa": expected_causa154            },155            "obtained_results": {156                "causa": causa157            }158        }159    }160    if os.path.isdir('./output_tests') == False:161        os.mkdir('./output_tests')162    out_file = open("./output_tests/results_test_set_causa.json", "a")163    json.dump(results, out_file, indent=4)164    out_file.close()165def save_results_test_get_poblacion(poblacion,expected_poblacion,case):166    results = {167        case: {168            "expected_results": {169                "poblacion": expected_poblacion170            },171            "obtained_results": {172                "poblacion": poblacion173            }174        }175    }176    if os.path.isdir('./output_tests') == False:177        os.mkdir('./output_tests')178    out_file = open("./output_tests/results_test_get_poblacion.json", "a")179    json.dump(results, out_file, indent=4)180    out_file.close()181def save_results_test_set_poblacion(poblacion,expected_poblacion,case):182    results = {183        case: {184            "expected_results": {185                "poblacion": expected_poblacion186            },187            "obtained_results": {188                "poblacion": poblacion189            }190        }191    }192    if os.path.isdir('./output_tests') == False:193        os.mkdir('./output_tests')194    out_file = open("./output_tests/results_test_set_poblacion.json", "a")195    json.dump(results, out_file, indent=4)196    out_file.close()197def save_results_test_get_fechahoraini(fechahoraini,expected_fechahoraini,case):198    results = {199        case: {200            "expected_results": {201                "fechahoraini": expected_fechahoraini202            },203            "obtained_results": {204                "fechahoraini": fechahoraini205            }206        }207    }208    if os.path.isdir('./output_tests') == False:209        os.mkdir('./output_tests')210    out_file = open("./output_tests/results_test_get_fechahoraini.json", "a")211    json.dump(results, out_file, indent=4)212    out_file.close()213def save_results_test_set_fechahoraini(fechahoraini,expected_fechahoraini,case):214    results = {215        case: {216            "expected_results": {217                "fechahoraini": expected_fechahoraini218            },219            "obtained_results": {220                "fechahoraini": fechahoraini221            }222        }223    }224    if os.path.isdir('./output_tests') == False:225        os.mkdir('./output_tests')226    out_file = open("./output_tests/results_test_set_fechahoraini.json", "a")227    json.dump(results, out_file, indent=4)228    out_file.close()229def save_results_test_get_nivel(nivel,expected_nivel,case):230    results = {231        case: {232            "expected_results": {233                "nivel": expected_nivel234            },235            "obtained_results": {236                "nivel": nivel237            }238        }239    }240    if os.path.isdir('./output_tests') == False:241        os.mkdir('./output_tests')242    out_file = open("./output_tests/results_test_get_nivel.json", "a")243    json.dump(results, out_file, indent=4)244    out_file.close()245def save_results_test_set_nivel(nivel,expected_nivel,case):246    results = {247        case: {248            "expected_results": {249                "nivel": expected_nivel250            },251            "obtained_results": {252                "nivel": nivel253            }254        }255    }256    if os.path.isdir('./output_tests') == False:257        os.mkdir('./output_tests')258    out_file = open("./output_tests/results_test_set_nivel.json", "a")259    json.dump(results, out_file, indent=4)260    out_file.close()261def save_results_test_get_carretera(carretera,expected_carretera,case):262    results = {263        case: {264            "expected_results": {265                "carretera": expected_carretera266            },267            "obtained_results": {268                "carretera": carretera269            }270        }271    }272    if os.path.isdir('./output_tests') == False:273        os.mkdir('./output_tests')274    out_file = open("./output_tests/results_test_get_carretera.json", "a")275    json.dump(results, out_file, indent=4)276    out_file.close()277def save_results_test_set_carretera(carretera,expected_carretera,case):278    results = {279        case: {280            "expected_results": {281                "carretera": expected_carretera282            },283            "obtained_results": {284                "carretera": carretera285            }286        }287    }288    if os.path.isdir('./output_tests') == False:289        os.mkdir('./output_tests')290    out_file = open("./output_tests/results_test_set_carretera.json", "a")291    json.dump(results, out_file, indent=4)292    out_file.close()293def save_results_test_get_pkinicial(pkinicial,expected_pkinicial,case):294    results = {295        case: {296            "expected_results": {297                "pkinicial": expected_pkinicial298            },299            "obtained_results": {300                "pkinicial": pkinicial301            }302        }303    }304    if os.path.isdir('./output_tests') == False:305        os.mkdir('./output_tests')306    out_file = open("./output_tests/results_test_get_pkinicial.json", "a")307    json.dump(results, out_file, indent=4)308    out_file.close()309def save_results_test_set_pkinicial(pkinicial,expected_pkinicial,case):310    results = {311        case: {312            "expected_results": {313                "pkinicial": expected_pkinicial314            },315            "obtained_results": {316                "pkinicial": pkinicial317            }318        }319    }320    if os.path.isdir('./output_tests') == False:321        os.mkdir('./output_tests')322    out_file = open("./output_tests/results_test_set_pkinicial.json", "a")323    json.dump(results, out_file, indent=4)324    out_file.close()325def save_results_test_get_pkfinal(pkfinal,expected_pkfinal,case):326    results = {327        case: {328            "expected_results": {329                "pkfinal": expected_pkfinal330            },331            "obtained_results": {332                "pkfinal": pkfinal333            }334        }335    }336    if os.path.isdir('./output_tests') == False:337        os.mkdir('./output_tests')338    out_file = open("./output_tests/results_test_get_pkfinal.json", "a")339    json.dump(results, out_file, indent=4)340    out_file.close()341def save_results_test_set_pkfinal(pkfinal,expected_pkfinal,case):342    results = {343        case: {344            "expected_results": {345                "pkfinal": expected_pkfinal346            },347            "obtained_results": {348                "pkfinal": pkfinal349            }350        }351    }352    if os.path.isdir('./output_tests') == False:353        os.mkdir('./output_tests')354    out_file = open("./output_tests/results_test_set_pkfinal.json", "a")355    json.dump(results, out_file, indent=4)356    out_file.close()357def save_results_test_get_sentido(sentido,expected_sentido,case):358    results = {359        case: {360            "expected_results": {361                "sentido": expected_sentido362            },363            "obtained_results": {364                "sentido": sentido365            }366        }367    }368    if os.path.isdir('./output_tests') == False:369        os.mkdir('./output_tests')370    out_file = open("./output_tests/results_test_get_sentido.json", "a")371    json.dump(results, out_file, indent=4)372    out_file.close()373def save_results_test_set_sentido(sentido,expected_sentido,case):374    results = {375        case: {376            "expected_results": {377                "sentido": expected_sentido378            },379            "obtained_results": {380                "sentido": sentido381            }382        }383    }384    if os.path.isdir('./output_tests') == False:385        os.mkdir('./output_tests')386    out_file = open("./output_tests/results_test_set_sentido.json", "a")387    json.dump(results, out_file, indent=4)388    out_file.close()389def save_results_test_get_hacia(hacia,expected_hacia,case):390    results = {391        case: {392            "expected_results": {393                "hacia": expected_hacia394            },395            "obtained_results": {396                "hacia": hacia397            }398        }399    }400    if os.path.isdir('./output_tests') == False:401        os.mkdir('./output_tests')402    out_file = open("./output_tests/results_test_get_hacia.json", "a")403    json.dump(results, out_file, indent=4)404    out_file.close()405def save_results_test_set_hacia(hacia,expected_hacia,case):406    results = {407        case: {408            "expected_results": {409                "hacia": expected_hacia410            },411            "obtained_results": {412                "hacia": hacia413            }414        }415    }416    if os.path.isdir('./output_tests') == False:417        os.mkdir('./output_tests')418    out_file = open("./output_tests/results_test_set_hacia.json", "a")419    json.dump(results, out_file, indent=4)420    out_file.close()421def save_results_test_get_refincidencia(refincidencia,expected_refincidencia,case):422    results = {423        case: {424            "expected_results": {425                "refincidencia": expected_refincidencia426            },427            "obtained_results": {428                "refincidencia": refincidencia429            }430        }431    }432    if os.path.isdir('./output_tests') == False:433        os.mkdir('./output_tests')434    out_file = open("./output_tests/results_test_get_refincidencia.json", "a")435    json.dump(results, out_file, indent=4)436    out_file.close()437def save_results_test_set_refincidencia(refincidencia,expected_refincidencia,case):438    results = {439        case: {440            "expected_results": {441                "refincidencia": expected_refincidencia442            },443            "obtained_results": {444                "refincidencia": refincidencia445            }446        }447    }448    if os.path.isdir('./output_tests') == False:449        os.mkdir('./output_tests')450    out_file = open("./output_tests/results_test_set_refincidencia.json", "a")451    json.dump(results, out_file, indent=4)452    out_file.close()453def save_results_test_get_versionincidencia(versionincidencia,expected_versionincidencia,case):454    results = {455        case: {456            "expected_results": {457                "versionincidencia": expected_versionincidencia458            },459            "obtained_results": {460                "versionincidencia": versionincidencia461            }462        }463    }464    if os.path.isdir('./output_tests') == False:465        os.mkdir('./output_tests')466    out_file = open("./output_tests/results_test_get_versionincidencia.json", "a")467    json.dump(results, out_file, indent=4)468    out_file.close()469def save_results_test_set_versionincidencia(versionincidencia,expected_versionincidencia,case):470    results = {471        case: {472            "expected_results": {473                "versionincidencia": expected_versionincidencia474            },475            "obtained_results": {476                "versionincidencia": versionincidencia477            }478        }479    }480    if os.path.isdir('./output_tests') == False:481        os.mkdir('./output_tests')482    out_file = open("./output_tests/results_test_set_versionincidencia.json", "a")483    json.dump(results, out_file, indent=4)484    out_file.close()485def save_results_test_get_x(x,expected_x,case):486    results = {487        case: {488            "expected_results": {489                "x": expected_x490            },491            "obtained_results": {492                "x": x493            }494        }495    }496    if os.path.isdir('./output_tests') == False:497        os.mkdir('./output_tests')498    out_file = open("./output_tests/results_test_get_x.json", "a")499    json.dump(results, out_file, indent=4)500    out_file.close()501def save_results_test_set_x(x,expected_x,case):502    results = {503        case: {504            "expected_results": {505                "x": expected_x506            },507            "obtained_results": {508                "x": x509            }510        }511    }512    if os.path.isdir('./output_tests') == False:513        os.mkdir('./output_tests')514    out_file = open("./output_tests/results_test_set_x.json", "a")515    json.dump(results, out_file, indent=4)516    out_file.close()517def save_results_test_get_y(y,expected_y,case):518    results = {519        case: {520            "expected_results": {521                "y": expected_y522            },523            "obtained_results": {524                "x": y525            }526        }527    }528    if os.path.isdir('./output_tests') == False:529        os.mkdir('./output_tests')530    out_file = open("./output_tests/results_test_get_y.json", "a")531    json.dump(results, out_file, indent=4)532    out_file.close()533def save_results_test_set_y(y,expected_y,case):534    results = {535        case: {536            "expected_results": {537                "y": expected_y538            },539            "obtained_results": {540                "y": y541            }542        }543    }544    if os.path.isdir('./output_tests') == False:545        os.mkdir('./output_tests')546    out_file = open("./output_tests/results_test_set_y.json", "a")547    json.dump(results, out_file, indent=4)548    out_file.close()549def save_results_test_get_tipolocalizacion(tipolocalizacion,expected_tipolocalizacion,case):550    results = {551        case: {552            "expected_results": {553                "tipolocalizacion": expected_tipolocalizacion554            },555            "obtained_results": {556                "tipolocalizacion": tipolocalizacion557            }558        }559    }560    if os.path.isdir('./output_tests') == False:561        os.mkdir('./output_tests')562    out_file = open("./output_tests/results_test_get_tipolocalizacion.json", "a")563    json.dump(results, out_file, indent=4)564    out_file.close()565def save_results_test_set_tipolocalizacion(tipolocalizacion,expected_tipolocalizacion,case):566    results = {567        case: {568            "expected_results": {569                "tipolocalizacion": expected_tipolocalizacion570            },571            "obtained_results": {572                "tipolocalizacion": tipolocalizacion573            }574        }575    }576    if os.path.isdir('./output_tests') == False:577        os.mkdir('./output_tests')578    out_file = open("./output_tests/results_test_set_tipolocalizacion.json", "a")579    json.dump(results, out_file, indent=4)...checker.py
Source:checker.py  
1from subprocess import call2from pathlib import Path3from os import walk4import hashlib5import argparse6import sys7import logging8from shutil import copyfile9from filecmp import cmp as diff10def run_system_command(cmd: str,11                       shell: bool = False,12                       err_msg: str = None,13                       verbose: bool = True,14                       split: bool = True,15                       stdout=None,16                       stderr=None) -> int:17    """18    :param cmd: A string with the terminal command invoking an external program19    :param shell: Whether the command should be executed through the shell20    :param err_msg: Error message to print if execution fails21    :param verbose: Whether to print the command to the standard output stream22    :param split: Whether to split the tokens in the command string23    :param stdout: file pointer to redirect stdout to24    :param stderr: file pointer to redirect stderr to25    :return: Return code26    """27    if verbose:28        sys.stdout.write("Run cmd: {}\n".format(cmd))29    if split:30        cmd = cmd.split()31    rc = call(cmd, shell=shell, stdout=stdout, stderr=stderr)32    if err_msg and rc:33        sys.stderr.write(err_msg)34        exit(rc)35    return rc36def setup_logging(args):37    msg_format = '%(asctime)s:%(levelname)s: %(message)s'38    formatter = logging.Formatter(msg_format, datefmt='%H:%M:%S')39    handler = logging.StreamHandler()40    handler.setFormatter(formatter)41    logging.getLogger().addHandler(handler)42    logging.getLogger().setLevel(logging.DEBUG)43def check_makefile(args):44    options = ["makefile", "./Makefile"]45    rules = ["build", "run-p1", "run-p2", "run-best"]46    for file in options:47        file = Path(file)48        if file.exists():49            logging.info("Found Makefile!")50            with open(file, "r") as f:51                content = f.read()52                for rule in rules:53                    if rule not in content:54                        logging.warning(55                            "Rule {} not found in Makefile!".format(rule))56                    elif rule == "build":57                        call("make build", shell=True)58            break59    else:60        logging.critical("No Makefile found!")61def check_readme(args):62    options = ["README", "readme", "Readme"]63    for file in options:64        file = Path(file)65        if file.exists():66            logging.info("Found Readme")67            with open(file, "r") as f:68                content = f.read()69                if len(content) < 10:70                    logging.warning(71                        "Readme file is very short - be sure to include all the required information")72            break73    else:74        logging.critical("No README file found!")75def check_tests(args):76    tests = []77    input_path = Path("./in")78    if args.unique_tests:79        output_path = Path("./out/{}".format(args.algo))80    else:81        output_path = Path("./out")82    if not input_path.exists() or not input_path.is_dir():83        logging.critical("No folder for the input tests found!")84        return None85    if not output_path.exists() or not output_path.is_dir():86        logging.critical("No folder for the output tests found!")87        return None88    for (_, _, input_tests) in walk(input_path):89        input_tests = [file for file in input_tests if file.endswith(".in")]90        if len(input_tests) < 10:91            logging.critical(92                "Only {} input tests generated!".format(len(input_tests)))93        else:94            logging.info("Found {} tests.".format(len(input_tests)))95        unique = set()96        for test in input_tests:97            contents = open(input_path / test, "r").read().encode("utf-8")98            checksum = hashlib.md5(contents).hexdigest()99            unique.add(checksum)100        if len(input_tests) != len(unique):101            logging.critical(102                "Unique tests: {} / {}!".format(len(unique), len(input_tests)))103    for (_, _, output_tests) in walk(output_path):104        output_tests = [file for file in output_tests if file.endswith(".out")]105        for test in output_tests:106            test_id = test[:-4].replace("test", "")107            test_out = "test{}.out".format(test_id)108            if test_out not in output_tests:109                logging.critical(110                    "Missing output test for test {}!".format(test_id))111            else:112                tests.append((test_id,113                              input_path / "test{}.in".format(test_id),114                              output_path / "test{}.out".format(test_id)))115    return tests116def check_folder_structure(args):117    check_makefile(args)118    check_readme(args)119    return check_tests(args)120def main():121    parser = argparse.ArgumentParser()122    parser.add_argument('--algo', type=str,123                        help="Tells makefile which rule to run (e.g. --algo p1 will trigger rule make run-p1",124                        required=True)125    parser.add_argument('--unique-tests', dest='unique_tests',126                        help="Set this to True if each algorithm has different tests",127                        action='store_true')128    parser.add_argument('--task', type=int,129                        help="Task ID (e.g. --task 1 for Max Flow",130                        required=True)131    parser.add_argument('--run-test', type=int)132    parser.set_defaults(unique_tests=False)133    args = parser.parse_args()134    setup_logging(args)135    tests = check_folder_structure(args)136    for id, input_path, output_path in tests:137        logging.info("Running run-{} test {}..".format(args.algo, id))138        copyfile(input_path, "./test.in")139        call("make run-{}".format(args.algo), shell=True)140        if not diff(output_path, "./test.out"):141            logging.critical("Error trying to run test {}".format(id))142    logging.info("Done!")143if __name__ == "__main__":...tests.py
Source:tests.py  
1# Ty Bergstrom2# tests.py3# CSCE A4014# November 20205# Software Engineering Project6#7# Testing for the main mask detection model fully integrated with tracking statitics8# This will run the detect_and_track.py with different input video files9# The output results of for each video will be saved to args["testfile"]10# These are compared with the expected results located in "expected.csv"11# The output results are:12#		number of masks counted, number without masks counted, usage percent13#14# python3 tests.py -t test1.csv15#16# Sample output from this testing looks like this:17'''18	Test [1] passed19	Test [2] passed20	Test [3] passed21	Test [4] passed22	Test [5] failed: 23	 Expected: ['1', '0', '100.00'], Actual: ['2', '0', '100.00']24	Test [6] failed: 25	 Expected: ['1', '0', '100.00'], Actual: ['0', '0', '0.00']26'''27import os28import sys29import argparse30import filecmp31ap = argparse.ArgumentParser()32ap.add_argument("-t", "--testfile", required=True)33args = vars(ap.parse_args())34test_file = args["testfile"]35test_file_path = "../testing/" + test_file36os.system("touch " + test_file)37os.system("echo \"filename,mask,without_mask,usage\" > " + test_file)38cmd = "python3 -W ignore ../computer_vision/detect_and_track.py "39cmd += " -t " + test_file_path + " "40vid_dir = "../computer_vision/uploads/"41for vid in os.listdir(vid_dir):42	vid_path = vid_dir + vid43	os.system(cmd + " -f " + vid_path)44expected = "expected.csv"45if filecmp.cmp(test_file, expected):46	print("All tests passed")47	sys.exit(0)48print("Failed some tests")49expected = open(expected, "r")50correct_tests = [i for i in expected]51outputs = open(test_file)52output_tests = [i for i in outputs]53passed = 054i = 155while i < len(correct_tests):56	if correct_tests[i] == output_tests[i]:57		print("Test [{}] passed".format(i))58		passed += 159	else:60		print("Test [{}] failed: ".format(i))61		correct = [j.replace("\n", "") for j in correct_tests[i].split(",")[1:] ]62		actual = [j.replace("\n", "") for j in output_tests[i].split(",")[1:] ]63		print(" Expected: {}, Actual: {}".format(64			correct, actual65		))66	i += 167print("Passed {} out of {} tests".format(passed, len(output_tests)-1))...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!!
