Setting liveserver port when running tests in django
Asked Answered
B

3

9

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 !

Bocock answered 19/12, 2017 at 12:33 Comment(0)
B
10

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()
Bocock answered 19/12, 2017 at 14:28 Comment(2)
You method works (thanks!), but your code is misleading: You're writing a class method, so you get the class (and not an instance of that class) which is called cls by convention.Adela
Not bad idea to say that after the first comment, the author has edited the answer using the comment proposition.Liverwort
S
6

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

class MyTestCase(LiveServerTestCase):
    port = 12345
Segal answered 17/11, 2018 at 18:40 Comment(0)
P
3

According to the Django 1.11 release notes you should set the port attribute on the LiveServerTestCase:

If you need to bind LiveServerTestCase to a specific port, use the port attribute added in Django 1.11.2.

Phan answered 29/1, 2018 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.