Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 硒测试在单独测试之间非常缓慢_Django_Python 2.7_Selenium_Selenium Webdriver - Fatal编程技术网

Django 硒测试在单独测试之间非常缓慢

Django 硒测试在单独测试之间非常缓慢,django,python-2.7,selenium,selenium-webdriver,Django,Python 2.7,Selenium,Selenium Webdriver,我正在使用Django 1.4、Selenium 2.53.1和Chrome Webdriver 2.21作为我的测试Webdriver来测试我的Django应用程序 我初始化我的类,如下所示: class SeleniumTest(LiveServerTestCase): @classmethod def setUpClass(cls): cls.display = Display(visible=0, size=(800, 600)) cls.display.start()

我正在使用Django 1.4、Selenium 2.53.1和Chrome Webdriver 2.21作为我的测试Webdriver来测试我的Django应用程序

我初始化我的类,如下所示:

class SeleniumTest(LiveServerTestCase):

@classmethod
def setUpClass(cls):
    cls.display = Display(visible=0, size=(800, 600))
    cls.display.start()
    cls.driver = webdriver.Chrome()
    cls.driver.set_page_load_timeout(15)
    cls.driver.maximize_window()
    super(SeleniumTest, cls).setUpClass()

def setUp(self):

    settings.SESSION_ENGINE = 'django.contrib.sessions.backends.db'
    engine = import_module(settings.SESSION_ENGINE)
    self.sessionStore = engine.SessionStore()
    self.sessionStore.save()
    username = 'hello'
    password = 'hello'
    self.cad_user, created = User.objects.get_or_create(username=username, email='hello@hello.com')
    self.cad_user.set_password(password)
    self.cad_user.save()

    try:
        self.get_url('login')
        if self.driver.title == 'Login':
            self.driver.find_element_by_id('id_username').send_keys(username)
            self.driver.find_element_by_id('id_password').send_keys(password)
            self.driver.find_element_by_css_selector('input[type="submit"]').click()
下面是我的一个测试示例。它使用多个级别测试下拉列表,这些级别在您将鼠标移到下拉列表上并检查它们是否转到正确的链接后显示

def dropdown_check(self, header_ids, choice_id, title):
    choice = self.driver.find_element_by_id(choice_id)
    mouse = webdriver.ActionChains(self.driver)
    for header_id in header_ids:
        header_element = self.driver.find_element_by_id(header_id)
        WebDriverWait(self.driver, 1).until(EC.element_to_be_clickable((By.ID, header_id)))
        mouse.move_to_element(header_element)
        mouse.perform()
    WebDriverWait(self.driver, 1).until(EC.element_to_be_clickable((By.ID, choice_id)))
    choice.click()
    self.assertEquals(self.driver.title, title)

def test_my_status_navigation(self):
    self.dropdown_check(['menubar_my_status'], 'menubar_my_status', 'User Status')
我试过这些东西:

  • 我已经为每个测试对代码进行了计时,它们花费的时间不到一秒钟
  • 我还对setup和setupclass方法进行了计时,它们最多需要2秒钟
  • 我已将页面加载超时设置为0,并且不会改变整个执行时间
  • 我通过每轮增加一个额外的测试来运行测试,发现每个测试的总测试套件时间增加了大约40秒

  • 鉴于此,整个8个测试套件需要超过300秒,我不知道为什么。我确信Webdriver的加载需要一些时间,但是在每个单独的测试结束后,我可以看到Webdriver只是坐在那里,什么也不做。

    我把它缩小到与Django以及它如何与数据库交互有关的部分,因为我使用了
    setup
    setUpClass
    以及我的测试方法:

    def setUpClass(cls):
        pass
    

    而且总时间不会改变,将数据库更改为sqlite3会显著减少总测试时间,但不会出现一些错误。

    最大的问题是
    LiveServerTestCase
    是Django项目中所有测试用例中最慢的。它继承自

    TransactionTestCase从SimpleTestCase继承来添加一些 数据库特定功能:

    在每次测试开始时将数据库重置为已知状态 简化测试和使用ORM

    Django的TestCase类是 TransactionTestCase,利用数据库事务处理设施 加快将数据库重置为当前已知状态的过程 每次测试的开始

    因此,每次测试都会导致数据库完全重置,这非常缓慢。一种解决方案是使用-k或--keep选项

    ./manage.py test -k myapp
    
    这将至少缩短测试执行时间100秒

    还有一种解决方案不能应用于所有条件,因为TestCase不能用于Selenium测试。但是,您可以编写连接到dev服务器的独立selenium测试,以加快某些测试的速度。在本例中,使用python中的
    unittest.TestCase
    ,而不是
    django.test.TestCase.TestCase
    。这听起来完全令人困惑,所以让我举个例子

    from unittest import TestCase
    from selenium import webdriver
    
    class SeleniumTest(TestCase):
    
    
        def setUp(self):
            # usual code to setup drivers etc
    
        def testSomething(self):
            self.driver.get('localhost:8000/somepage')
    
            # now you are connecting directly to the development server.  
            # this approach is not suitable for all requirements but 
            # very fast compared to using a LiveServerTestCase
    

    最后但并非最不重要的一点:通常您根本不需要LiveServerTestCase或selenium测试。使用django更快更容易

    如果不知道细节,很难回答。您能否展示您的测试、如何初始化webdriver、selenium和django的版本、您使用的浏览器和版本?谢谢。@alecxe我已经用你提到的内容更新了这个问题!我希望这对汉克斯很有帮助。是否虚拟显示的启动占用了大部分时间?你能衡量一下什么对性能的负面影响最大吗?@alecxe我通常在没有虚拟显示器的情况下运行(在开发过程中),并且需要相同的时间。我刚刚检查过,整个setUpClass方法只需2秒钟。@alecxe这也是我遇到的问题之一,我不确定如何使用django测试测试/观察哪些行正在运行,以查看什么影响性能。用一个示例更新了我的答案。