在 Django 中 运行 测试时设置 liveserver 端口
Setting liveserver port when running tests in django
我正在将 django 用于 webapp,并且我正在使用 docker 来部署它。我需要在容器中用硒测试它。我正在使用硒网格进行测试。为了与 docker 上的 liveserver 连接,我需要转发一个特定的端口,但据我在 django 文档中阅读,LiveServerTestCase 使用端口 0,这意味着每次我 [=17] 的随机端口=] 测试。由于不推荐使用 --liveserver 选项,是否有任何其他方法来设置测试服务器的端口或使用 selenium 测试它的更智能的方法?
提前致谢!
如果有人想知道我是怎么做到的:
覆盖方法 setUpClass,它启动服务器 运行
所在的线程
@classmethod
def setUpClass(cls):
cls.host = "example.com" # or ip
cls.port = 12345
super(ExampleTestCase, cls).setUpClass()
根据 Django 1.11 release notes,您应该在 LiveServerTestCase
上设置 port
属性:
If you need to bind LiveServerTestCase
to a specific port, use the port
attribute added in Django 1.11.2.
前面的答案都很好,但是最简洁的端口设置方法如下:
class MyTestCase(LiveServerTestCase):
port = 12345
我正在将 django 用于 webapp,并且我正在使用 docker 来部署它。我需要在容器中用硒测试它。我正在使用硒网格进行测试。为了与 docker 上的 liveserver 连接,我需要转发一个特定的端口,但据我在 django 文档中阅读,LiveServerTestCase 使用端口 0,这意味着每次我 [=17] 的随机端口=] 测试。由于不推荐使用 --liveserver 选项,是否有任何其他方法来设置测试服务器的端口或使用 selenium 测试它的更智能的方法?
提前致谢!
如果有人想知道我是怎么做到的: 覆盖方法 setUpClass,它启动服务器 运行
所在的线程@classmethod
def setUpClass(cls):
cls.host = "example.com" # or ip
cls.port = 12345
super(ExampleTestCase, cls).setUpClass()
根据 Django 1.11 release notes,您应该在 LiveServerTestCase
上设置 port
属性:
If you need to bind
LiveServerTestCase
to a specific port, use theport
attribute added in Django 1.11.2.
前面的答案都很好,但是最简洁的端口设置方法如下:
class MyTestCase(LiveServerTestCase):
port = 12345