Skip to content

Commit 8cd63fa

Browse files
committed
Fixed server and profiling unit tests
1 parent 68d4f7c commit 8cd63fa

File tree

3 files changed

+56
-30
lines changed

3 files changed

+56
-30
lines changed

pytest-profiling/pytest_profiling.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pstats
99
import errno
1010
from hashlib import md5
11-
from subprocess import Popen, PIPE
11+
import subprocess
1212

1313
import six
1414
import pytest
@@ -71,8 +71,8 @@ def pytest_sessionfinish(self, session, exitstatus): # @UnusedVariable
7171

7272
# A handcrafted Popen pipe actually seems to work on both windows and unix:
7373
# do it in 2 subprocesses, with a pipe in between
74-
pdot = Popen(dot_args, stdin=PIPE, shell=True)
75-
pgprof = Popen(gprof2dot_args, stdout=pdot.stdin, shell=True)
74+
pdot = subprocess.Popen(dot_args, stdin=subprocess.PIPE, shell=True)
75+
pgprof = subprocess.Popen(gprof2dot_args, stdout=pdot.stdin, shell=True)
7676
(stdoutdata1, stderrdata1) = pgprof.communicate()
7777
(stdoutdata2, stderrdata2) = pdot.communicate()
7878
if stderrdata1 is not None or pgprof.poll() > 0:

pytest-profiling/tests/unit/test_profile.py

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,31 @@
55
from six.moves import reload_module # @UnresolvedImport
66

77
import pytest_profiling
8+
89
reload_module(pytest_profiling)
910

11+
import os
12+
import subprocess
13+
1014
from pytest_profiling import Profiling, pytest_addoption, pytest_configure
1115

1216
try:
13-
from unittest.mock import Mock, ANY, patch, sentinel
17+
from unittest.mock import Mock, ANY, patch, sentinel, call
1418
except ImportError:
1519
# python 2
1620
from mock import Mock, ANY, patch, sentinel
1721

1822

1923
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:
2125
Profiling(False).pytest_sessionstart(Mock())
22-
makedirs.assert_called_with('prof')
26+
makedirs.assert_called_with("prof")
2327

2428

2529
def 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

3539
def 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

4565
def 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

5676
def 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

6793
def 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

76102
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:
79105
pytest_configure(config)
80106
config.pluginmanager.register.assert_called_with(Profiling.return_value)
81107

82108

83109
def test_clean_filename():
84-
assert pytest_profiling.clean_filename('a:b/c\256d') == 'a_b_c_d'
110+
assert pytest_profiling.clean_filename("a:b/c\256d") == "a_b_c_d"

pytest-server-fixtures/tests/unit/test_server_unit.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ def test_init():
2222
ts.workspace = ws
2323

2424

25-
def test_kill():
26-
self = create_autospec(_TestServer, dead=False,
27-
hostname=sentinel.hostname,
28-
port=sentinel.port)
29-
self.run.side_effect = ['100\n', '', '']
25+
def test_kill_by_port():
26+
server = _TestServer(hostname=sentinel.hostname, port=sentinel.port)
27+
server.run = Mock(side_effect=['100\n', '', ''])
28+
server._signal = Mock()
3029
with patch('socket.gethostbyname', return_value=sentinel.ip):
31-
_TestServer._find_and_kill_by_port(self, 2, sentinel.signal)
32-
assert self.run.call_args_list == [call("netstat -anp 2>/dev/null | grep sentinel.ip:sentinel.port "
30+
server._find_and_kill_by_port(2, sentinel.signal)
31+
server.dead = True
32+
assert server.run.call_args_list == [call("netstat -anp 2>/dev/null | grep sentinel.ip:sentinel.port "
3333
"| grep LISTEN | awk '{ print $7 }' | cut -d'/' -f1", capture=True, cd='/'),
3434
call("netstat -anp 2>/dev/null | grep sentinel.ip:sentinel.port "
3535
"| grep LISTEN | awk '{ print $7 }' | cut -d'/' -f1", capture=True, cd='/')]
36-
assert self._signal.call_args_list == [call(100, sentinel.signal)]
36+
assert server._signal.call_args_list == [call(100, sentinel.signal)]

0 commit comments

Comments
 (0)