Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting liveserver port when running tests in django

I am using django for a webapp and I'm using docker to deploy it. I need to test it in the container with selenium. I'm using selenium grid for testing. In order to connect with the liveserver on the docker, i need to port forward a specific port, but as far as i read in the django docs, LiveServerTestCase uses port 0, which means random port every time i run the tests. Since --liveserver options is deprecated, is there any other way to set the port of the test server or a smarter way to test it with selenium ?

Thanks in advance !

like image 546
Nikolai Nikolov Avatar asked Dec 19 '17 12:12

Nikolai Nikolov


2 Answers

If anyone is wondering this is how I did it: Override method setUpClass, which starts the thread on which the server run

@classmethod
def setUpClass(cls):
    cls.host = "example.com" # or ip
    cls.port = 12345
    super(ExampleTestCase, cls).setUpClass()
like image 190
Nikolai Nikolov Avatar answered Sep 17 '22 16:09

Nikolai Nikolov


All the previous answers work well, but the most succinct way to set the port is as follows:

class MyTestCase(LiveServerTestCase):
    port = 12345
like image 43
Eerik Sven Puudist Avatar answered Sep 16 '22 16:09

Eerik Sven Puudist