当我运行测试时 Django LiveServerTestCase 挂起

标签 django django-testing django-tests

我正在尝试使用 Django 1.10.4 设置 LiveServerTestCase。每当我运行测试时,浏览器都会打开挂起并且无法访问本地主机。我的前端是一个单独的角度/ react 应用程序。因此,我使用 grunt build 构建静态 Assets ,然后运行collectstatic。下面是我的测试代码。

from django.test.testcases import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By



class ChromeTestCase(LiveServerTestCase):

    @classmethod
    def setUpClass(cls):
        super(ChromeTestCase, cls).setUpClass()
        cls.driver = webdriver.Chrome('/path/to/chromedriver')
        cls.driver.implicitly_wait(10)
        cls.wait = WebDriverWait(cls.driver, 10)

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()
        super(ChromeTestCase, cls).tearDownClass()

    def test_user_sign_up_from_form(self):
        self.driver.get('%s%s' % (self.live_server_url, '/'))

        self.wait.until(EC.presence_of_element_located((By.XPATH, '//input[@id="email"]')))
        email_input = self.driver.find_element_by_xpath(
            '//input[@id="email"]')
        email_input.send_keys("test@gmail.com")
        password_input = self.driver.find_element_by_xpath(
            '//input[@id="password"]')
        password_input.send_keys("secret")

        signup_button = self.driver.find_elements_by_xpath(
            '//*[@id="signup_button"]')
        signup_button.click()

        url = self.live_server_url + '/home'
        self.assertEquals(self.driver.current_url, url)

有谁知道为什么我的测试无法到达测试服务器?

此外,我的测试服务器创建的 URL 是 https。

最佳答案

该问题最终与在生产环境中将请求重定向到 https 的中间件有关。我通过删除该中间件使测试正常工作。

关于当我运行测试时 Django LiveServerTestCase 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44010613/

相关文章:

Django - 使用大型静态数据表进行测试

python - 如何在 kwargs 中使用 UUID 字段?

python - django传递响应上下文无

django - 使用临时数据库启动django shell

使用假用户的 Django 测试模型

python - 有什么方法可以重命名 django.auth 表吗?

python - 是否有 Django 函数可以对 None 对象进行对象计数?

python - Django 测试完成后如何保留测试数据?

python - 安装 psycopg2 失败 python 3.7

django - 如何在 Django 中添加新语言? Django 不支持我的语言 "Uyghur"或 "Uighur"