Skip to content

Commit 17cadc1

Browse files
committed
Add --live-server-port option
1 parent d2f8ae1 commit 17cadc1

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

docs/features.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,17 @@ example, in your project’s ``pytest.ini`` file)::
174174
assert b'got it' in res.read()
175175
176176
177+
``--live-server-port`` - use a fixed port
178+
`````````````````````````````````````````
179+
180+
By default the server uses a random port. In some cases it is desirable to run
181+
the server with a fixed port. You can use ``--live-server-port`` (for example,
182+
in your project's ``pytest.ini`` file)::
183+
184+
[pytest]
185+
addopts = --live-server-port=5000
186+
187+
177188
``request_ctx`` - request context
178189
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
179190

pytest_flask/fixtures.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def _rewrite_server_name(server_name, new_port):
9898

9999

100100
@pytest.fixture(scope='function')
101-
def live_server(request, app, monkeypatch):
101+
def live_server(request, app, monkeypatch, pytestconfig):
102102
"""Run application in a separate process.
103103
104104
When the ``live_server`` fixture is applied, the ``url_for`` function
@@ -112,11 +112,14 @@ def test_server_is_up_and_running(live_server):
112112
assert res.code == 200
113113
114114
"""
115-
# Bind to an open port
116-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
117-
s.bind(('', 0))
118-
port = s.getsockname()[1]
119-
s.close()
115+
port = pytestconfig.getvalue('live_server_port')
116+
117+
if port == 0:
118+
# Bind to an open port
119+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
120+
s.bind(('', 0))
121+
port = s.getsockname()[1]
122+
s.close()
120123

121124
# Explicitly set application ``SERVER_NAME`` for test suite
122125
# and restore original value on test teardown.

pytest_flask/plugin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ def pytest_addoption(parser):
124124
action="store_false", dest="start_live_server",
125125
help="don't start server automatically when live_server "
126126
"fixture is applied.")
127+
group.addoption('--live-server-port', action='store', default=0, type=int,
128+
help='use a fixed port for the live_server fixture.')
127129

128130

129131
def pytest_configure(config):

tests/test_live_server.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,15 @@ def two():
116116
result = appdir.runpytest('-v', '--no-start-live-server')
117117
result.stdout.fnmatch_lines(['*PASSED*'])
118118
assert result.ret == 0
119+
120+
@pytest.mark.parametrize('port', [5000, 5001])
121+
def test_live_server_fixed_port(self, port, appdir):
122+
appdir.create_test_module('''
123+
import pytest
124+
125+
def test_port(live_server):
126+
assert live_server.port == %d
127+
''' % port)
128+
result = appdir.runpytest('-v', '--live-server-port', port)
129+
result.stdout.fnmatch_lines(['*PASSED*'])
130+
assert result.ret == 0

0 commit comments

Comments
 (0)