55from six .moves import reload_module # @UnresolvedImport
66
77import pytest_profiling
8+
89reload_module (pytest_profiling )
910
11+ import os
12+ import subprocess
13+
1014from pytest_profiling import Profiling , pytest_addoption , pytest_configure
1115
1216try :
13- from unittest .mock import Mock , ANY , patch , sentinel
17+ from unittest .mock import Mock , ANY , patch , sentinel , call
1418except ImportError :
1519 # python 2
1620 from mock import Mock , ANY , patch , sentinel
1721
1822
1923def test_creates_prof_dir ():
20- with patch (' os.makedirs' , side_effect = OSError ) as makedirs :
24+ with patch (" os.makedirs" , side_effect = OSError ) as makedirs :
2125 Profiling (False ).pytest_sessionstart (Mock ())
22- makedirs .assert_called_with (' prof' )
26+ makedirs .assert_called_with (" prof" )
2327
2428
2529def test_combines_profs ():
2630 plugin = Profiling (False )
2731 plugin .profs = [sentinel .prof0 , sentinel .prof1 ]
28- with patch (' pstats.Stats' ) as Stats :
32+ with patch (" pstats.Stats" ) as Stats :
2933 plugin .pytest_sessionfinish (Mock (), Mock ())
3034 Stats .assert_called_once_with (sentinel .prof0 )
3135 Stats .return_value .add .assert_called_once_with (sentinel .prof1 )
@@ -34,51 +38,73 @@ def test_combines_profs():
3438
3539def test_generates_svg ():
3640 plugin = Profiling (True )
41+ plugin .gprof2dot = "/somewhere/gprof2dot"
3742 plugin .profs = [sentinel .prof ]
38- with patch ('pstats.Stats' ):
39- with patch ('pipes.Template' ) as Template :
43+ popen1 = Mock (
44+ communicate = Mock (return_value = [None , None ]), poll = Mock (return_value = 0 )
45+ )
46+ popen2 = Mock (
47+ communicate = Mock (return_value = [None , None ]), poll = Mock (return_value = 0 )
48+ )
49+ with patch ("pstats.Stats" ):
50+ with patch ("subprocess.Popen" , side_effect = [popen1 , popen2 ]) as popen :
4051 plugin .pytest_sessionfinish (Mock (), Mock ())
41- assert any ('gprof2dot' in args [0 ][0 ] for args in Template .return_value .append .call_args_list )
42- assert Template .return_value .copy .called
52+ calls = popen .mock_calls
53+ assert calls [0 ] == call (
54+ ["dot" , "-Tsvg" , "-o" , f"{ os .getcwd ()} /prof/combined.svg" ],
55+ stdin = subprocess .PIPE ,
56+ shell = True ,
57+ )
58+ assert calls [1 ] == call (
59+ ["/somewhere/gprof2dot" , "-f" , "pstats" , f"{ os .getcwd ()} /prof/combined.prof" ],
60+ stdout = popen1 .stdin ,
61+ shell = True ,
62+ )
4363
4464
4565def test_writes_summary ():
4666 plugin = Profiling (False )
4767 plugin .profs = [sentinel .prof ]
4868 terminalreporter , stats = Mock (), Mock ()
49- with patch (' pstats.Stats' , return_value = stats ) as Stats :
69+ with patch (" pstats.Stats" , return_value = stats ) as Stats :
5070 plugin .pytest_sessionfinish (Mock (), Mock ())
5171 plugin .pytest_terminal_summary (terminalreporter )
52- assert ' Profiling' in terminalreporter .write .call_args [0 ][0 ]
72+ assert " Profiling" in terminalreporter .write .call_args [0 ][0 ]
5373 assert Stats .called_with (stats , stream = terminalreporter )
5474
5575
5676def test_writes_summary_svg ():
5777 plugin = Profiling (True )
5878 plugin .profs = [sentinel .prof ]
5979 terminalreporter = Mock ()
60- with patch ('pstats.Stats' ):
61- with patch ('pipes.Template' ):
80+ popen1 = Mock (
81+ communicate = Mock (return_value = [None , None ]), poll = Mock (return_value = 0 )
82+ )
83+ popen2 = Mock (
84+ communicate = Mock (return_value = [None , None ]), poll = Mock (return_value = 0 )
85+ )
86+ with patch ("pstats.Stats" ):
87+ with patch ("subprocess.Popen" , side_effect = [popen1 , popen2 ]):
6288 plugin .pytest_sessionfinish (Mock (), Mock ())
6389 plugin .pytest_terminal_summary (terminalreporter )
64- assert ' SVG' in terminalreporter .write .call_args [0 ][0 ]
90+ assert " SVG" in terminalreporter .write .call_args [0 ][0 ]
6591
6692
6793def test_adds_options ():
6894 parser = Mock ()
6995 pytest_addoption (parser )
70- parser .getgroup .assert_called_with (' Profiling' )
96+ parser .getgroup .assert_called_with (" Profiling" )
7197 group = parser .getgroup .return_value
72- group .addoption .assert_any_call (' --profile' , action = ' store_true' , help = ANY )
73- group .addoption .assert_any_call (' --profile-svg' , action = ' store_true' , help = ANY )
98+ group .addoption .assert_any_call (" --profile" , action = " store_true" , help = ANY )
99+ group .addoption .assert_any_call (" --profile-svg" , action = " store_true" , help = ANY )
74100
75101
76102def test_configures ():
77- config = Mock (getvalue = lambda x : x == ' profile' )
78- with patch (' pytest_profiling.Profiling' ) as Profiling :
103+ config = Mock (getvalue = lambda x : x == " profile" )
104+ with patch (" pytest_profiling.Profiling" ) as Profiling :
79105 pytest_configure (config )
80106 config .pluginmanager .register .assert_called_with (Profiling .return_value )
81107
82108
83109def test_clean_filename ():
84- assert pytest_profiling .clean_filename (' a:b/c\256 d' ) == ' a_b_c_d'
110+ assert pytest_profiling .clean_filename (" a:b/c\256 d" ) == " a_b_c_d"
0 commit comments