Best Python code snippet using hypothesis
test.py
Source:test.py  
...177        raise ValueError('unrecognized file type: ' + fname)178    clen = len( comment_string ) + 1179#------------------------------------------------------------------180bin_name = "../../tara"181def execute_example(fname,o):182    ops =o.split(' ')183    ops = [o for o in ops if o != '']184    ops.append(fname)185    return check_output([bin_name]+ops)186class example:187    def populate_runs( self, options, outputs ):188        for o in options:189            self.count = self.count + 1190            if( not args.suppress ):191                printf( "[%d..", self.count )192            sys.stdout.flush()193            result = execute_example(self.filename, o)194            if result in outputs:195                if( not args.suppress ):196                    printf( "pass] " )197            else:198                if( not args.suppress ):199                    printf( "fail]\n" )200                printf( "call: %s %s %s\n", bin_name, o, self.filename)201                if( args.verbose ):202                    for output in outputs:203                        process_diff( output, result )204                205    def load_running_cases(self):206        with open(self.filename, 'r') as f:207            options = []208            output = ''209            outputs = []210            started = False211            for line in f:212                if started:213                    if line[:clen] == comment_string +'~' :214                        outputs.append( output )215                        self.populate_runs( options, outputs )216                        options = []217                        output = ''218                        started = False219                    elif line[:clen] == comment_string + '#' :220                        output += line[clen:]221                    elif line[:clen] == comment_string + '|' :222                        outputs.append( output )223                        output = ''224                else:225                    if line[:clen] == comment_string + '!':226                        options.append( line[clen:-1] )227                    if line[:clen] == comment_string + '~' :228                        started = True229            if( not args.suppress ):230                printf( "\n" )231    def __init__( self, fname ):232        set_comment_string( fname )233        self.filename = fname234        self.count = 0235        self.load_running_cases()236#------------------------------------------------------------------237if args.update:238    for filename in files:239        set_comment_string( filename )240        options = []241        with open(filename, 'r') as f:242            for line in f:243                if line[:clen] == comment_string + '!':244                    options.append( line[clen:-1] )245    246        with open( filename, "a") as myfile:247            myfile.write( "\n" + comment_string + "####### ERASE TESTS ABOVE #######\n" )248        for o in options:249            printf( "%s\n", o )250            ops =o.split(' ')251            ops = [o for o in ops if o != '']252            ops.append(filename)253            check_output(['./gen.py']+ops)254elif args.compare != None:255    m1 = args.compare[0]256    m2 = args.compare[1]257    printf( "Comparing %s %s:\n", m1, m2)258    # prune_list = '-r diffvar,unsat_core,remove_implied'259    prune_list = ''260    for filename in files:261	out1 = execute_example( filename, prune_list + ' -M ' + m1 )262	out2 = execute_example( filename, prune_list + ' -M ' + m2 )263	if out1 == out2:264            pass265	else:266	    printf( "Different output %s", filename)267	    printf( "%s",out1)268	    printf( "%s",out2)269elif args.run != None:270    m1 = args.run271    printf( "Running %s:\n", m1)272    out2 = "\nAll traces are good or infeasable.\n"273    good_files = []274    for filename in files:275	out1 = execute_example( filename, ' -M ' + m1 )276	if out1 == out2:277            good_files.append(filename)278	else:279	    printf( "Bad executions: %s\n", filename)280	    # printf( "%s",out1)281    printf( "Following files returned no bad executions:\n" )282    for gf in good_files:283        printf( "%s\n", gf)284else:285    for f in files:286        if( not args.suppress ):287            printf( "%s\n", f )288        benchmakr = example(f)289#----------------------------------------------------------------290# code added by Barke291# all.py292# def memory_model(filename,model):293#     n = 0294#     list = []295#     with open(filename, 'r') as f:296# 	lines = f.readlines()297# 	for line in lines:298# 	    n = n + 1299# 	    if "#!-r diffvar,unsat_core,remove_implied -M " + model + "\n" in line:300# 	        'print (line,n)'301# 	    	num = n302# 		'print num'303# 		break304# 	for line in islice(lines,num,num+100):305# 	    'print (line)'306# 	    if "#!" not in line:307# 		list.append(line.strip())308# 	    else:309# 		break310#     return list311# def compare(filename,model1,model2):312#     list1 = memory_model(filename,model1)313#     list2 = memory_model(filename,model2)314#     'print list1'315#     'print list2'316#     'return set(list1) == set(list2) and len(list1) == len(list2)'317#     if list1 == list2:318# 	print("Same Output\n")319#     else:320# 	print("Different output\n")321# models = ["sc","tso","pso","rmo"]322# lists=[]323# for files in known_files:324#     for model1 in models:325# 	for model2 in models:326# 	    print (files,model1,model2)327# 	    if model1 != model2:328# 	    	compare(files,model1,model2)329# 		'lists.append(compare(files,model1,model2))'330#compare.py331# parser = argparse.ArgumentParser(description='Compare 2 memory models')332# parser.add_argument("-c1","--compare1", choices=["sc","tso","pso","alpha","rmo"],help = "compare two memory models",type = str)333# parser.add_argument("-c2","--compare2", choices=["sc","tso","pso","alpha","rmo"],help = "compare two memory models",type = str)334# parser.add_argument("-file","--file", help = "filename",type = str)335# args = parser.parse_args()336# first_model = args.compare1337# second_model = args.compare2338# filename = args.file339# print (first_model)340# print (second_model)341# print (filename)342# def memory_model(filename,model):343#     n = 0344#     list = []345#     with open(filename, 'r') as f:346# 	lines = f.readlines()347# 	for line in lines:348# 	    n = n + 1349# 	    if "#!-r diffvar,unsat_core,remove_implied -M " + model + "\n" in line:350# 	        'print (line,n)'351# 	    	num = n352# 		'print num'353# 		break354# 	for line in islice(lines,num,num+100):355# 	    'print (line)'356# 	    if "#!" not in line:357# 		list.append(line.strip())358# 	    else:359# 		break360#     return list361	    362# def compare(filename,model1,model2):363#     list1 = memory_model(filename,model1)364#     list2 = memory_model(filename,model2)365#     'print list1'366#     'print list2'367#     if list1 == list2:368# 	print("Same Output")369#     else:370# 	print("Different output")371# compare(filename,first_model,second_model)372# new.py373# def execute_example(o,filename):374#     ops =o.split(' ')375#     ops = [o for o in ops if o != '']376#     ops.append(filename)377#     return check_output([bin_name]+ops)378# def compare(o1,o2,filename):379#     rmo = execute_example(o1,filename)380#     'print rmo'381#     alpha = execute_example(o2,filename)382#     'print alpha'383#     print filename384#     if rmo == alpha:385# 	print "Same"386#     else:387# 	print "Different"388# for files in known_files:...all_examples.py
Source:all_examples.py  
...46    with open(new_filename, mode='w') as fh:47        fh.write(import_matplotlib)48        fh.write(file_text)49    return new_filename50def execute_example(filename):51    """Executes a file as script.52    First prepends matplotlib import53    """54    #filename = os.path.join(os.getcwd(), filename)55    new_filename = None56    try:57        new_filename = prepend_mpl_import(filename)58        retcode = subprocess.call(sys.executable + ' ' + new_filename, shell=True)59        if retcode != 0:60            print('### ERROR: Example `%s` failed! ###' % filename)61            sys.exit(retcode)62        else:63            print('#### Example successful ####')64    finally:65        if new_filename is not None:66            os.remove(new_filename)67def main():68    os.chdir(os.path.join('..','..','examples'))69    sys.path.append(os.getcwd())70    simple_examples = glob.glob('*.py')71    assert len(simple_examples) == 22 - 4 + 1 + 272    # + Number of Examples - Number of Packages +  __init__.py + 19b and 19c73    for simple_example in simple_examples:74        if simple_example == '__init__':75            continue76        if skip(simple_example):77            print("---------- Skipping %s ----------" % simple_example)78        else:79            print("########## Running %s ###########" % simple_example)80            execute_example(simple_example)81    ex17 = 'example_17_wrapping_an_existing_project'82    if skip(ex17):83        print("------- Skipping %s -------" % ex17)84    else:85        os.chdir(ex17)86        sys.path.append(os.getcwd())87        print('Running original')88        execute_example('original.py')89        print('Running pypet wrapping')90        execute_example('pypetwrap.py')91        os.chdir('..')92    ex13 = 'example_13_post_processing'93    if skip(ex13):94        print("------- Skipping %s -------" % ex13)95    else:96        print("########## Running %s ###########" % ex13)97        os.chdir(ex13)98        sys.path.append(os.getcwd())99        print("Running main")100        execute_example('main.py')101        print("Running analysis")102        execute_example('analysis.py')103        print("Running pipeline")104        execute_example('pipeline.py')105        os.chdir('..')106    ex24 = 'example_24_large_scale_brian2_simulation'107    if skip(ex24):108        print("------- Skipping %s -------" % ex24)109    else:110        print("########## Running %s ###########" % ex24)111        os.chdir(ex24)112        sys.path.append(os.getcwd())113        print("Running script")114        execute_example('runscript.py')115        print("Running analysis")116        execute_example('plotff.py')117        os.chdir('..')118if __name__ == '__main__':...test_examples.py
Source:test_examples.py  
2'''Test functionality of examples.'''3import os4import runpy5import eminus6def execute_example(name):7    '''Test the execution of a given Python script.'''8    os.chdir(f'{eminus.__path__[0]}/../examples/{name}')9    try:10        runpy.run_path(f'{name}.py')11    except Exception as err:12        print(f'Test for {name} failed.')13        raise SystemExit(err) from None14    else:15        print(f'Test for {name} passed.')16    return17def clean_example(trash):18    '''Clean the example folder after running the script.'''19    for it in trash:20        if os.path.exists(it):21            os.remove(it)22    return23def test_01():24    execute_example('01_installation_test')25def test_02():26    execute_example('02_minimal_example')27def test_03():28    execute_example('03_atoms_objects')29def test_04():30    execute_example('04_dft_calculations')31def test_05():32    execute_example('05_input_output')33    clean_example(['CH4.pkl', 'CH4_density.cube'])34def test_06():35    execute_example('06_advanced_functionalities')36    clean_example(['Ne_1.cube', 'Ne_2.cube', 'Ne_3.cube', 'Ne_4.cube'])37def test_07():38    execute_example('07_fod_addon')39    clean_example(['CH4_FLO_1.cube', 'CH4_FLO_2.cube', 'CH4_FLO_3.cube', 'CH4_FLO_4.cube',40                   'CH4_fods.xyz'])41def test_08():42    clean_example(['CH4_density.cube', 'CH4_fods.xyz'])43    pass44def test_09():45    execute_example('09_sic_calculations')46def test_10():47    pass48if __name__ == '__main__':49    test_01()50    test_02()51    test_03()52    test_04()53    test_05()54    test_06()55    test_07()56    test_08()57    test_09()...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!!
