How to use before_hook method in autotest

Best Python code snippet using autotest_python

test_unittest.py

Source:test_unittest.py Github

copy

Full Screen

1#!/usr/bin/python2#pylint: disable-msg=C01113"""Unit Tests for autotest.client.common_lib.test"""4__author__ = 'gps@google.com (Gregory P. Smith)'5import unittest6import common7from autotest_lib.client.common_lib import test8from autotest_lib.client.common_lib.test_utils import mock9from autotest_lib.client.common_lib import error as common_lib_error10class TestTestCase(unittest.TestCase):11 class _neutered_base_test(test.base_test):12 """A child class of base_test to avoid calling the constructor."""13 def __init__(self, *args, **kwargs):14 class MockJob(object):15 pass16 class MockProfilerManager(object):17 def active(self):18 return False19 def present(self):20 return True21 self.job = MockJob()22 self.job.default_profile_only = False23 self.job.profilers = MockProfilerManager()24 self.job.test_retry = 025 self._new_keyval = False26 self.iteration = 027 self.before_iteration_hooks = []28 self.after_iteration_hooks = []29 def setUp(self):30 self.god = mock.mock_god()31 self.test = self._neutered_base_test()32 def tearDown(self):33 self.god.unstub_all()34class Test_base_test_execute(TestTestCase):35 # Test the various behaviors of the base_test.execute() method.36 def setUp(self):37 TestTestCase.setUp(self)38 self.god.stub_function(self.test, 'run_once_profiling')39 self.god.stub_function(self.test, 'postprocess')40 self.god.stub_function(self.test, 'process_failed_constraints')41 def test_call_run_once(self):42 # setup43 self.god.stub_function(self.test, 'drop_caches_between_iterations')44 self.god.stub_function(self.test, 'run_once')45 self.god.stub_function(self.test, 'postprocess_iteration')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.169 self.god.stub_function(self.test, '_call_run_once')170 self._expect_call_run_once()171 self._expect_call_run_once()172 self._expect_call_run_once()173 self.test.run_once_profiling.expect_call(None)174 self.test.postprocess.expect_call()175 self.test.process_failed_constraints.expect_call()176 fake_time = iter(xrange(4)).next177 self.test.execute(iterations=1, test_length=3, _get_time=fake_time)178 self.god.check_playback()179 def test_execute_iterations(self):180 # test that iterations works.181 self.god.stub_function(self.test, '_call_run_once')182 iterations = 2183 for _ in range(iterations):184 self._expect_call_run_once()185 self.test.run_once_profiling.expect_call(None)186 self.test.postprocess.expect_call()187 self.test.process_failed_constraints.expect_call()188 self.test.execute(iterations=iterations)189 self.god.check_playback()190 def _mock_calls_for_execute_no_iterations(self):191 self.test.run_once_profiling.expect_call(None)192 self.test.postprocess.expect_call()193 self.test.process_failed_constraints.expect_call()194 def test_execute_iteration_zero(self):195 # test that iterations=0 works.196 self._mock_calls_for_execute_no_iterations()197 self.test.execute(iterations=0)198 self.god.check_playback()199 def test_execute_profile_only(self):200 # test that profile_only=True works.201 self.god.stub_function(self.test, 'drop_caches_between_iterations')202 self.test.drop_caches_between_iterations.expect_call()203 self.test.run_once_profiling.expect_call(None)204 self.test.drop_caches_between_iterations.expect_call()205 self.test.run_once_profiling.expect_call(None)206 self.test.postprocess.expect_call()207 self.test.process_failed_constraints.expect_call()208 self.test.execute(profile_only=True, iterations=2)209 self.god.check_playback()210 def test_execute_default_profile_only(self):211 # test that profile_only=True works.212 self.god.stub_function(self.test, 'drop_caches_between_iterations')213 for _ in xrange(3):214 self.test.drop_caches_between_iterations.expect_call()215 self.test.run_once_profiling.expect_call(None)216 self.test.postprocess.expect_call()217 self.test.process_failed_constraints.expect_call()218 self.test.job.default_profile_only = True219 self.test.execute(iterations=3)220 self.god.check_playback()221 def test_execute_postprocess_profiled_false(self):222 # test that postprocess_profiled_run=False works223 self.god.stub_function(self.test, '_call_run_once')224 self.test._call_run_once.expect_call((), False, False, (), {})225 self.test.run_once_profiling.expect_call(False)226 self.test.postprocess.expect_call()227 self.test.process_failed_constraints.expect_call()228 self.test.execute(postprocess_profiled_run=False, iterations=1)229 self.god.check_playback()230 def test_execute_postprocess_profiled_true(self):231 # test that postprocess_profiled_run=True works232 self.god.stub_function(self.test, '_call_run_once')233 self.test._call_run_once.expect_call((), False, True, (), {})234 self.test.run_once_profiling.expect_call(True)235 self.test.postprocess.expect_call()236 self.test.process_failed_constraints.expect_call()237 self.test.execute(postprocess_profiled_run=True, iterations=1)238 self.god.check_playback()239if __name__ == '__main__':...

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 autotest 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