Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ShauryaDusht wants to merge5 commits intopython:main
base:main
Choose a base branch
Loading
fromShauryaDusht:gh-131178-profile-cli-tests
Open
Changes fromall commits
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
115 changes: 115 additions & 0 deletionsLib/test/test_profile.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,6 +4,9 @@
import pstats
import unittest
import os
import subprocess
import tempfile
import shutil
from difflib import unified_diff
from io import StringIO
from test.support.os_helper import TESTFN, unlink, temp_dir, change_cwd
Expand DownExpand Up@@ -131,6 +134,118 @@ 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."""

def setUp(self):
self.temp_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.temp_dir)

self.script_content = """\
def factorial(n):
if n <= 1:
return 1
return n * factorial(n-1)

if __name__ == "__main__":
factorial(10)
"""
self.script_file = os.path.join(self.temp_dir, "factorial_script.py")
with open(self.script_file, "w") as f:
f.write(self.script_content)

def _run_profile_cli(self, *args):
cmd = [sys.executable, '-m', 'profile', *args]
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True
)
stdout, stderr = proc.communicate()
return proc.returncode, stdout, stderr

def test_basic_profile(self):
returncode, stdout, stderr = self._run_profile_cli(self.script_file)
self.assertEqual(returncode, 0)
self.assertIn("function calls", stdout)
self.assertIn("factorial", stdout)
self.assertIn("ncalls", stdout)

def test_sort_options(self):
sort_options = ['calls', 'cumulative', 'cumtime', 'file',
'filename', 'module', 'ncalls', 'pcalls',
'line', 'stdname', 'time', 'tottime']

for option in sort_options:
with self.subTest(sort_option=option):
returncode, stdout, stderr = self._run_profile_cli(
'-s', option, self.script_file
)
self.assertEqual(returncode, 0)
self.assertIn("function calls", stdout)

def test_output_file(self):
output_file = os.path.join(self.temp_dir, "profile_output.prof")

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

self.assertTrue(os.path.exists(output_file))
stats = pstats.Stats(output_file)
self.assertGreater(stats.total_calls, 0)

def test_invalid_option(self):
returncode, stdout, stderr = self._run_profile_cli(
'--invalid-option', self.script_file
)
self.assertNotEqual(returncode, 0)
self.assertIn("error", stderr.lower())

def test_no_arguments(self):
returncode, stdout, stderr = self._run_profile_cli()
self.assertNotEqual(returncode, 0)

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"
)

def test_run_module(self):
module_name = "profilemod"
module_file = os.path.join(self.temp_dir, f"{module_name}.py")

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

env = os.environ.copy()

python_path = self.temp_dir
if 'PYTHONPATH' in env:
python_path = os.pathsep.join([python_path, env['PYTHONPATH']])
env['PYTHONPATH'] = python_path

cmd = [sys.executable, '-m', 'profile', '-m', module_name]
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
env=env
)
stdout, stderr = proc.communicate()
returncode = proc.returncode

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

[8]ページ先頭

©2009-2025 Movatter.jp