Best Python code snippet using autotest_python
test_everything.py
Source:test_everything.py
...14 @retry(reraise=True, stop=stop_after_attempt(12))15 def test_simple_exception(self):16 flaky_exception()17 @retry(reraise=True, stop=stop_after_attempt(12))18 def test_simple_failure(self):19 flaky_fail(self)20 @retry(reraise=True, stop=stop_after_attempt(12))21 def test_simple_assertion(self):22 if random.random() < flaky_rate:23 assert False, "flaky assertion"24def make_callback(f):25 return lambda result, *args, **kwargs: f(*args, **kwargs)26def retry_deferred(*retry_args, **retry_kwargs):27 """28 Wrap a test method that may or may not return a Deferred29 result and ensure the underlying behavior honors the retry30 parameters. Honors the same parameters as ``retry``.31 """32 def decorator(f):33 @functools.wraps(f)34 def wrapper(*args, **kwargs):35 retried = retry(*retry_args, **retry_kwargs)(f)36 # invoke the function using retries37 result = retried(*args, **kwargs)38 # if the result is a Deferred, wrap the inner39 # callback(s) in retries40 if isinstance(result, Deferred):41 result.callbacks[:] = [42 (43 (44 retry(*retry_args, **retry_kwargs)(callback),45 callbackArgs,46 callbackKeywords,47 ),48 errback_spec,49 )50 for (51 callback, callbackArgs, callbackKeywords),52 errback_spec in result.callbacks53 ]54 return result55 return wrapper56 return decorator57def setup_deferred(f):58 """59 Wrap a synchronous test method to instead return a Deferred60 that is triggered after 100ms.61 """62 @functools.wraps(f)63 def wrapper(*args, **kwargs):64 result = Deferred()65 reactor.callLater(.1, result.callback, None)66 result.addCallback(make_callback(f), *args, **kwargs)67 return result68 return wrapper69def make_flaky(f):70 """71 Wrap a function to make it flaky before called.72 """73 @functools.wraps(f)74 def wrapper(*args, **kwargs):75 flaky_exception()76 return f(*args, **kwargs)77 return wrapper78class DeferredsTests(unittest.TestCase):79 @retry_deferred(reraise=True, stop=stop_after_attempt(12))80 @make_flaky81 @setup_deferred82 def test_simple_exception(self):83 flaky_exception()84 @retry_deferred(reraise=True, stop=stop_after_attempt(12))85 @make_flaky86 @setup_deferred87 def test_simple_failure(self):...
unittest_qunit.py
Source:unittest_qunit.py
...8 (js('test_simple_success.js'),),9 (js('test_with_dep.js'), (js('dep_1.js'),)),10 (js('test_with_ordered_deps.js'), (js('dep_1.js'), js('deps_2.js'),)),11 )12 def test_simple_failure(self):13 js_tests = list(self._test_qunit(js('test_simple_failure.js')))14 self.assertEqual(len(js_tests), 3)15 test_1, test_2, test_3 = js_tests16 self.assertRaises(self.failureException, test_1[1], *test_1[2:])17 self.assertRaises(self.failureException, test_2[1], *test_2[2:])18 test_3[1](*test_3[2:])19if __name__ == '__main__':20 from unittest import main...
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!!