Skip to content

Commit d8c6d13

Browse files
dtomastimb07
authored andcommitted
Support for setting the live server port (pytest-dev#500)
* Allow setting the live server port for Django >= 1.11.2. * Removed warning when specifying live server port with Django >= 1.11 < 1.11.2. * Added test for specifying a live server port with Django >= 1.11.2.
1 parent c9a0936 commit d8c6d13

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

pytest_django/fixtures.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,14 @@ def live_server(request):
302302
addr = (request.config.getvalue('liveserver') or
303303
os.getenv('DJANGO_LIVE_TEST_SERVER_ADDRESS'))
304304

305-
if addr and django.VERSION >= (1, 11) and ':' in addr:
306-
request.config.warn('D001', 'Specifying a live server port is not supported '
307-
'in Django 1.11. This will be an error in a future '
308-
'pytest-django release.')
305+
if addr and ':' in addr:
306+
if django.VERSION >= (1, 11):
307+
ports = addr.split(':')[1]
308+
if '-' in ports or ',' in ports:
309+
request.config.warn('D001',
310+
'Specifying multiple live server ports is not supported '
311+
'in Django 1.11. This will be an error in a future '
312+
'pytest-django release.')
309313

310314
if not addr:
311315
if django.VERSION < (1, 11):

pytest_django/live_server_helper.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ def __init__(self, addr):
3838
self.thread = LiveServerThread(host, possible_ports,
3939
**liveserver_kwargs)
4040
else:
41-
host = addr
41+
try:
42+
host, port = addr.split(':')
43+
except ValueError:
44+
host = addr
45+
else:
46+
liveserver_kwargs['port'] = int(port)
4247
self.thread = LiveServerThread(host, **liveserver_kwargs)
4348

4449
self._live_server_modified_settings = modify_settings(

tests/test_fixtures.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from __future__ import with_statement
88

9+
import socket
10+
911
import pytest
1012

1113
from django.db import connection, transaction
@@ -321,18 +323,35 @@ def test_serve_static_dj17_without_staticfiles_app(self, live_server,
321323

322324
@pytest.mark.skipif(get_django_version() < (1, 11),
323325
reason='Django >= 1.11 required')
324-
def test_specified_port_error_message_django_111(self, django_testdir):
326+
def test_specified_port_range_error_message_django_111(self, django_testdir):
325327
django_testdir.create_test_module("""
326328
def test_with_live_server(live_server):
327329
pass
328330
""")
329331

330-
result = django_testdir.runpytest_subprocess('--liveserver=localhost:1234')
332+
result = django_testdir.runpytest_subprocess('--liveserver=localhost:1234-2345')
331333
result.stdout.fnmatch_lines([
332-
'*Specifying a live server port is not supported in Django 1.11. This '
334+
'*Specifying multiple live server ports is not supported in Django 1.11. This '
333335
'will be an error in a future pytest-django release.*'
334336
])
335337

338+
@pytest.mark.skipif(get_django_version() < (1, 11, 2),
339+
reason='Django >= 1.11.2 required')
340+
def test_specified_port_django_111(self, django_testdir):
341+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
342+
try:
343+
sock.bind(('', 0))
344+
__, port = sock.getsockname()
345+
finally:
346+
sock.close()
347+
348+
django_testdir.create_test_module("""
349+
def test_with_live_server(live_server):
350+
assert live_server.port == %d
351+
""" % port)
352+
353+
django_testdir.runpytest_subprocess('--liveserver=localhost:%s' % port)
354+
336355

337356
@pytest.mark.django_project(extra_settings="""
338357
AUTH_USER_MODEL = 'app.MyCustomUser'

0 commit comments

Comments
 (0)