Best Python code snippet using autotest_python
test_unittest.py
Source:test_unittest.py  
1#!/usr/bin/python2"""Unit Tests for autotest.client.common_lib.test"""3__author__ = 'gps@google.com (Gregory P. Smith)'4import unittest5from cStringIO import StringIO6import common7from autotest_lib.client.common_lib import error, test8from autotest_lib.client.common_lib.test_utils import mock9class TestTestCase(unittest.TestCase):10    class _neutered_base_test(test.base_test):11        """A child class of base_test to avoid calling the constructor."""12        def __init__(self, *args, **kwargs):13            class MockJob(object):14                pass15            class MockProfilerManager(object):16                def active(self):17                    return False18                def present(self):19                    return True20            self.job = MockJob()21            self.job.default_profile_only = False22            self.job.profilers = MockProfilerManager()23            self._new_keyval = False24            self.iteration = 025            self.before_iteration_hooks = []26            self.after_iteration_hooks = []27    def setUp(self):28        self.god = mock.mock_god()29        self.test = self._neutered_base_test()30    def tearDown(self):31        self.god.unstub_all()32class Test_base_test_execute(TestTestCase):33    # Test the various behaviors of the base_test.execute() method.34    def setUp(self):35        TestTestCase.setUp(self)36        self.god.stub_function(self.test, 'run_once_profiling')37        self.god.stub_function(self.test, 'postprocess')38        self.god.stub_function(self.test, 'process_failed_constraints')39    def test_call_run_once(self):40        # setup41        self.god.stub_function(self.test, 'drop_caches_between_iterations')42        self.god.stub_function(self.test, 'run_once')43        self.god.stub_function(self.test, 'postprocess_iteration')44        self.god.stub_function(self.test, 'analyze_perf_constraints')45        before_hook = self.god.create_mock_function('before_hook')46        after_hook = self.god.create_mock_function('after_hook')47        self.test.register_before_iteration_hook(before_hook)48        self.test.register_after_iteration_hook(after_hook)49        # tests the test._call_run_once implementation50        self.test.drop_caches_between_iterations.expect_call()51        before_hook.expect_call(self.test)52        self.test.run_once.expect_call(1, 2, arg='val')53        self.test.postprocess_iteration.expect_call()54        self.test.analyze_perf_constraints.expect_call([])55        after_hook.expect_call(self.test)56        self.test._call_run_once([], False, None, (1, 2), {'arg': 'val'})57        self.god.check_playback()58    def test_call_run_once_with_exception(self):59        # setup60        self.god.stub_function(self.test, 'drop_caches_between_iterations')61        self.god.stub_function(self.test, 'run_once')62        before_hook = self.god.create_mock_function('before_hook')63        after_hook = self.god.create_mock_function('after_hook')64        self.test.register_before_iteration_hook(before_hook)65        self.test.register_after_iteration_hook(after_hook)66        error = Exception('fail')67        # tests the test._call_run_once implementation68        self.test.drop_caches_between_iterations.expect_call()69        before_hook.expect_call(self.test)70        self.test.run_once.expect_call(1, 2, arg='val').and_raises(error)71        after_hook.expect_call(self.test)72        try:73            self.test._call_run_once([], False, None, (1, 2), {'arg': 'val'})74        except:75            pass76        self.god.check_playback()77    def _expect_call_run_once(self):78        self.test._call_run_once.expect_call((), False, None, (), {})79    def test_execute_test_length(self):80        # test that test_length overrides iterations and works.81        self.god.stub_function(self.test, '_call_run_once')82        self._expect_call_run_once()83        self._expect_call_run_once()84        self._expect_call_run_once()85        self.test.run_once_profiling.expect_call(None)86        self.test.postprocess.expect_call()87        self.test.process_failed_constraints.expect_call()88        fake_time = iter(xrange(4)).next89        self.test.execute(iterations=1, test_length=3, _get_time=fake_time)90        self.god.check_playback()91    def test_execute_iterations(self):92        # test that iterations works.93        self.god.stub_function(self.test, '_call_run_once')94        iterations = 295        for _ in range(iterations):96            self._expect_call_run_once()97        self.test.run_once_profiling.expect_call(None)98        self.test.postprocess.expect_call()99        self.test.process_failed_constraints.expect_call()100        self.test.execute(iterations=iterations)101        self.god.check_playback()102    def _mock_calls_for_execute_no_iterations(self):103        self.test.run_once_profiling.expect_call(None)104        self.test.postprocess.expect_call()105        self.test.process_failed_constraints.expect_call()106    def test_execute_iteration_zero(self):107        # test that iterations=0 works.108        self._mock_calls_for_execute_no_iterations()109        self.test.execute(iterations=0)110        self.god.check_playback()111    def test_execute_profile_only(self):112        # test that profile_only=True works.113        self.god.stub_function(self.test, 'drop_caches_between_iterations')114        self.test.drop_caches_between_iterations.expect_call()115        self.test.run_once_profiling.expect_call(None)116        self.test.drop_caches_between_iterations.expect_call()117        self.test.run_once_profiling.expect_call(None)118        self.test.postprocess.expect_call()119        self.test.process_failed_constraints.expect_call()120        self.test.execute(profile_only=True, iterations=2)121        self.god.check_playback()122    def test_execute_default_profile_only(self):123        # test that profile_only=True works.124        self.god.stub_function(self.test, 'drop_caches_between_iterations')125        for _ in xrange(3):126            self.test.drop_caches_between_iterations.expect_call()127            self.test.run_once_profiling.expect_call(None)128        self.test.postprocess.expect_call()129        self.test.process_failed_constraints.expect_call()130        self.test.job.default_profile_only = True131        self.test.execute(iterations=3)132        self.god.check_playback()133    def test_execute_postprocess_profiled_false(self):134        # test that postprocess_profiled_run=False works135        self.god.stub_function(self.test, '_call_run_once')136        self.test._call_run_once.expect_call((), False, False, (), {})137        self.test.run_once_profiling.expect_call(False)138        self.test.postprocess.expect_call()139        self.test.process_failed_constraints.expect_call()140        self.test.execute(postprocess_profiled_run=False, iterations=1)141        self.god.check_playback()142    def test_execute_postprocess_profiled_true(self):143        # test that postprocess_profiled_run=True works144        self.god.stub_function(self.test, '_call_run_once')145        self.test._call_run_once.expect_call((), False, True, (), {})146        self.test.run_once_profiling.expect_call(True)147        self.test.postprocess.expect_call()148        self.test.process_failed_constraints.expect_call()149        self.test.execute(postprocess_profiled_run=True, iterations=1)150        self.god.check_playback()151if __name__ == '__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!!
