【问题标题】:Run LiveServerTestCase from Docker Selenium with Django 1.11使用 Django 1.11 从 Docker Selenium 运行 LiveServerTestCase
【发布时间】:2017-05-29 10:22:45
【问题描述】:

Since Django 1.11--liveserver 选项已从 manage.py test 命令中删除。

我使用此选项允许使用以下命令从机器的 IP 地址而不是 localhost 访问 liveserver:

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

不幸的是,此选项已消失,我正在寻找一种新的解决方案,以允许我的 Docker Selenium 映像在测试期间访问我的 LiveServerTestCase。

【问题讨论】:

  • 在某些情况下,您可以拥有一台装有 Selenium 的专用机器,并且您想用这台专用机器运行 django 测试 selenium。所以需要打开localhost以外的liveserver。

标签: python django selenium docker django-1.11


【解决方案1】:

似乎没有必要使用StaticLiveServerTestCase:同样的方法适用于其父类:

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

【讨论】:

  • LiveServerTestCase 有效,但不提供静态文件。
  • 好点,谢谢。我不会注意到,因为我只使用 Django 来提供数据 API。
【解决方案2】:

我通过覆盖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 在我的本地主机之外访问..

【讨论】:

  • 在 docker 中使用 cls.host = socket.gethostbyname(socket.gethostname())cls.host = 'web' 有什么区别。 @VivienCormier
【解决方案3】:

this thread 和 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

【讨论】:

    猜你喜欢
    • 2015-12-26
    • 1970-01-01
    • 2013-01-19
    • 2015-12-01
    • 2017-10-16
    • 2019-12-02
    • 2014-07-13
    • 2015-01-05
    • 2016-01-16
    相关资源
    最近更新 更多