diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index 692e0614a..917ac4e7d 100644 --- a/pytest_django/fixtures.py +++ b/pytest_django/fixtures.py @@ -302,10 +302,14 @@ def live_server(request): addr = (request.config.getvalue('liveserver') or os.getenv('DJANGO_LIVE_TEST_SERVER_ADDRESS')) - if addr and django.VERSION >= (1, 11) and ':' in addr: - request.config.warn('D001', 'Specifying a live server port is not supported ' - 'in Django 1.11. This will be an error in a future ' - 'pytest-django release.') + if addr and ':' in addr: + if django.VERSION >= (1, 11): + ports = addr.split(':')[1] + if '-' in ports or ',' in ports: + request.config.warn('D001', + 'Specifying multiple live server ports is not supported ' + 'in Django 1.11. This will be an error in a future ' + 'pytest-django release.') if not addr: if django.VERSION < (1, 11): diff --git a/pytest_django/live_server_helper.py b/pytest_django/live_server_helper.py index a220639b5..5d2464ed0 100644 --- a/pytest_django/live_server_helper.py +++ b/pytest_django/live_server_helper.py @@ -38,7 +38,12 @@ def __init__(self, addr): self.thread = LiveServerThread(host, possible_ports, **liveserver_kwargs) else: - host = addr + try: + host, port = addr.split(':') + except ValueError: + host = addr + else: + liveserver_kwargs['port'] = int(port) self.thread = LiveServerThread(host, **liveserver_kwargs) self._live_server_modified_settings = modify_settings( diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index d03a7d6fb..10ceac35c 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -6,6 +6,8 @@ from __future__ import with_statement +import socket + import pytest from django.db import connection, transaction @@ -321,18 +323,35 @@ def test_serve_static_dj17_without_staticfiles_app(self, live_server, @pytest.mark.skipif(get_django_version() < (1, 11), reason='Django >= 1.11 required') - def test_specified_port_error_message_django_111(self, django_testdir): + def test_specified_port_range_error_message_django_111(self, django_testdir): django_testdir.create_test_module(""" def test_with_live_server(live_server): pass """) - result = django_testdir.runpytest_subprocess('--liveserver=localhost:1234') + result = django_testdir.runpytest_subprocess('--liveserver=localhost:1234-2345') result.stdout.fnmatch_lines([ - '*Specifying a live server port is not supported in Django 1.11. This ' + '*Specifying multiple live server ports is not supported in Django 1.11. This ' 'will be an error in a future pytest-django release.*' ]) + @pytest.mark.skipif(get_django_version() < (1, 11, 2), + reason='Django >= 1.11.2 required') + def test_specified_port_django_111(self, django_testdir): + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + try: + sock.bind(('', 0)) + __, port = sock.getsockname() + finally: + sock.close() + + django_testdir.create_test_module(""" + def test_with_live_server(live_server): + assert live_server.port == %d + """ % port) + + django_testdir.runpytest_subprocess('--liveserver=localhost:%s' % port) + @pytest.mark.django_project(extra_settings=""" AUTH_USER_MODEL = 'app.MyCustomUser'