Best Python code snippet using lemoncheesecake
filter.py
Source:filter.py
...163 def __bool__(self):164 return BaseTreeNodeFilter.__bool__(self) or any((self.passed, self.failed, self.grep))165 def _do_passed(self, step):166 return step.is_successful() if self.passed else True167 def _do_failed(self, step):168 return not step.is_successful() if self.failed else True169 def _do_grep(self, step):170 if not self.grep:171 return True172 return _grep(self.grep, (step,))173 def _apply_step_criteria(self, step):174 return self._apply_criteria(175 step, self._do_passed, self._do_failed, self._do_grep176 )177 def __call__(self, step):178 # type: (Step) -> bool179 assert isinstance(step, Step)180 # test result:181 if isinstance(step.parent_result, TestResult):...
UTSession.py
Source:UTSession.py
...52 elif callable(func_get_expt):53 self.assertEqual(body, func_get_expt())54 else:55 self.assertTrue(False)56 def _do_failed(self, do_func):57 self.assertRaises(ConnectionError or ReadTimeout, do_func)58 @httpretty.activate59 def _do_with_retry(self, do_func, func_check_request=None):60 self.retry_cnt = 061 def _retry(request, uri, headers):62 if func_check_request is not None and callable(func_check_request):63 func_check_request(request)64 print('Checking Request:{0}, Pass'.format(request.body))65 if self.retry_cnt == 5:66 print('Round:{0}, Response'.format(self.retry_cnt))67 return 200, headers, 'Mock, Connection succeed'68 else:69 print('Round:{0}, Timeout'.format(self.retry_cnt))70 self.retry_cnt += 171 raise socket.timeout()72 httpretty.register_uri(httpretty.GET, 'http://haijia.bjxueche.net/', body=_retry)73 httpretty.register_uri(httpretty.POST, 'http://haijia.bjxueche.net/', body=_retry)74 self.dut.set_max_retry(for_url='http://haijia.bjxueche.net/', max_retries=6)75 body = do_func()76 self.assertEqual(body, 'Mock, Connection succeed')77 self.assertEqual(self.retry_cnt, 5)78 @httpretty.activate79 def _do_retried_but_failed(self, do_func, func_check_request=None):80 self.retry_cnt = 081 def _retry(request, uri, headers):82 if func_check_request is not None and callable(func_check_request):83 func_check_request(request)84 print('Checking Request:{0}, Pass'.format(request.body))85 print('Round:{0}, Timeout'.format(self.retry_cnt))86 self.retry_cnt += 187 raise socket.timeout()88 httpretty.register_uri(httpretty.GET, 'http://haijia.bjxueche.net/', body=_retry)89 httpretty.register_uri(httpretty.POST, 'http://haijia.bjxueche.net/', body=_retry)90 self.dut.set_max_retry(for_url='http://haijia.bjxueche.net/', max_retries=10)91 self.assertRaises(ConnectionError, do_func)92 self.assertEqual(self.retry_cnt, 11)93 def _do_timeout_failed(self, do_func, expt_time):94 start_time = time.time()95 self.assertRaises(Timeout, do_func)96 delta = time.time() - start_time97 print('Return after {0} seconds because of timeout '.format(delta))98 self.assertLessEqual(delta, expt_time)99 def _do_multi_timeout_failed(self, do_func, expt_time):100 start_time = time.time()101 self.assertRaises(ConnectionError, do_func)102 delta = time.time() - start_time103 print('Return after {0} seconds because of timeout '.format(delta))104 self.assertLessEqual(delta, expt_time)105 def test_connect_success(self):106 def _connect():107 resp = self.dut.connect()108 print(resp.text)109 return resp.text110 self._do_success(_connect)111 def test_connect_connection_failed(self):112 self.dut.urls['connect'] = 'http://haijia.bjxueche.ne/'113 self._do_failed(self.dut.connect)114 def test_connect_with_retry(self):115 def _test_connect():116 resp = self.dut.connect()117 print(resp.text)118 return resp.text119 self._do_with_retry(_test_connect)120 def test_connect_retried_but_failed(self):121 self._do_retried_but_failed(self.dut.connect)122 def test_connect_timeout_failed(self):123 self.dut.urls['connect'] = 'http://haijia.bjxueche.net:81/'124 self.dut.timeout_parameters['connect'] = 3125 self._do_timeout_failed(self.dut.connect, self.dut.timeout_parameters['connect']+1)126 def test_connect_multi_timeout_failed(self):127 self.dut.urls['connect'] = 'http://haijia.bjxueche.net:81/'128 self.dut.timeout_parameters['connect'] = 2129 self.dut.set_max_retry(for_url=self.dut.urls['connect'], max_retries=5)130 self._do_multi_timeout_failed(self.dut.connect, (self.dut.timeout_parameters['connect']+0.5)*5)131 def test_open_url_n_read_success(self):132 def _test():133 resp, body = self.dut.open_url_n_read('http://haijia.bjxueche.net/')134 return resp.text135 self._do_success(_test)136 def test_open_url_n_read_failed(self):137 def _test():138 resp, body = self.dut.open_url_n_read('http://haijia.bjxueche.ne/')139 return body140 self._do_failed(_test)141 def test_open_url_n_read_with_retry(self):142 def _test():143 resp, body = self.dut.open_url_n_read('http://haijia.bjxueche.net/')144 return body145 self._do_with_retry(_test)146 def test_open_url_n_read_retry_but_failed(self):147 def _test():148 resp, body = self.dut.open_url_n_read('http://haijia.bjxueche.net/')149 return resp.text150 self._do_retried_but_failed(_test)151 def test_open_url_n_read_timeout_failed(self):152 self.dut.timeout_parameters['open_url_n_read'] = 6153 def _test():154 resp, body = self.dut.open_url_n_read('http://haijia.bjxueche.net:81/')155 return resp.text156 def _test_with_timeout_args():157 resp, body = self.dut.open_url_n_read('http://haijia.bjxueche.net:81/', timeout=7)158 return resp.text159 self._do_timeout_failed(_test, self.dut.timeout_parameters['open_url_n_read']+1)160 self._do_timeout_failed(_test_with_timeout_args, 8)161 def test_open_url_n_read_multi_timeout_failed(self):162 self.dut.timeout_parameters['open_url_n_read'] = 2163 def _test():164 resp, body = self.dut.open_url_n_read('http://haijia.bjxueche.net:81/')165 return body166 self.dut.set_max_retry(for_url='http://haijia.bjxueche.net:81/', max_retries=5)167 self._do_multi_timeout_failed(_test, (self.dut.timeout_parameters['open_url_n_read']+0.5)*5)168 def test_post_with_response_success(self):169 def _post_with_response():170 resp, body = self.dut.post_with_response('http://haijia.bjxueche.net/', data='Posted Data')171 print(body)172 return body173 def chk_post_data(request, uri, headers):174 self.assertEqual(request.body, 'Posted Data')175 return 200, headers, 'Mock, Post succeed'176 self._do_success(_post_with_response, fake_body=chk_post_data, func_get_expt=lambda: 'Mock, Post succeed')177 def test_post_with_response_fail(self):178 def _post_with_response():179 resp, body = self.dut.post_with_response('http://haijia.bjxueche.ne/', data='Posted Data')180 print(body)181 return body182 self._do_failed(_post_with_response)183 def test_post_with_response_with_retry(self):184 def _test():185 resp, body = self.dut.post_with_response('http://haijia.bjxueche.net/', data='Posted Data')186 return body187 self._do_with_retry(_test, func_check_request=lambda r: self.assertEqual(r.body, 'Posted Data'))188 def test_post_with_response_retry_but_failed(self):189 def _test():190 resp, body = self.dut.post_with_response('http://haijia.bjxueche.net/', data='Posted Data')191 return body192 self._do_retried_but_failed(_test, func_check_request=lambda r: self.assertEqual(r.body, 'Posted Data'))193 def test_post_with_response_timeout_failed(self):194 self.dut.timeout_parameters['post_with_response'] = 3195 def _test():196 print('Calling Test function')...
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!!