Skip to content

gh-131178: Add CLI tests for profile module #134103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Lint fixes
  • Loading branch information
ShauryaDusht committed May 16, 2025
commit fd894ec9928dccfde970a80d744ce734a4537290
30 changes: 17 additions & 13 deletions Lib/test/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def test_output_file_when_changing_directory(self):

self.assertTrue(os.path.exists('out.pstats'))


class ProfileCLITests(unittest.TestCase):
"""Tests for the profile module's command line interface."""

Expand Down Expand Up @@ -173,9 +174,9 @@ def test_sort_options(self):
"""Test different sort options."""
# List of sort options known to work
sort_options = ['calls', 'cumulative', 'cumtime', 'file',
'filename', 'module', 'ncalls', 'pcalls',
'line', 'stdname', 'time', 'tottime']
'filename', 'module', 'ncalls', 'pcalls',
'line', 'stdname', 'time', 'tottime']

# Test each sort option individually
for option in sort_options:
with self.subTest(sort_option=option):
Expand All @@ -189,12 +190,12 @@ def test_output_file(self):
"""Test writing profile results to a file."""
output_file = TESTFN + '.prof'
self.addCleanup(unlink, output_file)

returncode, stdout, stderr = self._run_profile_cli(
'-o', output_file, self.script_file
)
self.assertEqual(returncode, 0)

# Check that the output file exists and contains profile data
self.assertTrue(os.path.exists(output_file))
stats = pstats.Stats(output_file)
Expand All @@ -212,32 +213,35 @@ def test_no_arguments(self):
"""Test behavior with no arguments."""
returncode, stdout, stderr = self._run_profile_cli()
self.assertNotEqual(returncode, 0)

# Check either stdout or stderr for usage information
combined_output = stdout.lower() + stderr.lower()
self.assertTrue("usage:" in combined_output or
"error:" in combined_output or
"no script filename specified" in combined_output,
"Expected usage information or error message not found")
self.assertTrue(
"usage:" in combined_output or
"error:" in combined_output or
"no script filename specified" in combined_output,
"Expected usage information or error message not found"
)

def test_run_module(self):
"""Test profiling a module with -m option."""
# Create a small module
module_name = "test_profile_module"
module_file = f"{module_name}.py"

with open(module_file, "w") as f:
f.write("print('Module executed')\n")

self.addCleanup(unlink, module_file)

returncode, stdout, stderr = self._run_profile_cli(
'-m', module_name
)
self.assertEqual(returncode, 0)
self.assertIn("Module executed", stdout)
self.assertIn("function calls", stdout)


def regenerate_expected_output(filename, cls):
filename = filename.rstrip('co')
print('Regenerating %s...' % filename)
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy