Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion Lib/test/test_cprofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

# rip off all interesting stuff from test_profile
import cProfile
from test.test_profile import ProfileTest, regenerate_expected_output
from test.test_profile import ProfileTest, ProfileCommandLineTest
from test.test_profile import regenerate_expected_output


class CProfileTest(ProfileTest):
Expand Down Expand Up @@ -36,8 +37,14 @@ def test_bad_counter_during_dealloc(self):
unlink(TESTFN)


class CProfileCommandLineTest(ProfileCommandLineTest):

profilename = 'cProfile'


def test_main():
run_unittest(CProfileTest)
run_unittest(CProfileCommandLineTest)

def main():
if '-r' not in sys.argv:
Expand Down
76 changes: 76 additions & 0 deletions Lib/test/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
from contextlib import contextmanager

import profile
from test import support
from test.profilee import testfunc, timer
from test.support import script_helper

TEMPDIR = os.path.abspath(support.TESTFN) + '-profiledir'
outputdir = TEMPDIR + '-output-test'
outputname = os.path.join(outputdir, 'output_stats')


class ProfileTest(unittest.TestCase):
Expand Down Expand Up @@ -94,6 +100,75 @@ def test_runctx(self):
self.assertTrue(os.path.exists(TESTFN))


class ProfileCommandLineTest(unittest.TestCase):

profilename = 'profile'

def profilecmd(self, *args, **kwargs):
rc, out, err = script_helper.assert_python_ok('-m', self.profilename,
*args, **kwargs)
return out.replace(os.linesep.encode(), b'\n')

def profilecmd_failure(self, *args):
return script_helper.assert_python_failure('-m', self.profilename,
*args)

def test_outputfile(self):
for opt in '-o', '--outfile':
try:
scriptfile = support.findfile('profilee.py')
with support.temp_cwd(outputdir):
out = self.profilecmd(opt, outputname, scriptfile)
self.assertEqual(out, b'')
finally:
support.rmtree(outputdir)

def test_sort_by_call_count(self):
for opt in '-s', '--sort':
scriptfile = support.findfile('profilee.py')
out = self.profilecmd(opt, 'ncalls', scriptfile)
self.assertIn(b'Ordered by: call count', out)

def test_sort_by_total_time(self):
for opt in '-s', '--sort':
scriptfile = support.findfile('profilee.py')
for arg in 'time', 'tottime':
out = self.profilecmd(opt, arg, scriptfile)
self.assertIn(b'Ordered by: internal time', out)

def test_sort_by_cumulative_time(self):
for opt in '-s', '--sort':
scriptfile = support.findfile('profilee.py')
for arg in 'cumtime', 'cumulative':
out = self.profilecmd(opt, arg, scriptfile)
self.assertIn(b'Ordered by: cumulative time', out)

def test_sort_by_filename(self):
for opt in '-s', '--sort':
scriptfile = support.findfile('profilee.py')
for arg in 'file', 'filename', 'module':
out = self.profilecmd(opt, 'filename', scriptfile)
self.assertIn(b'Ordered by: file name', out)

def test_sort_by_primitive_call_count_time(self):
for opt in '-s', '--sort':
scriptfile = support.findfile('profilee.py')
out = self.profilecmd(opt, 'pcalls', scriptfile)
self.assertIn(b'Ordered by: primitive call count', out)

def test_sort_by_line_number(self):
for opt in '-s', '--sort':
scriptfile = support.findfile('profilee.py')
out = self.profilecmd(opt, 'line', scriptfile)
self.assertIn(b'Ordered by: line number', out)

def test_sort_by_standard_name(self):
for opt in '-s', '--sort':
scriptfile = support.findfile('profilee.py')
out = self.profilecmd(opt, 'stdname', scriptfile)
self.assertIn(b'Ordered by: standard name', out)


def regenerate_expected_output(filename, cls):
filename = filename.rstrip('co')
print('Regenerating %s...' % filename)
Expand Down Expand Up @@ -125,6 +200,7 @@ def silent():

def test_main():
run_unittest(ProfileTest)
run_unittest(ProfileCommandLineTest)

def main():
if '-r' not in sys.argv:
Expand Down