2017-05-29 87 views
2

Since Django 1.11,从manage.py test命令中删除选项--liveserver使用Django从Docker Selenium运行LiveServerTestCase 1.11

我用这个选项允许liveserver是从机器的IP地址而不是localhost使用以下命令到达:

./manage.py test --liveserver=0.0.0.0:8000 

不幸的是,这个选项消失了,我期待一个新的解决方案允许我的Docker Selenium映像在测试期间访问我的LiveServerTestCase。

+0

在某些情况下,你可以有一个专用的机器使用Selenium和要与运行你的Django测试硒这台专用机器。所以你需要打开localhost以外的liveserver。 – VivienCormier

回答

3

我发现解决方案通过覆盖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是考虑到LiverServerTestCasesetUpClass因为default valuelocalhost

所以现在我liveserver可达我的本地主机之外,通过使用IP ..

0

this thread和VivienCormier有点帮助,这是为我工作使用Django 1.11和码头工人,组成

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 
0

使用StaticLiveServerTestCase似乎没有必要:同样的方法与其父类的工作:

class End2End(LiveServerTestCase): 
    host = '0.0.0.0' # or socket.gethostbyname(...) like @VivienCormier did 
+0

'LiveServerTestCase'工作,但不提供静态文件。 –

+0

好点,谢谢。我不会注意到,因为我只使用Django来提供数据API。 –