33import sys
44import pstats
55import unittest
6+ import os
67from difflib import unified_diff
78from 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
1012import profile
1113from test .profilee import testfunc , timer
1416class 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
7897def 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 ('\n if __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
99126def test_main ():
100127 run_unittest (ProfileTest )
0 commit comments