How to use test_tear_down method in Airtest

Best Python code snippet using Airtest

case.py

Source:case.py Github

copy

Full Screen

1#!/usr/bin/env python2# -*- coding: utf-8 -*-3#4# 获取config or testcases中的测试步骤5def check_testcases_node(testcases):6 if not testcases:7 raise Exception(8 "Please check yaml contents, testcases or config node cannot be empty")9# get config steps10def create_test_keywords(suite, yaml_testcase, method, uri, test_obj=None, index=0):11 '''12 1. create test 13 2. send requests14 3. create test keywords15 4. create test assertions16 '''17 from robot_yaml.model import testcases as tc18 from robot_yaml.model.requests import send_request19 from robot_yaml.running.suite import get_setup_step, get_teardown_step20 from robot_yaml.model.teststeps import create_step21 # ptn: parse testcases node22 try:23 name = get_case_name(24 yaml_testcase) or "call other api {}".format(str(index))25 tag = get_case_tag(yaml_testcase)26 session_name = get_case_session(yaml_testcase)27 assign_name = get_case_assign(yaml_testcase) or []28 steps = get_case_step(yaml_testcase)29 assertions = get_case_assertion(yaml_testcase)30 test_setup = get_setup_step(yaml_testcase)31 test_tear_down = get_teardown_step(yaml_testcase)32 test = test_obj or tc.create_test(suite, name, tag)33 # create test setup34 if test_setup:35 create_step(test, test_setup, 'setup')36 from robot.api import logger37 send_request(test, method, uri, yaml_testcase,38 session_name, assign_name)39 # create test steps40 tc.create_test_steps(test, steps)41 # create test assertions42 tc.create_test_assertions(test, assertions)43 # create test teardown44 if test_tear_down:45 create_step(test, test_tear_down, 'teardown')46 return test47 except Exception as e:48 raise e49def init_testcases(suite, cases, method, uri, path=None, api_yamls_obj=None, test_obj=None):50 '''51 This function will do:52 1. create suite.test53 2. create test keywords which are testcases in reference api yaml node[Option]54 3. create test keywords which are self yaml 55 Example: Logout need Login first, we assume run logout test, then need call login 56 You will see two Test: Login and Logout57 If you use second option:58 You will see one Test: Logout, but keywords include login keywords59 suite -- testsuites object60 case -- yaml testcases node61 method -- request method62 uri -- request uri63 path -- other api yaml files path64 api_yamls_obj -- config apis node65 test_obj -- suite test object66 '''67 # ptn: parse testcases node68 from robot_yaml.model import testcases as tc69 for index, tcase in enumerate(cases):70 # run other api testcases first71 create_test_keywords_of_other_apis(suite, path, api_yamls_obj)72 # run self testcases73 create_test_keywords(74 suite, tcase, method, uri, index=index)75 # second options76 # name = get_case_name(77 # tcase) or "call other api {}".format(str(index))78 # tag = get_case_tag(tcase)79 # test = test_obj or tc.create_test(suite, name, tag)80 # # run other api testcases first81 # create_test_keywords_of_other_apis(suite, path, api_yamls_obj, test)82 # # run self testcases83 # create_test_keywords(84 # suite, tcase, method, uri, test, index)85def create_test_keywords_of_other_apis(suite, path, api_yamls_obj, test_obj=None):86 '''87 1. Read yaml contents88 2. create test keywords89 '''90 import os91 from robot_yaml.parsing.yamlreader import YamlReader92 from robot_yaml.running import get_config_and_testcases93 from robot_yaml.model import testcases as tc94 if not api_yamls_obj:95 return96 try:97 reader = YamlReader()98 for index, api_case in enumerate(api_yamls_obj):99 file = api_case.get('file')100 file = os.path.join(path, file)101 _api_contents = reader.yaml_to_dict(file)102 configs, testcases = get_config_and_testcases(_api_contents)103 method = get_request_method(configs)104 uri = get_request_uri(configs)105 test = create_test_keywords(106 suite, testcases, method, uri, test_obj, index)107 save_response_value_from_other_apis(test, api_case)108 except Exception as e:109 raise e110def save_response_value_from_other_apis(test, extract_yaml_contents):111 '''112 run_func:113 - 114 keyword: extract_variable_from_reponse115 args: 116 - ${resp.json()}117 - access_token118 saved_var_names: 119 - 120 name: ${real_access_token}121 # type: set_global_variable, set_suite_variable, set_test_variable122 type: set_suite_variable123 '''124 if not extract_yaml_contents:125 return126 from robot_yaml.model.teststeps import create_step127 create_step(test, extract_yaml_contents)128def get_request_method(configs):129 from robot_yaml.datas.yaml_fields import METHOD130 check_testcases_node(configs)131 return configs.get(METHOD)132def get_request_uri(configs):133 from robot_yaml.datas.yaml_fields import URI134 check_testcases_node(configs)135 return configs.get(URI)136# get testcases steps137def get_case_name(case):138 from robot_yaml.datas.yaml_fields import NAME139 check_testcases_node(case)140 try:141 name = case[NAME]142 if not name:143 msg = "case name was none on yaml"144 logger.error(msg)145 raise Exception(msg)146 return name147 except KeyError as ke:148 return149 except Exception as e:150 raise e151def get_case_tag(case):152 from robot_yaml.datas.yaml_fields import TEST_TAGS153 check_testcases_node(case)154 return case.get(TEST_TAGS)155def get_case_assertion(case):156 from robot_yaml.datas.yaml_fields import TEST_ASSERTIONS157 check_testcases_node(case)158 return case.get(TEST_ASSERTIONS)159def get_case_step(case):160 from robot_yaml.datas.yaml_fields import TEST_STEPS161 check_testcases_node(case)162 return case.get(TEST_STEPS)163def get_case_headers(case):164 from robot_yaml.datas.yaml_fields import HEADERS165 check_testcases_node(case)166 return case.get(HEADERS)167def get_case_payloads(case):168 '''169 get testcases payloads170 '''171 from robot_yaml.datas.yaml_fields import TEST_PAYLOADS172 check_testcases_node(case)173 return case.get(TEST_PAYLOADS)174def get_case_data(case):175 '''176 get testcases data177 '''178 from robot_yaml.datas.yaml_fields import TEST_DATA179 check_testcases_node(case)180 return case.get(TEST_DATA)181def get_case_params(case):182 '''183 get testcases params184 '''185 from robot_yaml.datas.yaml_fields import TEST_PARAMS186 check_testcases_node(case)187 return case.get(TEST_PARAMS)188def get_case_json(case):189 '''190 get testcases json191 '''192 from robot_yaml.datas.yaml_fields import TEST_JSON193 check_testcases_node(case)194 return case.get(TEST_JSON)195def get_case_session(case):196 from robot_yaml.datas.yaml_fields import TEST_SESSION197 check_testcases_node(case)198 return case.get(TEST_SESSION)199def get_case_assign(case):200 from robot_yaml.datas.yaml_fields import ASSIGN201 check_testcases_node(case)...

