【问题标题】:Django+Selenium tests give different results with and without coverageDjango+Selenium 测试在覆盖和不覆盖的情况下给出不同的结果
【发布时间】:2014-04-17 01:33:50
【问题描述】:

如果我使用coverage 运行测试,硒测试会失败。如果我使用python manage.py test ... 运行它们,则此测试通过。

我有一个测试套件,包括一些普通的 python/django 测试以及一些使用 Selenium 的测试。 CustomWebDriver() 是 selenium.webdriver.firefox.webdriver 的子类。

class WebDriverITTTests(LiveServerTestCase, Someothermixins):
    @classmethod
    def setUpClass(cls):
        cls.webdriver = webdriver.CustomWebDriver()
        super(WebDriverITTTests, cls).setUpClass()

    def test_instructions_logic(self):
        ...
        login, check that some things happened on the server side
        open a page, check for "#some_element" loaded via ajax
        ...

当我在覆盖范围内运行test_instructions_logic 测试时它失败了,因为它找不到"#some_element"

覆盖命令:coverage run --source mymodule.subapp manage.py test mymodule.subapp --settings myproj.settings

Manage.py 命令:python manage.py test mymodule.subapp --settings myproj.settings

为什么这两种情况的测试结果不同?

【问题讨论】:

    标签: python django unit-testing selenium


    【解决方案1】:

    我想通了。我用this作为参考,问题是……

    1. 使用 Selenium find_element_by_blah() 时,如果元素是通过 Ajax 加载的,则需要等到它加载完毕后再查找。因此链接中的wait_for_css() 方法。 (我确实是用那个方法)。
    2. manage.py 测试运行器和coverage test 运行之间的差异可能是由于时间 - 如果我在检查元素之前添加了 time.sleep(2),则两种情况下的测试都会通过。
    3. 但是为什么wait_for_css方法不能防止这个问题???
    4. 因为我天真地创建了NoSuchElementException,喜欢

      class NoSuchElementException(Exception)
          pass
      

      而不是导入它:from selenium.common.exceptions import NoSuchElementException(因此链接中的find_css 方法引发了与 Selenium 预期不同的异常,并且失败并退出而不是等待)。

    哦。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-24
      • 2020-02-24
      • 1970-01-01
      • 2019-09-25
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多