diff --git a/Lib/test/test_cprofile.py b/Lib/test/test_cprofile.py index 192c8eab26ebff..7606ec41cebaf3 100644 --- a/Lib/test/test_cprofile.py +++ b/Lib/test/test_cprofile.py @@ -2,7 +2,7 @@ import sys import unittest - +import os # rip off all interesting stuff from test_profile import cProfile import tempfile @@ -151,23 +151,110 @@ def test_bad_descriptor(self): class TestCommandLine(unittest.TestCase): + def setUp(self): + self.test_script = tempfile.NamedTemporaryFile("w+", suffix=".py", delete=False) + self.test_script.write(textwrap.dedent(""" + def simple_function(): + sum(range(1000)) + + if __name__ == "__main__": + simple_function() + """)) + self.test_script.close() + + def tearDown(self): + os.unlink(self.test_script.name) + def test_sort(self): - rc, out, err = assert_python_failure('-m', 'cProfile', '-s', 'demo') + rc, _, err = assert_python_failure('-m', 'cProfile', '-s', 'demo') self.assertGreater(rc, 0) self.assertIn(b"option -s: invalid choice: 'demo'", err) + def test_valid_sort_options(self): + for sort_opt in ('calls', 'cumulative', 'cumtime', 'filename', + 'ncalls', 'pcalls', 'line', 'name', 'nfl', + 'stdname', 'time', 'tottime'): + rc, out, _ = assert_python_ok('-m', 'cProfile', '-s', sort_opt, self.test_script.name) + self.assertEqual(rc, 0) + self.assertIn(b"function calls", out) + + def test_outfile(self): + with tempfile.NamedTemporaryFile(suffix='.prof', delete=False) as outfile: + outfile_name = outfile.name + try: + rc, _, __ = assert_python_ok('-m', 'cProfile', '-o', outfile_name, self.test_script.name) + self.assertEqual(rc, 0) + self.assertTrue(os.path.exists(outfile_name)) + self.assertGreater(os.path.getsize(outfile_name), 0) + finally: + if os.path.exists(outfile_name): + os.unlink(outfile_name) + + def test_no_arguments(self): + rc, out, _ = assert_python_failure('-m', 'cProfile') + self.assertGreater(rc, 0) + self.assertIn(b'Usage:', out) + + def test_help_option(self): + rc, out, _ = assert_python_ok('-m', 'cProfile', '--help') + self.assertEqual(rc, 0) + self.assertIn(b"Usage:", out) + + def test_version_output(self): + rc, out, _ = assert_python_ok('-m', 'cProfile', self.test_script.name) + self.assertEqual(rc, 0) + self.assertIn(os.path.basename(self.test_script.name).encode(), out) + + def test_run_command_line_module(self): + rc, out, _ = assert_python_ok('-m', 'cProfile', '-m', 'timeit', '-n', '1', 'pass') + self.assertEqual(rc, 0) + self.assertIn(b"function calls", out) + def test_profile_script_importing_main(self): - """Check that scripts that reference __main__ see their own namespace - when being profiled.""" - with tempfile.NamedTemporaryFile("w+", delete_on_close=False) as f: + with tempfile.NamedTemporaryFile("w+", suffix='.py', delete=False) as f: f.write(textwrap.dedent("""\ - class Foo: - pass - import __main__ - assert Foo == __main__.Foo + def test_func(): + x = 1 + 1 + return x + + if __name__ == "__main__": + test_func() """)) f.close() - assert_python_ok('-m', "cProfile", f.name) + try: + rc, out, _ = assert_python_ok('-m', "cProfile", f.name) + self.assertEqual(rc, 0) + self.assertIn(b"function calls", out) + self.assertIn(b"test_func", out) + finally: + os.unlink(f.name) + + def test_output_format(self): + rc, out, _ = assert_python_ok('-m', 'cProfile', self.test_script.name) + self.assertEqual(rc, 0) + + self.assertRegex(out, rb'\d+ function calls in \d+\.\d+ seconds') + + self.assertIn(b'Ordered by:', out) + self.assertIn(b'ncalls', out) + self.assertIn(b'tottime', out) + self.assertIn(b'percall', out) + self.assertIn(b'cumtime', out) + self.assertIn(b'filename:lineno(function)', out) + self.assertIn(b'simple_function', out) + + def test_different_sort_outputs(self): + rc1, out1, _ = assert_python_ok('-m', 'cProfile', '-s', 'time', self.test_script.name) + rc2, out2, _ = assert_python_ok('-m', 'cProfile', '-s', 'cumulative', self.test_script.name) + + self.assertEqual(rc1, 0) + self.assertEqual(rc2, 0) + + self.assertRegex(out1.decode(), r'Ordered by: (?:internal )?time') + self.assertRegex(out2.decode(), r'Ordered by: cumulative') + + self.assertIn(b'simple_function', out1) + self.assertIn(b'simple_function', out2) def main():