Best Python code snippet using autotest_python
test_unittest.py
Source:test_unittest.py  
1#!/usr/bin/python2"""Unit Tests for autotest.client.shared.test"""3__author__ = 'gps@google.com (Gregory P. Smith)'4import unittest5try:6    import autotest.common as common  # pylint: disable=W06117except ImportError:8    import common  # pylint: disable=W06119from autotest.client.shared import test10from autotest.client.shared.test_utils import mock11class TestTestCase(unittest.TestCase):12    class _neutered_base_test(test.base_test):13        """A child class of base_test to avoid calling the constructor."""14        def __init__(self, *args, **kwargs):15            class MockJob(object):16                pass17            class MockProfilerManager(object):18                def active(self):19                    return False20                def present(self):21                    return True22            self.job = MockJob()23            self.job.default_profile_only = False24            self.job.profilers = MockProfilerManager()25            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 Exception:77            pass78        self.god.check_playback()79    def _expect_call_run_once(self):80        self.test._call_run_once.expect_call((), False, None, (), {})81    def test_execute_test_length(self):82        # test that test_length overrides iterations and works.83        self.god.stub_function(self.test, '_call_run_once')84        self._expect_call_run_once()85        self._expect_call_run_once()86        self._expect_call_run_once()87        self.test.run_once_profiling.expect_call(None)88        self.test.postprocess.expect_call()89        self.test.process_failed_constraints.expect_call()90        fake_time = iter(xrange(4)).next91        self.test.execute(iterations=1, test_length=3, _get_time=fake_time)92        self.god.check_playback()93    def test_execute_iterations(self):94        # test that iterations works.95        self.god.stub_function(self.test, '_call_run_once')96        iterations = 297        for _ in range(iterations):98            self._expect_call_run_once()99        self.test.run_once_profiling.expect_call(None)100        self.test.postprocess.expect_call()101        self.test.process_failed_constraints.expect_call()102        self.test.execute(iterations=iterations)103        self.god.check_playback()104    def _mock_calls_for_execute_no_iterations(self):105        self.test.run_once_profiling.expect_call(None)106        self.test.postprocess.expect_call()107        self.test.process_failed_constraints.expect_call()108    def test_execute_iteration_zero(self):109        # test that iterations=0 works.110        self._mock_calls_for_execute_no_iterations()111        self.test.execute(iterations=0)112        self.god.check_playback()113    def test_execute_profile_only(self):114        # test that profile_only=True works.115        self.god.stub_function(self.test, 'drop_caches_between_iterations')116        self.test.drop_caches_between_iterations.expect_call()117        self.test.run_once_profiling.expect_call(None)118        self.test.drop_caches_between_iterations.expect_call()119        self.test.run_once_profiling.expect_call(None)120        self.test.postprocess.expect_call()121        self.test.process_failed_constraints.expect_call()122        self.test.execute(profile_only=True, iterations=2)123        self.god.check_playback()124    def test_execute_default_profile_only(self):125        # test that profile_only=True works.126        self.god.stub_function(self.test, 'drop_caches_between_iterations')127        for _ in xrange(3):128            self.test.drop_caches_between_iterations.expect_call()129            self.test.run_once_profiling.expect_call(None)130        self.test.postprocess.expect_call()131        self.test.process_failed_constraints.expect_call()132        self.test.job.default_profile_only = True133        self.test.execute(iterations=3)134        self.god.check_playback()135    def test_execute_postprocess_profiled_false(self):136        # test that postprocess_profiled_run=False works137        self.god.stub_function(self.test, '_call_run_once')138        self.test._call_run_once.expect_call((), False, False, (), {})139        self.test.run_once_profiling.expect_call(False)140        self.test.postprocess.expect_call()141        self.test.process_failed_constraints.expect_call()142        self.test.execute(postprocess_profiled_run=False, iterations=1)143        self.god.check_playback()144    def test_execute_postprocess_profiled_true(self):145        # test that postprocess_profiled_run=True works146        self.god.stub_function(self.test, '_call_run_once')147        self.test._call_run_once.expect_call((), False, True, (), {})148        self.test.run_once_profiling.expect_call(True)149        self.test.postprocess.expect_call()150        self.test.process_failed_constraints.expect_call()151        self.test.execute(postprocess_profiled_run=True, iterations=1)152        self.god.check_playback()153class test_subtest(unittest.TestCase):154    """155    Test subtest class.156    """157    def setUp(self):158        self.god = mock.mock_god(ut=self)159        self.god.stub_function(test.logging, 'error')160        self.god.stub_function(test.logging, 'info')161    def tearDown(self):162        self.god.unstub_all()163    def test_test_not_implemented_raise(self):164        test.logging.info.expect_any_call()165        test.logging.error.expect_any_call()166        test.logging.error.expect_any_call()167        test.logging.error.expect_any_call()168        test.logging.info.expect_call("Subtest (test_not_implement):"169                                      " --> FAIL")170        class test_not_implement(test.Subtest):171            pass172        self.assertRaises(NotImplementedError, test_not_implement)173    def test_clean_not_implemented_raise(self):174        test.logging.info.expect_any_call()175        test.logging.info.expect_any_call()176        class test_test_not_cleanup_implement(test.Subtest):177            def test(self):178                pass179        self.assertRaises(NotImplementedError, test_test_not_cleanup_implement)180    def test_fail_in_nofatal_test(self):181        test.logging.info.expect_any_call()182        test.logging.error.expect_any_call()183        test.logging.error.expect_any_call()184        test.logging.error.expect_any_call()185        test.logging.info.expect_call("Subtest (test_raise_in_nofatal"186                                      "_test): --> FAIL")187        class test_raise_in_nofatal_test(test.Subtest):188            @test.subtest_nocleanup189            def test(self):190                raise Exception("No fatal test.")191        test_raise_in_nofatal_test()192    def test_fail_in_fatal_test(self):193        test.logging.info.expect_any_call()194        test.logging.error.expect_any_call()195        test.logging.error.expect_any_call()196        test.logging.error.expect_any_call()197        test.logging.info.expect_call("Subtest (test_raise_in_fatal"198                                      "_test): --> FAIL")199        class test_raise_in_fatal_test(test.Subtest):200            @test.subtest_nocleanup201            @test.subtest_fatal202            def test(self):203                raise Exception("Fatal test.")204        self.assertRaises(Exception, test_raise_in_fatal_test)205    def test_pass_with_cleanup_test(self):206        test.logging.info.expect_any_call()207        test.logging.info.expect_call("Subtest (test_pass_test):"208                                      " --> PASS")209        class test_pass_test(test.Subtest):210            @test.subtest_fatal211            def test(self):212                pass213            def clean(self):214                pass215        test_pass_test()216    def test_results(self):217        test.logging.info.expect_any_call()218        test.logging.info.expect_call("Subtest (test_pass_test):"219                                      " --> PASS")220        test.logging.info.expect_any_call()221        test.logging.error.expect_any_call()222        test.logging.error.expect_any_call()223        test.logging.error.expect_any_call()224        test.logging.info.expect_call("Subtest (test_raise_in_nofatal"225                                      "_test): --> FAIL")226        # Reset test fail count.227        test.Subtest.failed = 0228        class test_pass_test(test.Subtest):229            @test.subtest_fatal230            def test(self):231                pass232            def clean(self):233                pass234        class test_raise_in_nofatal_test(test.Subtest):235            @test.subtest_nocleanup236            def test(self):237                raise Exception("No fatal test.")238        test_pass_test()239        test_raise_in_nofatal_test()240        self.assertEqual(test.Subtest.has_failed(), True,241                         "Subtest class did not catch subtest failure.")242        self.assertEqual(test.Subtest.failed, 1,243                         "Subtest count failure is wrong")244if __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!!
