diff options
Diffstat (limited to 'Lib/test/test_timeit.py')
-rw-r--r-- | Lib/test/test_timeit.py | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py index 09e76e023d..83c93f7d90 100644 --- a/Lib/test/test_timeit.py +++ b/Lib/test/test_timeit.py @@ -5,7 +5,6 @@ import io import time from textwrap import dedent -from test.support import run_unittest from test.support import captured_stdout from test.support import captured_stderr @@ -98,9 +97,10 @@ class TestTimeit(unittest.TestCase): def fake_callable_stmt(self): self.fake_timer.inc() - def timeit(self, stmt, setup, number=None): + def timeit(self, stmt, setup, number=None, globals=None): self.fake_timer = FakeTimer() - t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer) + t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer, + globals=globals) kwargs = {} if number is None: number = DEFAULT_NUMBER @@ -139,6 +139,17 @@ class TestTimeit(unittest.TestCase): timer=FakeTimer()) self.assertEqual(delta_time, 0) + def test_timeit_globals_args(self): + global _global_timer + _global_timer = FakeTimer() + t = timeit.Timer(stmt='_global_timer.inc()', timer=_global_timer) + self.assertRaises(NameError, t.timeit, number=3) + timeit.timeit(stmt='_global_timer.inc()', timer=_global_timer, + globals=globals(), number=3) + local_timer = FakeTimer() + timeit.timeit(stmt='local_timer.inc()', timer=local_timer, + globals=locals(), number=3) + def repeat(self, stmt, setup, repeat=None, number=None): self.fake_timer = FakeTimer() t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer) @@ -300,6 +311,26 @@ class TestTimeit(unittest.TestCase): 10000 loops, best of 3: 50 usec per loop """)) + def test_main_with_time_unit(self): + unit_sec = self.run_main(seconds_per_increment=0.002, + switches=['-u', 'sec']) + self.assertEqual(unit_sec, + "1000 loops, best of 3: 0.002 sec per loop\n") + unit_msec = self.run_main(seconds_per_increment=0.002, + switches=['-u', 'msec']) + self.assertEqual(unit_msec, + "1000 loops, best of 3: 2 msec per loop\n") + unit_usec = self.run_main(seconds_per_increment=0.002, + switches=['-u', 'usec']) + self.assertEqual(unit_usec, + "1000 loops, best of 3: 2e+03 usec per loop\n") + # Test invalid unit input + with captured_stderr() as error_stringio: + invalid = self.run_main(seconds_per_increment=0.002, + switches=['-u', 'parsec']) + self.assertEqual(error_stringio.getvalue(), + "Unrecognized unit. Please select usec, msec, or sec.\n") + def test_main_exception(self): with captured_stderr() as error_stringio: s = self.run_main(switches=['1/0']) @@ -311,8 +342,5 @@ class TestTimeit(unittest.TestCase): self.assert_exc_string(error_stringio.getvalue(), 'ZeroDivisionError') -def test_main(): - run_unittest(TestTimeit) - if __name__ == '__main__': - test_main() + unittest.main() |