Full Screen

Full Screen

tests.py

Source:tests.py Github

copy

Full Screen

...26 # create 27 def test_create_neighborhood(self):28 self.assertIsInstance(self.phase4, Neighborhood)29 # delete30 def test_tear_down(self):31 Neighborhood.objects.all().delete()32 33 34# # business35class BusinessTestCase(TestCase):36 def setUp(self):37 self.butcher = Business(b_name="leshanButcher",contacts="0712347809",email="butche@gmail.com")38 def test_instance(self):39 self.assertTrue(isinstance(self.butcher,Business))40 def test_get_business_id(self,id):41 self.phase4.business_id()42 n=Business.objects.get(id=1)43 self.assertTrue(len(n)>0)44 # update45 def test_update_business(self):46 self.phase4.update_business()47 update_business=Business.objects.all()48 self.assertTrue(len(update_business)>0)49# # Testing Save Method50 def test_save_method(self):51 self.butcher.save_business()52 businessess= Business.objects.all()53 self.assertTrue(len(businessess) > 0)54# # create 55 def test_create_business(self):56 self.assertIsInstance(self.butcher, Business)57 # delete58 def test_tear_down(self):59 Business.objects.all().delete()60 ...

Full Screen

Full Screen

test_ios_instruct_cmd.py

Source:test_ios_instruct_cmd.py Github

copy

Full Screen

...43 def test_do_proxy2(self):44 self.ihelper.do_proxy(9101, 9100)45 time.sleep(3)46 self.ihelper.remove_proxy(9101)47 def test_tear_down(self):48 port, _ = self.ihelper.setup_proxy(9100)49 self.assertTrue(is_port_open('localhost', port))50 self.ihelper.tear_down()51 time.sleep(3)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Airtest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful