Skip to content

Commit 61cb484

Browse files
authored
Merge pull request #82 from RazerM/master
Add --live-server-port option
2 parents 6c27516 + b923c74 commit 61cb484

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

docs/changelog.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
Changelog
44
=========
55

6+
7+
Next Release
8+
------------
9+
10+
- Add new ``--live-server-port`` option to select the port the live server will use (`#82`_).
11+
Thanks `@RazerM`_ for the PR.
12+
13+
.. _@RazerM: https://github.com/RazerM
14+
.. _#82: https://github.com/pytest-dev/pytest-flask/pull/82
15+
16+
617
0.11.0 (compared to 0.10.0)
718
---------------------------
819

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
@@ -121,3 +121,15 @@ def two():
121121
result = appdir.runpytest('-v', '--no-start-live-server')
122122
result.stdout.fnmatch_lines(['*PASSED*'])
123123
assert result.ret == 0
124+
125+
@pytest.mark.parametrize('port', [5000, 5001])
126+
def test_live_server_fixed_port(self, port, appdir):
127+
appdir.create_test_module('''
128+
import pytest
129+
130+
def test_port(live_server):
131+
assert live_server.port == %d
132+
''' % port)
133+
result = appdir.runpytest('-v', '--live-server-port', str(port))
134+
result.stdout.fnmatch_lines(['*PASSED*'])
135+
assert result.ret == 0

0 commit comments

Comments
 (0)