5
5
from six .moves import reload_module # @UnresolvedImport
6
6
7
7
import pytest_profiling
8
+
8
9
reload_module (pytest_profiling )
9
10
11
+ import os
12
+ import subprocess
13
+
10
14
from pytest_profiling import Profiling , pytest_addoption , pytest_configure
11
15
12
16
try :
13
- from unittest .mock import Mock , ANY , patch , sentinel
17
+ from unittest .mock import Mock , ANY , patch , sentinel , call
14
18
except ImportError :
15
19
# python 2
16
20
from mock import Mock , ANY , patch , sentinel
17
21
18
22
19
23
def test_creates_prof_dir ():
20
- with patch (' os.makedirs' , side_effect = OSError ) as makedirs :
24
+ with patch (" os.makedirs" , side_effect = OSError ) as makedirs :
21
25
Profiling (False ).pytest_sessionstart (Mock ())
22
- makedirs .assert_called_with (' prof' )
26
+ makedirs .assert_called_with (" prof" )
23
27
24
28
25
29
def test_combines_profs ():
26
30
plugin = Profiling (False )
27
31
plugin .profs = [sentinel .prof0 , sentinel .prof1 ]
28
- with patch (' pstats.Stats' ) as Stats :
32
+ with patch (" pstats.Stats" ) as Stats :
29
33
plugin .pytest_sessionfinish (Mock (), Mock ())
30
34
Stats .assert_called_once_with (sentinel .prof0 )
31
35
Stats .return_value .add .assert_called_once_with (sentinel .prof1 )
@@ -34,51 +38,73 @@ def test_combines_profs():
34
38
35
39
def test_generates_svg ():
36
40
plugin = Profiling (True )
41
+ plugin .gprof2dot = "/somewhere/gprof2dot"
37
42
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 :
40
51
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
+ )
43
63
44
64
45
65
def test_writes_summary ():
46
66
plugin = Profiling (False )
47
67
plugin .profs = [sentinel .prof ]
48
68
terminalreporter , stats = Mock (), Mock ()
49
- with patch (' pstats.Stats' , return_value = stats ) as Stats :
69
+ with patch (" pstats.Stats" , return_value = stats ) as Stats :
50
70
plugin .pytest_sessionfinish (Mock (), Mock ())
51
71
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 ]
53
73
assert Stats .called_with (stats , stream = terminalreporter )
54
74
55
75
56
76
def test_writes_summary_svg ():
57
77
plugin = Profiling (True )
58
78
plugin .profs = [sentinel .prof ]
59
79
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 ]):
62
88
plugin .pytest_sessionfinish (Mock (), Mock ())
63
89
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 ]
65
91
66
92
67
93
def test_adds_options ():
68
94
parser = Mock ()
69
95
pytest_addoption (parser )
70
- parser .getgroup .assert_called_with (' Profiling' )
96
+ parser .getgroup .assert_called_with (" Profiling" )
71
97
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 )
74
100
75
101
76
102
def 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 :
79
105
pytest_configure (config )
80
106
config .pluginmanager .register .assert_called_with (Profiling .return_value )
81
107
82
108
83
109
def 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