Skip to content

Commit ad98425

Browse files
committed
Improve clean stop test to use mock instead of codecov
1 parent 6e46f15 commit ad98425

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

pytest_flask/fixtures.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,26 @@ def url(self, url=''):
8787
def stop(self):
8888
"""Stop application process."""
8989
if self._process:
90-
if self.clean_stop:
91-
# We wait a maximum of 5 seconds for the server to terminate cleanly
92-
timeout = 5
93-
try:
94-
os.kill(self._process.pid, signal.SIGINT)
95-
self._process.join(timeout)
96-
except Exception as ex:
97-
logging.error('Failed to join the live server process: %r', ex)
98-
finally:
99-
if self._process.is_alive():
100-
# If it's still alive, kill it
101-
self._process.terminate()
102-
else:
90+
if self.clean_stop and self._stop_cleanly():
91+
return
92+
if self._process.is_alive():
93+
# If it's still alive, kill it
10394
self._process.terminate()
10495

96+
def _stop_cleanly(self, timeout=5):
97+
"""Attempts to stop the server cleanly by sending a SIGINT signal and waiting for
98+
``timeout`` seconds.
99+
100+
:return: True if the server was cleanly stopped, False otherwise.
101+
"""
102+
try:
103+
os.kill(self._process.pid, signal.SIGINT)
104+
self._process.join(timeout)
105+
return True
106+
except Exception as ex:
107+
logging.error('Failed to join the live server process: %r', ex)
108+
return False
109+
105110
def __repr__(self):
106111
return '<LiveServer listening at %s>' % self.url()
107112

tests/test_live_server.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,25 @@ def test_a(live_server):
6767
result.stdout.fnmatch_lines(['*PASSED*'])
6868
assert result.ret == 0
6969

70-
def test_clean_stop_live_server(self, appdir):
71-
pytest_cov = pytest.importorskip("pytest_cov")
70+
def test_clean_stop_live_server(self, appdir, monkeypatch):
71+
"""Ensure the fixture is trying to cleanly stop the server.
72+
73+
Because this is tricky to test, we are checking that the _stop_cleanly() internal
74+
function was called and reported success.
75+
"""
76+
from pytest_flask.fixtures import LiveServer
77+
78+
original_stop_cleanly_func = LiveServer._stop_cleanly
79+
80+
stop_cleanly_result = []
81+
82+
def mocked_stop_cleanly(*args, **kwargs):
83+
result = original_stop_cleanly_func(*args, **kwargs)
84+
stop_cleanly_result.append(result)
85+
return result
86+
87+
monkeypatch.setattr(LiveServer, '_stop_cleanly', mocked_stop_cleanly)
88+
7289
appdir.create_test_module('''
7390
import pytest
7491
try:
@@ -89,30 +106,10 @@ def index():
89106
assert res.code == 200
90107
assert b'got it' in res.read()
91108
''')
92-
result_with = appdir.runpytest('-v',
93-
'--no-start-live-server',
94-
'--live-server-clean-stop',
95-
'--cov=%s' % str(appdir.tmpdir),
96-
'--cov-report=term-missing')
97-
result_without = appdir.runpytest('-v',
98-
'--no-start-live-server',
99-
'--no-live-server-clean-stop',
100-
'--cov=%s' % str(appdir.tmpdir),
101-
'--cov-report=term-missing')
102-
103-
def _get_missing(r):
104-
for line in r.outlines:
105-
if not line.startswith('TOTAL'):
106-
continue
107-
# Columns: Name Stmts Miss Cover Missing
108-
return int(line.split()[2])
109-
raise ValueError("Expected a TOTAL line in the cov output")
110-
111-
# Read the "Missing" column (i.e. lines not covered)
112-
missing_with, missing_without = _get_missing(result_with), _get_missing(result_without)
113-
114-
# If the clean stop worked, the single line in the view function should be covered
115-
assert missing_with == (missing_without - 1)
109+
result = appdir.runpytest_inprocess('-v', '--no-start-live-server',
110+
'--live-server-clean-stop')
111+
result.stdout.fnmatch_lines('*1 passed*')
112+
assert stop_cleanly_result == [True]
116113

117114
def test_add_endpoint_to_live_server(self, appdir):
118115
appdir.create_test_module('''

0 commit comments

Comments
 (0)