Skip to content

Commit b9f6cfc

Browse files
authored
Merge pull request #1 from sivasaiboggu/fix/131178-cli-tests-cprofile
gh-131178: Add tests for cProfile command-line interface
2 parents 1cc5802 + 6c24d17 commit b9f6cfc

2 files changed

Lines changed: 76 additions & 3 deletions

File tree

‎Lib/test/test_profiling/test_tracing_profiler.py‎

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,83 @@ def test_bad_descriptor(self):
243243

244244

245245
class TestCommandLine(unittest.TestCase):
246-
def test_sort(self):
247-
rc, out, err = assert_python_failure('-m', 'cProfile', '-s', 'demo')
248-
self.assertGreater(rc, 0)
246+
def test_help(self):
247+
for flag in ('-h', '--help'):
248+
with self.subTest(flag=flag):
249+
_, out, _ = assert_python_ok('-m', 'cProfile', flag)
250+
self.assertIn(b'Usage: cProfile.py', out)
251+
self.assertIn(b'-o OUTFILE', out)
252+
self.assertIn(b'-s SORT', out)
253+
254+
def test_no_args(self):
255+
rc, out, _ = assert_python_failure('-m', 'cProfile')
256+
self.assertEqual(rc, 2)
257+
self.assertIn(b'Usage: cProfile.py', out)
258+
259+
def test_unknown_option(self):
260+
rc, _, err = assert_python_failure('-m', 'cProfile', '--unknown')
261+
self.assertEqual(rc, 2)
262+
self.assertIn(b'no such option', err)
263+
264+
def test_invalid_sort(self):
265+
rc, _, err = assert_python_failure('-m', 'cProfile', '-s', 'demo')
266+
self.assertEqual(rc, 2)
249267
self.assertIn(b"option -s: invalid choice: 'demo'", err)
250268

269+
def test_valid_sort(self):
270+
with tempfile.NamedTemporaryFile("w", suffix=".py",
271+
delete_on_close=False) as f:
272+
f.write("def f(): pass\nf()\n")
273+
f.close()
274+
self.addCleanup(support.os_helper.unlink, f.name)
275+
for sort_key in ('calls', 'cumtime', 'tottime', 'name'):
276+
for flag in ('-s', '--sort'):
277+
with self.subTest(sort_key=sort_key, flag=flag):
278+
_, out, _ = assert_python_ok(
279+
'-m', 'cProfile', flag, sort_key, f.name
280+
)
281+
self.assertIn(b'function calls', out)
282+
283+
def test_outfile(self):
284+
with tempfile.NamedTemporaryFile("w", suffix=".py",
285+
delete_on_close=False) as script:
286+
script.write("def f(): pass\nf()\n")
287+
script.close()
288+
self.addCleanup(support.os_helper.unlink, script.name)
289+
outfile = tempfile.NamedTemporaryFile(delete=False)
290+
outfile.close()
291+
self.addCleanup(support.os_helper.unlink, outfile.name)
292+
for flag in ('-o', '--outfile'):
293+
with self.subTest(flag=flag):
294+
assert_python_ok(
295+
'-m', 'cProfile', flag, outfile.name, script.name
296+
)
297+
with open(outfile.name, 'rb') as f:
298+
self.assertGreater(len(f.read()), 0)
299+
300+
def test_profile_script(self):
301+
with tempfile.NamedTemporaryFile("w", suffix=".py",
302+
delete_on_close=False) as f:
303+
f.write("def f(): pass\nf()\n")
304+
f.close()
305+
self.addCleanup(support.os_helper.unlink, f.name)
306+
_, out, _ = assert_python_ok('-m', 'cProfile', f.name)
307+
self.assertIn(b'function calls', out)
308+
self.assertIn(b'Ordered by', out)
309+
310+
def test_profile_module(self):
311+
_, out, _ = assert_python_ok(
312+
'-m', 'cProfile', '-m', 'timeit', '-n', '1', 'pass'
313+
)
314+
self.assertIn(b'function calls', out)
315+
316+
def test_nonexistent_script(self):
317+
rc, _, err = assert_python_failure(
318+
'-m', 'cProfile', 'nonexistent_script.py'
319+
)
320+
self.assertEqual(rc, 1)
321+
self.assertIn(b'No such file or directory', err)
322+
251323
def test_profile_script_importing_main(self):
252324
"""Check that scripts that reference __main__ see their own namespace
253325
when being profiled."""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add tests for the ``cProfile`` command-line interface.

0 commit comments

Comments
 (0)