test_unittest.py revision 5c1bb2564e01e94f1fccb9272dbd613666db3e63
1#!/usr/bin/python2.4 2 3"""Unit Tests for autotest.client.common_lib.test""" 4 5__author__ = 'gps@google.com (Gregory P. Smith)' 6 7import unittest 8from cStringIO import StringIO 9import common 10from autotest_lib.client.common_lib import error, test, debug 11from autotest_lib.client.common_lib.test_utils import mock 12 13class TestTestCase(unittest.TestCase): 14 class _neutered_base_test(test.base_test): 15 """A child class of base_test to avoid calling the constructor.""" 16 def __init__(self, *args, **kwargs): 17 self.test_log = debug.get_logger(module='tests') 18 19 class MockJob(object): 20 pass 21 class MockProfilerManager(object): 22 def active(self): 23 return False 24 self.job = MockJob() 25 self.job.profilers = MockProfilerManager() 26 27 28 def setUp(self): 29 self.god = mock.mock_god() 30 self.test = self._neutered_base_test() 31 32 33 def tearDown(self): 34 self.god.unstub_all() 35 36 37 38class Test_base_test_execute(TestTestCase): 39 # Test the various behaviors of the base_test.execute() method. 40 def setUp(self): 41 TestTestCase.setUp(self) 42 self.god.stub_function(self.test, 'run_once_profiling') 43 self.god.stub_function(self.test, 'postprocess') 44 45 46 def test_call_run_once(self): 47 # setup 48 self.god.stub_function(self.test, 'drop_caches_between_iterations') 49 self.god.stub_function(self.test, 'run_once') 50 self.god.stub_function(self.test, 'postprocess_iteration') 51 before_hook = self.god.create_mock_function('before_hook') 52 after_hook = self.god.create_mock_function('after_hook') 53 54 # tests the test._call_run_once implementation 55 self.test.drop_caches_between_iterations.expect_call() 56 before_hook.expect_call() 57 self.test.run_once.expect_call(1, 2, arg='val') 58 after_hook.expect_call() 59 self.test.postprocess_iteration.expect_call() 60 self.test._call_run_once(before_hook, after_hook, (1, 2), 61 {'arg': 'val'}) 62 63 64 def _expect_call_run_once(self): 65 self.test._call_run_once.expect_call(None, None, (), {}) 66 67 68 def test_execute_test_length(self): 69 # test that test_length overrides iterations and works. 70 self.god.stub_function(self.test, '_call_run_once') 71 72 self._expect_call_run_once() 73 self._expect_call_run_once() 74 self._expect_call_run_once() 75 self.test.run_once_profiling.expect_call(None) 76 self.test.postprocess.expect_call() 77 78 fake_time = iter(xrange(4)).next 79 self.test.execute(iterations=1, test_length=3, _get_time=fake_time) 80 self.god.check_playback() 81 82 83 def test_execute_iterations(self): 84 # test that iterations works. 85 self.god.stub_function(self.test, '_call_run_once') 86 87 iterations = 2 88 for _ in range(iterations): 89 self._expect_call_run_once() 90 self.test.run_once_profiling.expect_call(None) 91 self.test.postprocess.expect_call() 92 93 self.test.execute(iterations=iterations) 94 self.god.check_playback() 95 96 97 def _mock_calls_for_execute_no_iterations(self): 98 self.test.run_once_profiling.expect_call(None) 99 self.test.postprocess.expect_call() 100 101 102 def test_execute_iteration_zero(self): 103 # test that iterations=0 works. 104 self._mock_calls_for_execute_no_iterations() 105 106 self.test.execute(iterations=0) 107 self.god.check_playback() 108 109 110 def test_execute_profile_only(self): 111 # test that profile_only=True works. (same as iterations=0) 112 self._mock_calls_for_execute_no_iterations() 113 114 self.test.execute(profile_only=True, iterations=2) 115 self.god.check_playback() 116 117 118 def test_execute_postprocess_profiled_false(self): 119 # test that postprocess_profiled_run=False works 120 self.god.stub_function(self.test, '_call_run_once') 121 122 self._expect_call_run_once() 123 self.test.run_once_profiling.expect_call(False) 124 self.test.postprocess.expect_call() 125 126 self.test.execute(postprocess_profiled_run=False, iterations=1) 127 self.god.check_playback() 128 129 130 def test_execute_postprocess_profiled_true(self): 131 # test that postprocess_profiled_run=True works 132 self.god.stub_function(self.test, '_call_run_once') 133 134 self._expect_call_run_once() 135 self.test.run_once_profiling.expect_call(True) 136 self.test.postprocess.expect_call() 137 138 self.test.execute(postprocess_profiled_run=True, iterations=1) 139 self.god.check_playback() 140 141 142if __name__ == '__main__': 143 unittest.main() 144