Best Python code snippet using tox_python
test_qmca.py
Source:test_qmca.py
...31 directories[name] = os.path.join(tpath,path)32 #end for33 #end if34#end def get_test_info35def get_exe():36 get_test_info()37 return test_info['exe']38#end def get_exe 39def enter(path_name):40 import os41 assert(path_name in directories)42 path = directories[path_name]43 assert(os.path.exists(path))44 assert('cwd' not in directories)45 directories['cwd'] = os.getcwd()46 assert('cwd' in directories)47 os.chdir(path)48#end def enter49def leave():50 import os51 assert('cwd' in directories)52 cwd = directories.pop('cwd')53 assert(not 'cwd' in directories)54 os.chdir(cwd)55#end def leave56def test_help():57 exe = get_exe()58 help_text = 'Usage: qmca'59 command = sys.executable+' {}'.format(exe)60 out,err,rc = execute(command)61 assert(help_text in out)62 command = sys.executable+' {} -h'.format(exe)63 out,err,rc = execute(command)64 assert(help_text in out)65#end def test_help66def test_examples():67 exe = get_exe()68 example_text = 'QMCA examples'69 command = sys.executable+' {} -x'.format(exe)70 out,err,rc = execute(command)71 assert(example_text in out)72#end def test_examples73def test_unit_conversion():74 exe = get_exe()75 enter('vmc')76 command = sys.executable+' {} -e 5 -q e -u eV --fp=16.8f *scalar*'.format(exe)77 out,err,rc = execute(command)78 out_ref = '''79 vmc series 0 LocalEnergy = -284.62368087 +/- 0.1034480280 '''81 assert(text_eq(out,out_ref))82 leave()83#end def test_unit_conversion84def test_selected_quantities():85 exe = get_exe()86 enter('vmc')87 command = sys.executable+" {} -e 5 -q 'e k p' --fp=16.8f *scalar*".format(exe)88 out,err,rc = execute(command)89 out_ref = '''90 vmc series 0 91 LocalEnergy = -10.45972798 +/- 0.00380164 92 Kinetic = 11.08225203 +/- 0.02281909 93 LocalPotential = -21.54198001 +/- 0.02390850 94 '''95 assert(text_eq(out,out_ref))96 leave()97#end def test_selected_quantities98def test_all_quantities():99 exe = get_exe()100 enter('vmc')101 command = sys.executable+" {} -e 5 --fp=16.8f *scalar*".format(exe)102 out,err,rc = execute(command)103 out_ref = '''104 vmc series 0 105 LocalEnergy = -10.45972798 +/- 0.00380164 106 Variance = 0.39708591 +/- 0.00971200 107 Kinetic = 11.08225203 +/- 0.02281909 108 LocalPotential = -21.54198001 +/- 0.02390850 109 ElecElec = -2.73960796 +/- 0.00627045 110 LocalECP = -6.55900348 +/- 0.02845221 111 NonLocalECP = 0.53229890 +/- 0.00924772 112 IonIon = -12.77566747 +/- 0.00000000 113 LocalEnergy_sq = 109.80363374 +/- 0.07821849 114 MPC = -2.47615080 +/- 0.00644218 115 BlockWeight = 600.00000000 +/- 0.00000000 116 BlockCPU = 0.03578981 +/- 0.00022222 117 AcceptRatio = 0.76940789 +/- 0.00038843 118 Efficiency = 122102.67468280 +/- 0.00000000 119 TotalTime = 3.40003187 +/- 0.00000000 120 TotalSamples = 57000.00000000 +/- 0.00000000 121 -------------------------------------------------------------- 122 CorrectedEnergy = -10.19627082 +/- 0.00469500 123 '''124 assert(text_eq(out,out_ref))125 leave()126#end def test_all_quantities127def test_energy_variance():128 exe = get_exe()129 enter('opt')130 command = sys.executable+" {} -e 5 -q ev --fp=16.8f *scalar*".format(exe)131 out,err,rc = execute(command)132 out_ref = '''133 LocalEnergy Variance ratio 134opt series 0 -10.44922550 +/- 0.00475437 0.60256216 +/- 0.01476264 0.0577 135opt series 1 -10.45426389 +/- 0.00320561 0.40278743 +/- 0.01716415 0.0385 136opt series 2 -10.45991696 +/- 0.00271802 0.39865602 +/- 0.00934316 0.0381 137opt series 3 -10.45830307 +/- 0.00298106 0.38110459 +/- 0.00529809 0.0364 138opt series 4 -10.46298481 +/- 0.00561322 0.38927957 +/- 0.01204068 0.0372 139opt series 5 -10.46086055 +/- 0.00375811 0.39354343 +/- 0.00913372 0.0376 140 '''141 assert(text_eq(out,out_ref))142 leave()143#end def test_energy_variance144def test_multiple_equilibration():145 exe = get_exe()146 enter('dmc')147 command = sys.executable+" {} -e '5 10 15 20' -q ev --fp=16.8f *scalar*".format(exe)148 out,err,rc = execute(command)149 out_ref = '''150 LocalEnergy Variance ratio 151dmc series 0 -10.48910618 +/- 0.00714379 0.45789123 +/- 0.04510618 0.0437 152dmc series 1 -10.53167630 +/- 0.00163992 0.38531028 +/- 0.00162825 0.0366 153dmc series 2 -10.53061971 +/- 0.00123791 0.38172188 +/- 0.00124608 0.0362 154dmc series 3 -10.52807733 +/- 0.00122687 0.38565052 +/- 0.00196074 0.0366 155 '''156 assert(text_eq(out,out_ref))157 leave()158#end def test_multiple_equilibration159def test_join():160 exe = get_exe()161 enter('dmc')162 command = sys.executable+" {} -e 5 -j '1 3' -q ev --fp=16.8f *scalar*".format(exe)163 out,err,rc = execute(command)164 out_ref = '''165 LocalEnergy Variance ratio 166dmc series 0 -10.48910618 +/- 0.00714379 0.45789123 +/- 0.04510618 0.0437 167dmc series 1 -10.53022752 +/- 0.00073527 0.38410495 +/- 0.00082972 0.0365 168 '''169 assert(text_eq(out,out_ref))170 leave()171#end def test_join172def test_multiple_directories():173 exe = get_exe()174 enter('multi')175 command = sys.executable+" {} -e 5 -q ev --fp=16.8f */*scalar*".format(exe)176 out,err,rc = execute(command)177 out_ref = '''178 LocalEnergy Variance ratio 179dmc/dmc series 0 -10.48910618 +/- 0.00714379 0.45789123 +/- 0.04510618 0.0437 180dmc/dmc series 1 -10.53165002 +/- 0.00153762 0.38502189 +/- 0.00158532 0.0366 181dmc/dmc series 2 -10.53018917 +/- 0.00106340 0.38161141 +/- 0.00111391 0.0362 182dmc/dmc series 3 -10.52884336 +/- 0.00135513 0.38568155 +/- 0.00151082 0.0366 183 184opt/opt series 0 -10.44922550 +/- 0.00475437 0.60256216 +/- 0.01476264 0.0577 185opt/opt series 1 -10.45426389 +/- 0.00320561 0.40278743 +/- 0.01716415 0.0385 186opt/opt series 2 -10.45991696 +/- 0.00271802 0.39865602 +/- 0.00934316 0.0381 187opt/opt series 3 -10.45830307 +/- 0.00298106 0.38110459 +/- 0.00529809 0.0364 188opt/opt series 4 -10.46298481 +/- 0.00561322 0.38927957 +/- 0.01204068 0.0372 189opt/opt series 5 -10.46086055 +/- 0.00375811 0.39354343 +/- 0.00913372 0.0376 190vmc/vmc series 0 -10.45972798 +/- 0.00380164 0.39708591 +/- 0.00971200 0.0380191 '''192 assert(text_eq(out,out_ref))193 leave()194#end def test_multiple_directories195def test_twist_average():196 exe = get_exe()197 enter('vmc_twist')198 command = sys.executable+" {} -a -e 5 -q ev --fp=16.8f *scalar*".format(exe)199 out,err,rc = execute(command)200 out_ref = '''201 LocalEnergy Variance ratio 202avg series 0 -11.34367335 +/- 0.00257603 0.57340688 +/- 0.00442552 0.0505 203 '''204 assert(text_eq(out,out_ref))205 leave()206#end def test_twist_average207def test_weighted_twist_average():208 exe = get_exe()209 enter('vmc_twist')210 command = sys.executable+" {} -a -w '1 3 3 1' -e 5 -q ev --fp=16.8f *scalar*".format(exe)211 out,err,rc = execute(command)212 out_ref = '''213 LocalEnergy Variance ratio 214avg series 0 -11.44375840 +/- 0.00292164 0.44863011 +/- 0.00502859 0.0392 215 '''216 assert(text_eq(out,out_ref))217 leave()...
test_cli.py
Source:test_cli.py
...25data = {data_path}26eval[test] = {data_path}27'''28 PROJECT_ROOT = tm.PROJECT_ROOT29 def get_exe(self):30 if platform.system() == 'Windows':31 exe = 'xgboost.exe'32 else:33 exe = 'xgboost'34 exe = os.path.join(self.PROJECT_ROOT, exe)35 assert os.path.exists(exe)36 return exe37 def test_cli_model(self):38 data_path = "{root}/demo/data/agaricus.txt.train?format=libsvm".format(39 root=self.PROJECT_ROOT)40 exe = self.get_exe()41 seed = 199442 with tempfile.TemporaryDirectory() as tmpdir:43 model_out_cli = os.path.join(44 tmpdir, 'test_load_cli_model-cli.json')45 model_out_py = os.path.join(46 tmpdir, 'test_cli_model-py.json')47 config_path = os.path.join(48 tmpdir, 'test_load_cli_model.conf')49 train_conf = self.template.format(data_path=data_path,50 seed=seed,51 task='train',52 model_in='NULL',53 model_out=model_out_cli,54 test_path='NULL',55 name_pred='NULL',56 model_dir='NULL')57 with open(config_path, 'w') as fd:58 fd.write(train_conf)59 subprocess.run([exe, config_path])60 predict_out = os.path.join(tmpdir,61 'test_load_cli_model-prediction')62 predict_conf = self.template.format(task='pred',63 seed=seed,64 data_path=data_path,65 model_in=model_out_cli,66 model_out='NULL',67 test_path=data_path,68 name_pred=predict_out,69 model_dir='NULL')70 with open(config_path, 'w') as fd:71 fd.write(predict_conf)72 subprocess.run([exe, config_path])73 cli_predt = numpy.loadtxt(predict_out)74 parameters = {75 'booster': 'gbtree',76 'objective': 'reg:squarederror',77 'eta': 1.0,78 'gamma': 1.0,79 'seed': seed,80 'min_child_weight': 0,81 'max_depth': 382 }83 data = xgboost.DMatrix(data_path)84 booster = xgboost.train(parameters, data, num_boost_round=10)85 # CLI model doesn't contain feature info.86 booster.feature_names = None87 booster.feature_types = None88 booster.save_model(model_out_py)89 py_predt = booster.predict(data)90 numpy.testing.assert_allclose(cli_predt, py_predt)91 cli_model = xgboost.Booster(model_file=model_out_cli)92 cli_predt = cli_model.predict(data)93 numpy.testing.assert_allclose(cli_predt, py_predt)94 with open(model_out_cli, 'rb') as fd:95 cli_model_bin = fd.read()96 with open(model_out_py, 'rb') as fd:97 py_model_bin = fd.read()98 assert hash(cli_model_bin) == hash(py_model_bin)99 def test_cli_help(self):100 exe = self.get_exe()101 completed = subprocess.run([exe], stdout=subprocess.PIPE)102 error_msg = completed.stdout.decode('utf-8')103 ret = completed.returncode104 assert ret == 1105 assert error_msg.find('Usage') != -1106 assert error_msg.find('eval[NAME]') != -1107 completed = subprocess.run([exe, '-V'], stdout=subprocess.PIPE)108 msg = completed.stdout.decode('utf-8')109 assert msg.find('XGBoost') != -1110 v = xgboost.__version__111 if v.find('dev') != -1:112 assert msg.split(':')[1].strip() == v.split('-')[0]113 elif v.find('rc') != -1:114 assert msg.split(':')[1].strip() == v.split('rc')[0]115 else:116 assert msg.split(':')[1].strip() == v117 def test_cli_model_json(self):118 exe = self.get_exe()119 data_path = "{root}/demo/data/agaricus.txt.train?format=libsvm".format(120 root=self.PROJECT_ROOT)121 seed = 1994122 with tempfile.TemporaryDirectory() as tmpdir:123 model_out_cli = os.path.join(124 tmpdir, 'test_load_cli_model-cli.json')125 config_path = os.path.join(tmpdir, 'test_load_cli_model.conf')126 train_conf = self.template.format(data_path=data_path,127 seed=seed,128 task='train',129 model_in='NULL',130 model_out=model_out_cli,131 test_path='NULL',132 name_pred='NULL',133 model_dir='NULL')134 with open(config_path, 'w') as fd:135 fd.write(train_conf)136 subprocess.run([exe, config_path])137 with open(model_out_cli, 'r') as fd:138 model = json.load(fd)139 assert model['learner']['gradient_booster']['name'] == 'gbtree'140 def test_cli_save_model(self):141 '''Test save on final round'''142 exe = self.get_exe()143 data_path = "{root}/demo/data/agaricus.txt.train?format=libsvm".format(144 root=self.PROJECT_ROOT)145 seed = 1994146 with tempfile.TemporaryDirectory() as tmpdir:147 model_out_cli = os.path.join(tmpdir, '0010.model')148 config_path = os.path.join(tmpdir, 'test_load_cli_model.conf')149 train_conf = self.template.format(data_path=data_path,150 seed=seed,151 task='train',152 model_in='NULL',153 model_out='NULL',154 test_path='NULL',155 name_pred='NULL',156 model_dir=tmpdir)...
run-lava.py
Source:run-lava.py
...24 p.add_argument('app', choices=OPTS.keys())25 return p.parse_args()26def get_app_dir(args):27 return os.path.join(LAVA_M_DIR, args.app)28def get_exe(args, for_afl):29 if for_afl:30 dn = "afl-lava-install"31 else:32 dn = "lava-install"33 app_dir = get_app_dir(args)34 exe = os.path.join(app_dir, "coreutils-8.24-lava-safe", dn, "bin", args.app)35 if not os.path.exists(exe):36 print("[-] Build LAVA-M")37 sys.exit(-1)38 return exe39def run(args):40 app_dir = get_app_dir(args)41 afl_exe = get_exe(args, True)42 exe = get_exe(args, False)43 output_dir = os.path.join(app_dir, "fuzzer_output")44 if os.path.exists(output_dir):45 print("[-] Output directory exists: %s" % output_dir)46 sys.exit(-1)47 input_dir = os.path.join(app_dir, "fuzzer_input")48 procs = []49 os.putenv("AFL_NO_UI", "1")50 # Run AFL master51 cmd = [AFL_FUZZ, "-M", "master",52 "-i", input_dir,53 "-o", output_dir,54 "--", afl_exe] + OPTS[args.app] + ["@@"]55 procs.append(sh(cmd))56 # Run AFL slave57 cmd = [AFL_FUZZ, "-S", "slave",58 "-i", input_dir,59 "-o", output_dir,60 "--", afl_exe] + OPTS[args.app] + ["@@"]61 procs.append(sh(cmd))62 # Run QSYM63 print('[+] Wait until AFL is initialized')64 time.sleep(5)65 cmd = [QSYM, "-a", "slave",66 "-n", "qsym",67 "-o", output_dir,68 "--", exe] + OPTS[args.app] + ["@@"]69 procs.append(sh(cmd))70 print("[+] Wait for 5 hours")71 time.sleep(5 * 60 * 60) # sleep 5 hours72 print("[+] Good... let's kill everything")73 for p in procs:74 p.terminate()75 try:76 p.wait(timeout=5)77 except subprocess.TimeoutExpired:78 p.kill()79 p.wait()80def collect(args):81 app_dir = get_app_dir(args)82 exe = get_exe(args, False)83 crash_dir = os.path.join(app_dir, "fuzzer_output/slave/crashes")84 if not os.path.exists(crash_dir):85 print("[-] Output directory does not exist: %s" % crash_dir)86 print("[-] Run the experiment first")87 sys.exit(-1)88 devnull = open("/dev/null", "wb")89 bugs = set()90 for name in sorted(os.listdir(crash_dir)):91 if not name.startswith("id:"):92 continue93 path = os.path.join(crash_dir, name)94 p = sh([exe] + OPTS[args.app] + [path], stdout=subprocess.PIPE, stderr=devnull)95 try:96 stdout, stderr = p.communicate(timeout=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!!