Best Python code snippet using autotest_python
test_functional_cli.py
Source:test_functional_cli.py
...6import json7import requests8from click.testing import CliRunner9import urllib310urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)11def test_login0():12 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)13 runner = CliRunner()14 result = runner.invoke(evgroup_38, ['login', '--userna', 'angrybird515', 'vfdbf'])15 assert result.exit_code != 016def test_login():17 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)18 runner = CliRunner()19 result = runner.invoke(evgroup_38, ['login', '--username', 'admin', '--passw', 'petrol4ever'])20 assert result.exit_code == 021def test_healthcheck():22 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)23 runner = CliRunner()24 result = runner.invoke(evgroup_38,['healthcheck'])25 #assert "{u'Status': u'OK'}\n" in result.output26 assert result.exit_code == 027#def test_resetsessions():28# urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)29# runner = CliRunner()30# result = runner.invoke(evgroup_38,['resetsessions'])31# assert "{u'Status': u'OK'}\n" in result.output32# assert result.exit_code == 033def test_sessions_per_station1():34 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)35 runner = CliRunner()36 result = runner.invoke(evgroup_38, ['SessionsPerStation', '--station', '2-39-129-17', '--datefrom','20080101','--dateto','20201010'])37 #assert "Success\n" in result.output38 assert result.exit_code == 039def test_sessions_per_station2():40 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)41 runner = CliRunner()42 result = runner.invoke(evgroup_38, ['SessionsPerStation', '--station', '2-99-129-17', '--datefrom','20080101','--dateto','20201010'])43 assert result.exit_code == 044 assert "No Chargings for this Station\n" in result.output45def test_sessions_per_station3():46 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)47 runner = CliRunner()48 result = runner.invoke(evgroup_38, ['SessionsPerStation', '--station', '2-39-129-17', '--datefrom','0101','--dateto','20201010'])49 assert result.exit_code == 050 assert "Bad Request : Invalid Date Format\n" in result.output51def test_sessions_per_station4():52 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)53 runner = CliRunner()54 result = runner.invoke(evgroup_38, ['SessionsPerStation', '--station', '2-39-129-17', '--datefrom','20080101','--dateto','2020'])55 assert result.exit_code == 056 assert "Bad Request : Invalid Date Format\n" in result.output57def test_sessions_per_station5():58 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)59 runner = CliRunner()60 result = runner.invoke(evgroup_38, ['SessionsPerStation', '--datefrom','20080101','--dateto','20201010'])61 assert result.exit_code != 062def test_sessions_per_station6():63 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)64 runner = CliRunner()65 result = runner.invoke(evgroup_38, ['SessionsPerStation', '2-39-129-17', '--datefrom','20080101','--dateto','20201010'])66 assert result.exit_code != 067#def test_sessions_per_station6():68# urllib3.disable_warnings()69# runner = CliRunner()70# result = runner.invoke(evgroup_38, ['SessionsPerStation'])71# assert result.exit_code == 172def test_sessions_per_point1():73 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)74 runner = CliRunner()75 result = runner.invoke(evgroup_38, ['SessionsPerPoint', '--point', 'CA-3132-39-123-23', '--datefrom','20080101','--dateto','20201010'])76 assert result.exit_code == 077def test_sessions_per_point2():78 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)79 runner = CliRunner()80 result = runner.invoke(evgroup_38, ['SessionsPerPoint', '--point', 'CA-3132-39-123-23', '--datefrom','kat','--dateto','20201010'])81 assert result.exit_code == 082 assert "Bad Request : Invalid Date Format" in result.output83def test_sessions_per_point3():84 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)85 runner = CliRunner()86 result = runner.invoke(evgroup_38, ['SessionsPerPoint', '--point', 'CA-31339-123-23', '--datefrom','20200101','--dateto','20201010'])87 assert result.exit_code == 088 assert "No Charging Point with this ID" in result.output89#def test_sessions_per_point1():90# runner = CliRunner()91# result = runner.invoke(evgroup_38, ['SessionsPerPoint', '--point', 'CA-3132-39-123-23'])92# assert result.exit_code == 193def test_sessions_per_ev():94 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)95 runner = CliRunner()96 result = runner.invoke(evgroup_38, ['SessionsPerEV', '--ev', '059c028d-b2a6-4a8d-947a-158c7537b290', '--datefrom','20080101','--dateto','20201010'])97 assert result.exit_code == 098def test_sessions_per_ev2():99 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)100 runner = CliRunner()101 result = runner.invoke(evgroup_38, ['SessionsPerEV', '--ev', 'kat', '--datefrom','20080101','--dateto','20201010'])102 assert result.exit_code == 0103 assert "No vehicle with this ID.\n" in result.output104def test_sessions_per_ev3():105 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)106 runner = CliRunner()107 result = runner.invoke(evgroup_38, ['SessionsPerEV', '--ev', '059c028d-b2a6-4a8d-947a-158c7537b290', '--datefrm','20080101','--dateto','20201010'])108 assert result.exit_code != 0109#def test_sessions_per_ev4():110# runner = CliRunner()111# result = runner.invoke(evgroup_38, ['SessionsPerEV', '--ev', '059c028d-b2a6-4a8d-947a-158c7537b290', '--dateto','20201010'])112# assert result.exit_code != 0113def test_sessions_per_provider():114 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)115 runner = CliRunner()116 result = runner.invoke(evgroup_38, ['SessionsPerProvider', '--provider', '1', '--datefrom','20200101','--dateto','20201010'])117 assert result.exit_code == 0118 #assert not "No Charging Point with this Provider ID.\n" in result.output119def test_sessions_per_provider2():120 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)121 runner = CliRunner()122 result = runner.invoke(evgroup_38, ['SessionsPerProvider', '--provider', 'kat', '--datefrom','20200101','--dateto','20201010'])123 assert result.exit_code == 0124 assert "No Charging Point with this ID.\n" in result.output125def test_sessions_per_provider3():126 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)127 runner = CliRunner()128 result = runner.invoke(evgroup_38, ['SessionsPerProvider', '--proer', '1', '--datefrom','20080101','--dateto','20201010'])129 assert result.exit_code != 0130def test_sessions_per_provider4():131 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)132 runner = CliRunner()133 result = runner.invoke(evgroup_38, ['SessionsPerProvider', '--provider', '100185946', '--datefrom','20200505','--dateto','20201010'])134 assert result.exit_code == 0135def test_sessions_per_provider5():136 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)137 runner = CliRunner()138 result = runner.invoke(evgroup_38, ['SessionsPerProvider', '--provider', '200', '--datefrom','20080101','--dateto','20201010'])139 assert result.exit_code == 0140 assert "No Charging Point with this ID.\n" in result.output141def test_admin_users1():142 urllib3.disable_warnings()143 runner = CliRunner()144 result = runner.invoke(evgroup_38, ['Admin', '--users', 'blackkoala898'])145 assert result.exit_code == 0146def test_admin_users2():147 urllib3.disable_warnings()148 runner = CliRunner()149 result = runner.invoke(evgroup_38, ['Admin', '--username', 'angrybird515', '--users', 'angrybird515'])150 #assert result.exit_code == 0151 assert "\nInvalid Combination\n\n" in result.output152def test_admin_users3():153 urllib3.disable_warnings()154 runner = CliRunner()155 result = runner.invoke(evgroup_38, ['Admin', '--username', 'angrybird515'])156 assert result.exit_code == 0157 assert "\nInvalid Combination\n\n" in result.output158#make many tests like that with correct usernames159def test_admin_usermod():160 urllib3.disable_warnings()161 runner = CliRunner()162 result = runner.invoke(evgroup_38, ['Admin','--username', 'browntiger776', '--passw', 'make'])163 assert result.exit_code == 0164 assert not "has been created or changed password" in result.output165#def test_admin_usermod2():166# urllib3.disable_warnings()167# runner = CliRunner()168# result = runner.invoke(evgroup_38, ['Admin','--username', 'katerina', 'passw', 'newuser'])169# assert "has been created or changed password" in result.output170def test_admin_sessionsupd():171 urllib3.disable_warnings()172 runner = CliRunner()173 result = runner.invoke(evgroup_38, ['Admin','--sessionsupd', 'test'])174 assert result.exit_code != 0175def test_logout():176 urllib3.disable_warnings()177 runner = CliRunner()178 result = runner.invoke(evgroup_38, ['logout'])...
nutanixv2api.py
Source:nutanixv2api.py
...5 self.base_url = base_url6 self.username = username7 self.password = password8 def get_vm_uuid(self, vm_name):9 requests.packages.urllib3.disable_warnings()10 s = requests.Session()11 s.auth = (self.username, self.password)12 s.headers.update({'Content-Type': 'application/json; charset=utf-8'})13 data = s.get(self.base_url + 'vms', verify=False).json()14 for vm in data['entities']:15 if vm_name == vm['name']:16 return vm['uuid']17 return 'no such vm'18 def get_vm_list(self):19 requests.packages.urllib3.disable_warnings()20 s = requests.Session()21 s.auth = (self.username, self.password)22 s.headers.update({'Content-Type': 'application/json; charset=utf-8'})23 data = s.get(self.base_url + 'vms', verify=False)24 return data 25 def get_vm(self, vm_uuid, include_vm_disk_config=False, include_vm_nic_config=False):26 requests.packages.urllib3.disable_warnings()27 s = requests.Session()28 s.auth = (self.username, self.password)29 s.headers.update({'Content-Type': 'application/json; charset=utf-8'})30 data = s.get(self.base_url + 'vms/%s' %vm_uuid, params={'include_vm_disk_config': include_vm_disk_config, 'include_vm_nic_config': include_vm_nic_config},31 verify=False)32 return data33 def vm_create(self, body):34 '''required params in body:35 {"memory_mb": 1024, "name": "pida","num_vcpus": 1}36 '''37 requests.packages.urllib3.disable_warnings()38 s = requests.Session()39 s.auth = (self.username, self.password)40 s.headers.update({'Content-Type': 'application/json; charset=utf-8'})41 data = s.post(self.base_url + 'vms', json=body, verify=False)42 return data43 def vm_delete(self, vm_uuid):44 requests.packages.urllib3.disable_warnings()45 s = requests.Session()46 s.auth = (self.username, self.password)47 s.headers.update({'Content-Type': 'application/json; charset=utf-8'})48 data = s.delete(self.base_url + 'vms/%s' %vm_uuid, verify=False)49 return data50 def vm_update(self, vm_uuid, body=None):51 requests.packages.urllib3.disable_warnings()52 s = requests.Session()53 s.auth = (self.username, self.password)54 s.headers.update({'Content-Type': 'application/json; charset=utf-8'})55 data = s.put(self.base_url + 'vms/%s' %vm_uuid, json=body, verify=False)56 return data57 def vm_clone(self, vm_uuid, body=None):58 ''' body(required) = clone machine parameters'''59 requests.packages.urllib3.disable_warnings()60 s = requests.Session()61 s.auth = (self.username, self.password)62 s.headers.update({'Content-Type': 'application/json; charset=utf-8'})63 data = s.post(self.base_url + 'vms/%s/clone' %vm_uuid, json=body, verify=False)64 return data65 def vm_migrate(self, vm_uuid, host_uuid):66 # works only with v0.8 api67 requests.packages.urllib3.disable_warnings()68 s = requests.Session()69 s.auth = (self.username, self.password)70 s.headers.update({'Content-Type': 'application/json; charset=utf-8'})71 body = {"hostUuid": host_uuid}72 data = s.post(self.base_url + 'vms/%s/migrate/' %vm_uuid, json=body, verify=False)73 return data74 def vm_change_power_state(self, vm_uuid, body):75 requests.packages.urllib3.disable_warnings()76 s = requests.Session()77 s.auth = (self.username, self.password)78 s.headers.update({'Content-Type': 'application/json; charset=utf-8'})79 data = s.post(self.base_url + 'vms/%s/set_power_state/' %vm_uuid, json=body, verify=False)80 return data81 def vm_restore(self, vm_uuid, body=None):82 '''snap_uuid is required field in body'''83 requests.packages.urllib3.disable_warnings()84 s = requests.Session()85 s.auth = (self.username, self.password)86 s.headers.update({'Content-Type': 'application/json; charset=utf-8'})87 data = s.post(self.base_url + 'vms/%s/restore' %(vm_uuid), json=body ,verify=False)88 return data89 def vm_add_nics(self, vm_uuid, body=None):90 '''network_uuid is required field'''91 requests.packages.urllib3.disable_warnings()92 s = requests.Session()93 s.auth = (self.username, self.password)94 s.headers.update({'Content-Type': 'application/json; charset=utf-8'})95 data = s.post(self.base_url + 'vms/%s/nics' %vm_uuid, json=body ,verify=False)96 return data97## SNAPSHOT operations98 def snapshot_vm(self, body):99 requests.packages.urllib3.disable_warnings()100 s = requests.Session()101 s.auth = (self.username, self.password)102 s.headers.update({'Content-Type': 'application/json; charset=utf-8'})103 data = s.post(self.base_url + '/snapshots/', json=body ,verify=False)104 return data105## Network operations106 def network_create(self, body):107 requests.packages.urllib3.disable_warnings()108 s = requests.Session()109 s.auth = (self.username, self.password)110 s.headers.update({'Content-Type': 'application/json; charset=utf-8'})111 data = s.post(self.base_url + '/networks/', json=body ,verify=False)112 return data113## Alerts114 def get_alerts(self):115 requests.packages.urllib3.disable_warnings()116 s = requests.Session()117 s.auth = (self.username, self.password)118 s.headers.update({'Content-Type': 'application/json; charset=utf-8'})119 data = s.get(self.base_url + 'alerts/', verify=False)120 return data121## Set public key122 def set_public_key(self, name, key):123 requests.packages.urllib3.disable_warnings()124 s = requests.Session()125 s.auth = (self.username, self.password)126 s.headers.update({'Content-Type': 'application/json; charset=utf-8'})127 body = {'name': name, 'key' : key}128 data = s.post(self.base_url + 'cluster/public_keys/', json=body, verify=False)...
shortenurl.py
Source:shortenurl.py
...19 raise TypeError20 except (UnicodeEncodeError, TypeError):21 longurl = cget('http://tinyurl.com/api-create.php', params=dict(url=longurl)).text22 if "shorte.st" in SHORTENER:23 disable_warnings()24 return cget(f'http://api.shorte.st/stxt/{SHORTENER_API}/{longurl}', verify=False).text25 elif "linkvertise" in SHORTENER:26 url = quote(b64encode(longurl.encode("utf-8")))27 linkvertise = [28 f"https://link-to.net/{SHORTENER_API}/{random() * 1000}/dynamic?r={url}",29 f"https://up-to-down.net/{SHORTENER_API}/{random() * 1000}/dynamic?r={url}",30 f"https://direct-link.net/{SHORTENER_API}/{random() * 1000}/dynamic?r={url}",31 f"https://file-link.net/{SHORTENER_API}/{random() * 1000}/dynamic?r={url}"]32 return choice(linkvertise)33 elif "bitly.com" in SHORTENER:34 shorten_url = "https://api-ssl.bit.ly/v4/shorten"35 headers = {"Authorization": f"Bearer {SHORTENER_API}"}36 response = create_scraper().post(shorten_url, json={"long_url": longurl}, headers=headers).json()37 return response["link"]38 elif "ouo.io" in SHORTENER:39 disable_warnings()40 return cget(f'http://ouo.io/api/{SHORTENER_API}?s={longurl}', verify=False).text41 elif "adfoc.us" in SHORTENER:42 disable_warnings()43 return cget(f'http://adfoc.us/api/?key={SHORTENER_API}&url={longurl}', verify=False).text44 elif "cutt.ly" in SHORTENER:45 disable_warnings()46 return cget(f'http://cutt.ly/api/api.php?key={SHORTENER_API}&short={longurl}', verify=False).json()['url']['shortLink']47 elif "shrinkearn.com" in SHORTENER:48 disable_warnings()49 return cget(f'https://{SHORTENER}/api?api={SHORTENER_API}&url={longurl}', verify=False).json()['shortenedUrl']50 elif "shrtfly.com" in SHORTENER:51 disable_warnings()52 return cget(f'https://{SHORTENER}/api?api={SHORTENER_API}&url={longurl}', verify=False).json()['shortenedUrl']53 else:54 return cget(f'https://{SHORTENER}/api?api={SHORTENER_API}&url={quote(longurl)}&format=text').text55 except Exception as e:56 LOGGER.error(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!!