从 Django 1.11开始,该选项--liveserver
已从manage.py test
命令中删除。
我使用此选项允许从机器的 IP 地址访问 liveserver,而不是localhost
使用以下命令:
./manage.py test --liveserver=0.0.0.0:8000
不幸的是,这个选项已经消失,我正在寻找一种新的解决方案,以允许我的 Docker Selenium 映像在测试期间访问我的 LiveServerTestCase。
从 Django 1.11开始,该选项--liveserver
已从manage.py test
命令中删除。
我使用此选项允许从机器的 IP 地址访问 liveserver,而不是localhost
使用以下命令:
./manage.py test --liveserver=0.0.0.0:8000
不幸的是,这个选项已经消失,我正在寻找一种新的解决方案,以允许我的 Docker Selenium 映像在测试期间访问我的 LiveServerTestCase。
我通过覆盖StaticLiveServerTestCase
和更改host
属性找到了解决方案。
例子:
import socket
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
class SeleniumTestCase(StaticLiveServerTestCase):
@classmethod
def setUpClass(cls):
cls.host = socket.gethostbyname(socket.gethostname())
super(SeleniumTestCase, cls).setUpClass()
使用此解决方案,我的机器的 IP 被赋予了setUpClass,LiverServerTestCase
因为默认值为localhost
.
所以现在我的 liveserver 可以在我的本地主机之外访问,通过使用 IP ..
在这个线程和 VivienCormier 的帮助下,这就是使用 Django 1.11 和 docker-compose 对我有用的东西
version: '2'
services:
db:
restart: "no"
image: postgres:9.6
ports:
- "5432:5432"
volumes:
- ./postgres-data:/var/lib/postgresql/data
env_file:
- .db_env
web:
build: ./myproject
command: python manage.py runserver 0.0.0.0:8000
ports:
- "8000:8000"
volumes:
- ./myproject:/usr/src/app
depends_on:
- db
- selenium
env_file: .web_env
selenium:
image: selenium/standalone-firefox
expose:
- "4444"
import os
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
class TestHomePageView(StaticLiveServerTestCase):
@classmethod
def setUpClass(cls):
cls.host = 'web'
cls.selenium = webdriver.Remote(
command_executor=os.environ['SELENIUM_HOST'],
desired_capabilities=DesiredCapabilities.FIREFOX,
)
super(TestHomePageView, cls).setUpClass()
@classmethod
def tearDownClass(cls):
cls.selenium.quit()
super(TestHomePageView, cls).tearDownClass()
def test_root_url_resolves_to_home_page_view(self):
response = self.client.get('/')
self.assertEqual(response.resolver_match.func.__name__, LoginView.as_view().__name__)
def test_page_title(self):
self.selenium.get('%s' % self.live_server_url)
page_title = self.selenium.find_element_by_tag_name('title').text
self.assertEqual('MyProject', page_title)
.web_env 文件
SELENIUM_HOST=http://selenium:4444/wd/hub
StaticLiveServerTestCase
似乎没有必要使用 :相同的方法适用于其父类:
class End2End(LiveServerTestCase):
host = '0.0.0.0' # or socket.gethostbyname(...) like @VivienCormier did