硒测试在个别测试之间测试非常

硒测试在个别测试之间测试非常

本文介绍了硒测试在个别测试之间测试非常缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Django 1.4, Selenium 2.53.1 and Chrome Webdriver 2.21 as my testing webdriver to test my Django app.

我初始化我的课程:

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='[email protected]')
    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()

我的一个测试例子如下。它测试一个下拉菜单,您将鼠标移到它们之后出现的多个级别,并检查它们是否正确链接。

An example one of my tests is below. It tests a dropdown with multiple levels that appear after you move your mouse over them and checks that they go to correct link

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')

我尝试了这些事情:


  1. 我已经为每个测试定了代码,他们花了不到一秒钟。

  2. 我也定了设置和设置类方法,最多需要2秒。

  3. 我已将set_page_load_timeout设置为0,并且不会更改累积执行时间。

  4. I通过每轮添加一个额外的测试来测试,发现每个测试的总测试时间增加了大约40秒。

  1. I've timed the code for each test, they take less than a second.
  2. I've also timed the setup and setupclass methods, and they take maximum 2 seconds.
  3. I've set to set_page_load_timeout to 0 and it does not change overal execution time.
  4. I've run the tests by adding one extra test each round and discovered that for each test there is an increase of around 40 seconds to the total test suite time.

鉴于此,整套8项测试需要超过300多秒,我不知道为什么。我确定加载Webdriver需要一些时间,但每次测试结束后,我可以看到Webdriver只是坐在那里,什么都不做。

Given this, the entire suite of 8 tests takes over 300+ seconds and I have no idea why. I'm sure the loading of Webdriver takes some time, but after each individual test ends, I can see the Webdriver just sit there and do nothing.

推荐答案

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

The biggest issue is that LiveServerTestCase is the slowest of all the test cases in the Django Project. It inherits from TransactionTestCase

重置数据库到每个测试开始时的一个已知状态
来简化测试和使用ORM。

Resetting the database to a known state at the beginning of each test to ease testing and using the ORM.



and

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

Thus each of your tests results in the database being completely reset, which is really slow. One solution is to use the -k or --keep option.

./manage.py test -k myapp

这将从您的测试执行时间开始至少100秒。

This will cut off at least 100 seconds from your test execution time.

另一种解决方案无法在所有条件下应用,因为TestCase不能用于硒测试。然而,您可以编写连接到dev服务器的独立硒测试,以加快一些测试速度。在这种情况下,您可以从python而不是 django.test.testcases.TestCase 使用 unittest.TestCase 。这听起来很混乱,所以让我举一个例子。

There is another solution that cannot be applied in all conditions because TestCase cannot be used for Selenium Tests. However you can write independent selenium tests that connect to the dev server to speed some tests up. In this case you use unittest.TestCase from from python instead of django.test.testcases.TestCase. That sounds totally confusing so let me give an example!

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或硒测试。使用django 更快更容易

Last but not least: Often you don't need LiveServerTestCase or selenium tests at all. It's much faster and easier to use the django test client

这篇关于硒测试在个别测试之间测试非常缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:38