Best Python code snippet using autotest_python
test_unittest.py
Source:test_unittest.py  
...46        self.god.stub_function(self.test, 'analyze_perf_constraints')47        before_hook = self.god.create_mock_function('before_hook')48        after_hook = self.god.create_mock_function('after_hook')49        self.test.register_before_iteration_hook(before_hook)50        self.test.register_after_iteration_hook(after_hook)51        # tests the test._call_run_once implementation52        self.test.drop_caches_between_iterations.expect_call()53        before_hook.expect_call(self.test)54        self.test.run_once.expect_call(1, 2, arg='val')55        self.test.postprocess_iteration.expect_call()56        self.test.analyze_perf_constraints.expect_call([])57        after_hook.expect_call(self.test)58        self.test._call_run_once([], False, None, (1, 2), {'arg': 'val'})59        self.god.check_playback()60    def test_call_run_once_with_exception(self):61        # setup62        self.god.stub_function(self.test, 'drop_caches_between_iterations')63        self.god.stub_function(self.test, 'run_once')64        before_hook = self.god.create_mock_function('before_hook')65        after_hook = self.god.create_mock_function('after_hook')66        self.test.register_before_iteration_hook(before_hook)67        self.test.register_after_iteration_hook(after_hook)68        error = Exception('fail')69        # tests the test._call_run_once implementation70        self.test.drop_caches_between_iterations.expect_call()71        before_hook.expect_call(self.test)72        self.test.run_once.expect_call(1, 2, arg='val').and_raises(error)73        after_hook.expect_call(self.test)74        try:75            self.test._call_run_once([], False, None, (1, 2), {'arg': 'val'})76        except:77            pass78        self.god.check_playback()79    def _setup_failed_test_calls(self, fail_count, error):80        """81        Set up failed test calls for use with call_run_once_with_retry.82        @param fail_count: The amount of times to mock a failure.83        @param error: The error to raise while failing.84        """85        self.god.stub_function(self.test.job, 'record')86        self.god.stub_function(self.test, '_call_run_once')87        # tests the test._call_run_once implementation88        for run in xrange(0, fail_count):89            self.test._call_run_once.expect_call([], False, None, (1, 2),90                                                 {'arg': 'val'}).and_raises(91                                                          error)92            info_str = 'Run %s failed with %s' % (run, error)93            # On the final run we do not emit this message.94            if run != self.test.job.test_retry and isinstance(error,95                                               common_lib_error.TestFailRetry):96                self.test.job.record.expect_call('INFO', None, None, info_str)97    def test_call_run_once_with_retry_exception(self):98        """99        Test call_run_once_with_retry duplicating a test that will always fail.100        """101        self.test.job.test_retry = 5102        self.god.stub_function(self.test, 'drop_caches_between_iterations')103        self.god.stub_function(self.test, 'run_once')104        before_hook = self.god.create_mock_function('before_hook')105        after_hook = self.god.create_mock_function('after_hook')106        self.test.register_before_iteration_hook(before_hook)107        self.test.register_after_iteration_hook(after_hook)108        error = common_lib_error.TestFailRetry('fail')109        self._setup_failed_test_calls(self.test.job.test_retry+1, error)110        try:111            self.test._call_run_once_with_retry([], False, None, (1, 2),112                                                {'arg': 'val'})113        except Exception as err:114            if err != error:115                raise116        self.god.check_playback()117    def test_call_run_once_with_retry_exception_unretryable(self):118        """119        Test call_run_once_with_retry duplicating a test that will always fail120        with a non-retryable exception.121        """122        self.test.job.test_retry = 5123        self.god.stub_function(self.test, 'drop_caches_between_iterations')124        self.god.stub_function(self.test, 'run_once')125        before_hook = self.god.create_mock_function('before_hook')126        after_hook = self.god.create_mock_function('after_hook')127        self.test.register_before_iteration_hook(before_hook)128        self.test.register_after_iteration_hook(after_hook)129        error = common_lib_error.TestFail('fail')130        self._setup_failed_test_calls(1, error)131        try:132            self.test._call_run_once_with_retry([], False, None, (1, 2),133                                                {'arg': 'val'})134        except Exception as err:135            if err != error:136                raise137        self.god.check_playback()138    def test_call_run_once_with_retry_exception_and_pass(self):139        """140        Test call_run_once_with_retry duplicating a test that fails at first141        and later passes.142        """143        # Stubbed out for the write_keyval call.144        self.test.outputdir = '/tmp'145        self.test.job._tap = None146        num_to_fail = 2147        self.test.job.test_retry = 5148        self.god.stub_function(self.test, 'drop_caches_between_iterations')149        self.god.stub_function(self.test, 'run_once')150        before_hook = self.god.create_mock_function('before_hook')151        after_hook = self.god.create_mock_function('after_hook')152        self.god.stub_function(self.test, '_call_run_once')153        self.test.register_before_iteration_hook(before_hook)154        self.test.register_after_iteration_hook(after_hook)155        self.god.stub_function(self.test.job, 'record')156        # tests the test._call_run_once implementation157        error = common_lib_error.TestFailRetry('fail')158        self._setup_failed_test_calls(num_to_fail, error)159        # Passing call160        self.test._call_run_once.expect_call([], False, None, (1, 2),161                                             {'arg': 'val'})162        self.test._call_run_once_with_retry([], False, None, (1, 2),163                                            {'arg': 'val'})164        self.god.check_playback()165    def _expect_call_run_once(self):166        self.test._call_run_once.expect_call((), False, None, (), {})167    def test_execute_test_length(self):168        # test that test_length overrides iterations and works....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!!
