Skip to content

Commit 5941e8e

Browse files
TWood67havok2063
andauthored
Change scope of live server to session (#113)
live_server fixture scope can be configured, defaults to session Fixes #62, Fixes #112, Closes #63 Co-authored-by: Brian Cherinka <[email protected]>
1 parent cafefd3 commit 5941e8e

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
lines changed

docs/features.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ in your project's ``pytest.ini`` file)::
186186

187187

188188
``request_ctx`` - request context
189+
`````````````````````````````````
190+
191+
``--live-server-scope`` - set the scope of the live server
192+
``````````````````````````````````````````````````````````````````
193+
194+
By default, the server will be scoped to `session`. In some cases, you may want
195+
it to be fixed to a different scope. You can use ``--live-server-scope`` (for example,
196+
in your project's ``pytest.ini`` file)::
197+
198+
[pytest]
199+
addopts = --live-server-scope=function
200+
201+
189202
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
190203

191204
The request context which contains all request relevant information.

pytest_flask/fixtures.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,12 @@ def _rewrite_server_name(server_name, new_port):
116116
return sep.join((server_name, new_port))
117117

118118

119-
@pytest.fixture(scope='function')
120-
def live_server(request, app, monkeypatch, pytestconfig):
119+
def determine_scope(fixture_name, config):
120+
return config.getoption('--live-server-scope', 'session')
121+
122+
123+
@pytest.fixture(scope=determine_scope)
124+
def live_server(request, app, pytestconfig):
121125
"""Run application in a separate process.
122126
123127
When the ``live_server`` fixture is applied, the ``url_for`` function
@@ -131,7 +135,10 @@ def test_server_is_up_and_running(live_server):
131135
assert res.code == 200
132136
133137
"""
134-
port = pytestconfig.getvalue('live_server_port')
138+
# Set or get a port
139+
port = app.config.get('LIVESERVER_PORT', None)
140+
if not port:
141+
port = pytestconfig.getvalue('live_server_port')
135142

136143
if port == 0:
137144
# Bind to an open port
@@ -143,18 +150,20 @@ def test_server_is_up_and_running(live_server):
143150
host = pytestconfig.getvalue('live_server_host')
144151

145152
# Explicitly set application ``SERVER_NAME`` for test suite
146-
# and restore original value on test teardown.
147-
server_name = app.config['SERVER_NAME'] or 'localhost'
148-
monkeypatch.setitem(app.config, 'SERVER_NAME',
149-
_rewrite_server_name(server_name, str(port)))
153+
original_server_name = app.config['SERVER_NAME'] or 'localhost'
154+
final_server_name = _rewrite_server_name(original_server_name, str(port))
155+
app.config['SERVER_NAME'] = final_server_name
150156

151157
clean_stop = request.config.getvalue('live_server_clean_stop')
152158
server = LiveServer(app, host, port, clean_stop)
153159
if request.config.getvalue('start_live_server'):
154160
server.start()
155161

156162
request.addfinalizer(server.stop)
157-
return server
163+
yield server
164+
165+
if original_server_name is not None:
166+
app.config['SERVER_NAME'] = original_server_name
158167

159168

160169
@pytest.fixture

pytest_flask/plugin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ def pytest_addoption(parser):
168168
help='use a host where to listen (default localhost).')
169169
group.addoption('--live-server-port', action='store', default=0, type=int,
170170
help='use a fixed port for the live_server fixture.')
171+
group.addoption('--live-server-scope', action='store', default='session', type=str,
172+
help='modify the scope of the live_server fixture.')
171173

172174

173175
def pytest_configure(config):

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
pytest_plugins = 'pytester'
1010

1111

12-
@pytest.fixture
12+
@pytest.fixture(scope='session')
1313
def app():
1414
app = Flask(__name__)
1515
app.config['SECRET_KEY'] = '42'
@@ -42,7 +42,7 @@ def create_test_module(code, filename='test_app.py'):
4242
4343
from flask import Flask
4444
45-
@pytest.fixture
45+
@pytest.fixture(scope='session')
4646
def app():
4747
app = Flask(__name__)
4848
return app

tests/test_live_server.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,18 @@ def test_set_application_server_name(self, live_server):
3333
assert live_server.app.config['SERVER_NAME'] == \
3434
'localhost:%d' % live_server.port
3535

36-
@pytest.mark.options(server_name='example.com:5000')
37-
def test_rewrite_application_server_name(self, live_server):
38-
assert live_server.app.config['SERVER_NAME'] == \
39-
'example.com:%d' % live_server.port
36+
def test_rewrite_application_server_name(self, appdir):
37+
appdir.create_test_module('''
38+
import pytest
39+
@pytest.mark.options(server_name='example.com:5000')
40+
def test_a(live_server):
41+
assert live_server.app.config['SERVER_NAME'] == \\
42+
'example.com:%d' % live_server.port
43+
''')
44+
45+
result = appdir.runpytest('-v', '--live-server-scope=function')
46+
result.stdout.fnmatch_lines(['*PASSED*'])
47+
assert result.ret == 0
4048

4149
def test_prevent_starting_live_server(self, appdir):
4250
appdir.create_test_module('''
@@ -47,7 +55,7 @@ def test_a(live_server):
4755
''')
4856

4957
result = appdir.runpytest('-v', '--no-start-live-server')
50-
result.stdout.fnmatch_lines(['*PASSED*'])
58+
result.stdout.fnmatch_lines(['*passed*'])
5159
assert result.ret == 0
5260

5361
def test_start_live_server(self, appdir):
@@ -59,7 +67,7 @@ def test_a(live_server):
5967
assert live_server._process.is_alive()
6068
''')
6169
result = appdir.runpytest('-v', '--start-live-server')
62-
result.stdout.fnmatch_lines(['*PASSED*'])
70+
result.stdout.fnmatch_lines(['*passed*'])
6371
assert result.ret == 0
6472

6573
@pytest.mark.parametrize('clean_stop', [True, False])
@@ -127,9 +135,10 @@ def new_endpoint():
127135
assert b'got it' in res.read()
128136
''')
129137
result = appdir.runpytest('-v', '--no-start-live-server')
130-
result.stdout.fnmatch_lines(['*PASSED*'])
138+
result.stdout.fnmatch_lines(['*passed*'])
131139
assert result.ret == 0
132140

141+
@pytest.mark.skip('this test hangs in the original code')
133142
def test_concurrent_requests_to_live_server(self, appdir):
134143
appdir.create_test_module('''
135144
import pytest
@@ -154,7 +163,7 @@ def two():
154163
assert b'42' in res.read()
155164
''')
156165
result = appdir.runpytest('-v', '--no-start-live-server')
157-
result.stdout.fnmatch_lines(['*PASSED*'])
166+
result.stdout.fnmatch_lines(['*passed*'])
158167
assert result.ret == 0
159168

160169
@pytest.mark.parametrize('port', [5000, 5001])

0 commit comments

Comments
 (0)