Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b071d4f

Browse files
committed
profile/cProfile: add tests for run() and runctx() functions
1 parent f29fb5e commit b071d4f

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

Lib/test/test_cprofile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
# rip off all interesting stuff from test_profile
77
import cProfile
88
from test.test_profile import ProfileTest, regenerate_expected_output
9+
from test.profilee import testfunc
910

1011
class CProfileTest(ProfileTest):
1112
profilerclass = cProfile.Profile
13+
profilermodule = cProfile
1214
expected_max_output = "{built-in method max}"
1315

1416
def get_expected_output(self):

Lib/test/test_profile.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import sys
44
import pstats
55
import unittest
6+
import os
67
from difflib import unified_diff
78
from io import StringIO
8-
from test.support import run_unittest
9+
from test.support import TESTFN, run_unittest, unlink
10+
from contextlib import contextmanager
911

1012
import profile
1113
from test.profilee import testfunc, timer
@@ -14,9 +16,13 @@
1416
class ProfileTest(unittest.TestCase):
1517

1618
profilerclass = profile.Profile
19+
profilermodule = profile
1720
methodnames = ['print_stats', 'print_callers', 'print_callees']
1821
expected_max_output = ':0(max)'
1922

23+
def tearDown(self):
24+
unlink(TESTFN)
25+
2026
def get_expected_output(self):
2127
return _ProfileOutput
2228

@@ -74,6 +80,19 @@ def test_calling_conventions(self):
7480
self.assertIn(self.expected_max_output, res,
7581
"Profiling {0!r} didn't report max:\n{1}".format(stmt, res))
7682

83+
def test_run(self):
84+
with silent():
85+
self.profilermodule.run("testfunc()")
86+
self.profilermodule.run("testfunc()", filename=TESTFN)
87+
self.assertTrue(os.path.exists(TESTFN))
88+
89+
def test_runctx(self):
90+
with silent():
91+
self.profilermodule.runctx("testfunc()", globals(), locals())
92+
self.profilermodule.runctx("testfunc()", globals(), locals(),
93+
filename=TESTFN)
94+
self.assertTrue(os.path.exists(TESTFN))
95+
7796

7897
def regenerate_expected_output(filename, cls):
7998
filename = filename.rstrip('co')
@@ -95,6 +114,14 @@ def regenerate_expected_output(filename, cls):
95114
method, results[i+1]))
96115
f.write('\nif __name__ == "__main__":\n main()\n')
97116

117+
@contextmanager
118+
def silent():
119+
stdout = sys.stdout
120+
try:
121+
sys.stdout = StringIO()
122+
yield
123+
finally:
124+
sys.stdout = stdout
98125

99126
def test_main():
100127
run_unittest(ProfileTest)

0 commit comments

Comments
 (0)