TimClifford TimClifford - 1 month ago 13
Python Question

Django's LiveServerTestCase Always Fails Due to Conflicting Address... Despite Address Appearing Free

I'm currently working on cleaning my Django functional tests to use the LiveServerTestCase rather than bouncing selenium based tests off an instance of the development environment running in the background, and I've hit a wall. Every time I try to run a LiveServerTestCase test, I get the following error:

======================================================================
ERROR: setUpClass (fun_tests.tests.backend.TestCmsLogin)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/user/Documents/env/local/lib/python2.7/site-packages/django/test/testcases.py", line 1187, in setUpClass
raise cls.server_thread.error
error: [Errno 98] Address already in use


Super fun, considering
sudo netstat -netp | grep 8081
yields nothing. Some background: I'm using Django 1.6, and I was using nose, django-nose, nose-exclude, but I've effectively chopped them out to help diagnose the issue. The code I'm using is abysmally simple:

from django.test import LiveServerTestCase
class TestCmsLogin(LiveServerTestCase):
def test_a_test_itself(self):
self.assertTrue(True)


I can't find any prior art on the subject, and Djangoproject's bug tracker is clean. What am I missing?

Edit: This morning the issue is irreproducible, whatever was flagging port 8081 as open is no longer causing issues.

edit2: Mistyped 8081 as 8082 in my writeup, fixed (and checked to make sure I had it right at the time).

Answer

You can set (in settings.py) the environment variable DJANGO_LIVE_TEST_SERVER_ADDRESS to include multiple ranges of ports that will be attempted:

os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS']="localhost:8000-8010,8080,9200-9300"

Had the same issue myself, maybe this helps someone out.

Comments