【问题标题】:Django LiveServerTestCase fails to load a page when I run multiple tests运行多个测试时,Django LiveServerTestCase 无法加载页面
【发布时间】:2025-05-25 09:15:02
【问题描述】:

我正在尝试在一个 Django LiveServerTestCase 中运行多个测试。当我运行任何单个测试(其他人评论)时,一切都按预期工作。但是当我用两个测试运行测试用例时,第一个工作正常,但第二个加载页面时显示“内部服务器错误”消息。

代码:

from django.test import LiveServerTestCase
from selenium.webdriver.firefox.webdriver import WebDriver


class MyLiveServerTestCase(LiveServerTestCase):    
    """
    BaseCleass for my selenium test cases
    """
    @classmethod
    def setUpClass(cls):
        cls.driver = WebDriver()
        cls.url = cls.live_server_url    

        super(MyLiveServerTestCase, cls).setUpClass()

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

class AdminEditFormTest(MyLiveServerTestCase):
    """
    Some test case
    """

    def test_valid_data(self):
        """
        test when user enters correct data
        """
        self.driver.get(self.url)
        # ...

    def test_invalid_data(self):
        """ test when user enters INcorrect data """
        self.driver.get(self.url)
        # ...

如果我使用close() 而不是quit() 它会失败,并出现类似于this 情况的“错误98:地址已在使用”,除非我只有在一个LiveServerTestCase 类或多个测试中有多个测试时才会出错一个 .py 文件中的案例。

如何让 LiveServerTestCase 在 tearDown 时释放端口(如果是核心问题)?

有什么解决方法吗?我想要的只是在本地和远程服务器上同等运行的功能性硒测试。

我正在使用 Django 1.6.7、Firefox 37.0、Selenium 2.45.0

更新

使用方法而不是类方法会导致同样的问题。

def setUp(self):
    self.driver = WebDriver()
    self.url = self.live_server_url    

def tearDown(self):
    self.driver.quit()

【问题讨论】:

  • 第二个测试自己运行正常吗?
  • 是的,它们独立运行良好。
  • 我遇到了同样的问题,我正在尝试找到一个有效且优雅的解决方案(这比将每个测试单独放在自己的 LiveServerTestCase 类中更好)。只是为了处理更多数据,您使用的是哪个版本的 Django?
  • 单独的课程也无济于事。我正在使用 django 1.6.7
  • 我试图通过将您的代码复制并粘贴到我的一个工作 Django 项目中来复制您的情况,但我完全没有错误。 不过,我需要更新 Firefox 和 Selenium。用Firefox==37.0Selenium==2.45.0 测试你的代码不会给我带来错误。您是否已经尝试更新它们? 附注:如果有机会,请尝试使用PhantomJS 代替 Firefox 进行功能测试:它真的更快!

标签: python django selenium selenium-webdriver


【解决方案1】:

最后,“内部服务器错误”消息的原因是 WebDriver 删除了 quit() 上数据库中的所有数据,包括内容类型和其他默认表

这会导致在下一次测试开始时尝试加载夹具时出错。

注意这种行为实际上是由于TransactionTestCaseLiveServerTestCase 继承自)在测试运行后重置数据库的方式:it truncates all tables


到目前为止,我的解决方案是在每次测试运行时加载所有数据(也是“默认”Django 数据,例如内容类型)的夹具。

class MyLiveServerTestCase(LiveServerTestCase):    
    """
    BaseClass for my Selenium test cases
    """
    fixtures = ['my_fixture_with_all_default_stuff_and_testing_data.json']

    @classmethod
    def setUpClass(cls):
        cls.driver = WebDriver()
        cls.url = cls.live_server_url    
        super(MyLiveServerTestCase, cls).setUpClass()

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

感谢@help_asap 在quit() 问题上指出这个刷新数据库!

【讨论】:

  • 非常感谢您澄清为什么我的第一个 LiveServerTestCase 测试运行良好,但我的第二个测试却出现了如此奇怪的错误。 未来的谷歌人请注意,如果您需要不这样做的LiveServerTestCase 版本,您可以使用class RealTransactionalLiveServerTestCase(LiveServerTestCase, TestCase): passTestCase 获取实际的交易功能。
  • @CoreDumpError 子类太棒了!
  • 可能感兴趣的相关 Django 票证:code.djangoproject.com/ticket/23640
最近更新 更多