当我运行多个测试时,Django LiveServerTestCase无法加载页面 [英] Django LiveServerTestCase fails to load a page when I run multiple tests

查看:75
本文介绍了当我运行多个测试时,Django LiveServerTestCase无法加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to run multiple tests within one Django LiveServerTestCase. When I run any single test (with others commented) everything works as expected. But when I run test case with two tests the first one works fine but the second one loads page with "internal server error" message.

代码:

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(),它将失败,并显示错误98:地址已在使用中",类似于

If I use close() instead of quit() it fails with "error 98: address already in use" similar to this case except I have an error only when I have multiple tests in one LiveServerTestCase class or multiple test cases in one .py file.

如何在tearDown上使LiveServerTestCase空闲端口(如果这是核心问题)?

How do I make LiveServerTestCase free port on tearDown (if it is the core problem)?

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

Is there any workaround? All I want are functional selenium tests running equally on local and remote servers.

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

I am using Django 1.6.7, Firefox 37.0, Selenium 2.45.0

更新

使用方法而不是类方法会导致相同的问题.

Using methods instead of classmethods leads to the same issue.

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

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

推荐答案

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

Finally, the reason for the "internal server error" message is that WebDriver deletes all data from database on quit(), including contenttypes and other default tables.

这会导致在下一次测试开始时尝试加载灯具时发生错误.

This leads to an error when trying to load fixtures at the begin of the next test.

NB 这种行为实际上是由于TransactionTestCase(LiveServerTestCase继承自)在测试运行后重置数据库的方式引起的:

N.B. This behaviour is actually due to the way TransactionTestCase (from which LiveServerTestCase inherits) resets the database after the test runs: it truncates all tables.

到目前为止,我的解决方案是在每次测试运行时都将所有数据(也包括默认" Django数据,例如contenttypes)加载到夹具中.

So far, my solution is to load fixtures with all the data (also "default" Django data, e.g. contenttypes) on every test run.

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()问题的刷新数据库!

Thanks to @help_asap for pointing out this flushing database on quit() problem!

这篇关于当我运行多个测试时,Django LiveServerTestCase无法加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