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        after_hook.expect_call(self.test)54        self.test.postprocess_iteration.expect_call()55        self.test.analyze_perf_constraints.expect_call([])56        self.test._call_run_once([], False, None, (1, 2), {'arg': 'val'})57        self.god.check_playback()58    def _expect_call_run_once(self):59        self.test._call_run_once.expect_call((), False, None, (), {})60    def test_execute_test_length(self):61        # test that test_length overrides iterations and works.62        self.god.stub_function(self.test, '_call_run_once')63        self._expect_call_run_once()64        self._expect_call_run_once()65        self._expect_call_run_once()66        self.test.run_once_profiling.expect_call(None)67        self.test.postprocess.expect_call()68        self.test.process_failed_constraints.expect_call()69        fake_time = iter(xrange(4)).next70        self.test.execute(iterations=1, test_length=3, _get_time=fake_time)71        self.god.check_playback()72    def test_execute_iterations(self):73        # test that iterations works.74        self.god.stub_function(self.test, '_call_run_once')75        iterations = 276        for _ in range(iterations):77            self._expect_call_run_once()78        self.test.run_once_profiling.expect_call(None)79        self.test.postprocess.expect_call()80        self.test.process_failed_constraints.expect_call()81        self.test.execute(iterations=iterations)82        self.god.check_playback()83    def _mock_calls_for_execute_no_iterations(self):84        self.test.run_once_profiling.expect_call(None)85        self.test.postprocess.expect_call()86        self.test.process_failed_constraints.expect_call()87    def test_execute_iteration_zero(self):88        # test that iterations=0 works.89        self._mock_calls_for_execute_no_iterations()90        self.test.execute(iterations=0)91        self.god.check_playback()92    def test_execute_profile_only(self):93        # test that profile_only=True works.94        self.god.stub_function(self.test, 'drop_caches_between_iterations')95        self.test.drop_caches_between_iterations.expect_call()96        self.test.run_once_profiling.expect_call(None)97        self.test.drop_caches_between_iterations.expect_call()98        self.test.run_once_profiling.expect_call(None)99        self.test.postprocess.expect_call()100        self.test.process_failed_constraints.expect_call()101        self.test.execute(profile_only=True, iterations=2)102        self.god.check_playback()103    def test_execute_default_profile_only(self):104        # test that profile_only=True works.105        self.god.stub_function(self.test, 'drop_caches_between_iterations')106        for _ in xrange(3):107            self.test.drop_caches_between_iterations.expect_call()108            self.test.run_once_profiling.expect_call(None)109        self.test.postprocess.expect_call()110        self.test.process_failed_constraints.expect_call()111        self.test.job.default_profile_only = True112        self.test.execute(iterations=3)113        self.god.check_playback()114    def test_execute_postprocess_profiled_false(self):115        # test that postprocess_profiled_run=False works116        self.god.stub_function(self.test, '_call_run_once')117        self.test._call_run_once.expect_call((), False, False, (), {})118        self.test.run_once_profiling.expect_call(False)119        self.test.postprocess.expect_call()120        self.test.process_failed_constraints.expect_call()121        self.test.execute(postprocess_profiled_run=False, iterations=1)122        self.god.check_playback()123    def test_execute_postprocess_profiled_true(self):124        # test that postprocess_profiled_run=True works125        self.god.stub_function(self.test, '_call_run_once')126        self.test._call_run_once.expect_call((), False, True, (), {})127        self.test.run_once_profiling.expect_call(True)128        self.test.postprocess.expect_call()129        self.test.process_failed_constraints.expect_call()130        self.test.execute(postprocess_profiled_run=True, iterations=1)131        self.god.check_playback()132if __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!!
