python - 带有 selenium 的单元测试在 python 中完美无缺,但在 django manage.py 测试中出现导入错误

标签 python django selenium functional-testing importerror

我有这个文件,是从 Selenium IDE 生成的

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re

class Tcase(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://localhost:9999/"
        self.verificationErrors = []

    def test_tcase(self):
        driver = self.driver
        driver.get(self.base_url)
        # other things...

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

我正在开发一个 Django 项目 (1.4) 和 python 2.6,我想用 Selenium 对其进行测试。

如果我直接从 python 运行这个文件,它可以正常工作。

如果我运行这个做

python manage.py test myapp

导入错误

C:\web\opineo> python .\manage.py test core
Traceback (most recent call last):
  File ".\manage.py", line 14, in <module>
    execute_manager(settings)
  File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line 459, in execute_manager
    utility.execute()
  File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python26\lib\site-packages\django\core\management\commands\test.py", line 49, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "C:\Python26\lib\site-packages\south-0.7.3-py2.6.egg\south\management\commands\test.py", line 8, in handle
    super(Command, self).handle(*args, **kwargs)
  File "C:\Python26\lib\site-packages\django\core\management\commands\test.py", line 72, in handle
    failures = test_runner.run_tests(test_labels)
  File "C:\Python26\lib\site-packages\django\test\simple.py", line 380, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
  File "C:\Python26\lib\site-packages\django\test\simple.py", line 264, in build_suite
    suite.addTest(build_suite(app))
  File "C:\Python26\lib\site-packages\django\test\simple.py", line 79, in build_suite
    test_module = get_tests(app_module)
  File "C:\Python26\lib\site-packages\django\test\simple.py", line 36, in get_tests
    test_module = import_module('.'.join(prefix + [TEST_MODULE]))
  File "C:\Python26\lib\site-packages\django\utils\importlib.py", line 35, in import_module
    __import__(name)
  File "C:\web\opineo\core\tests\__init__.py", line 3, in <module>
    from core.tests.func import *
  File "C:\web\opineo\core\tests\func.py", line 1, in <module>
    from selenium import webdriver
  File "C:\web\opineo\core\tests\selenium.py", line 3, in <module>
    from selenium import webdriver
ImportError: cannot import name webdriver

怎么了? Selenium 文件在那里!在 Python 2.6 的站点包中

编辑:

这有效,正如预期的那样,它打开了 Firefox

PS C:\web\opineo> python.exe .\manage.py shell
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32
>>> from selenium import webdriver
>>> driver = webdriver.Firefox()

最佳答案

最后我发现了问题:从测试中调用的文件名为 selenium.py,所以它覆盖了库;一个非常新手的错误。

这就是它在 selenium 中找不到 webdriver 的原因!

关于python - 带有 selenium 的单元测试在 python 中完美无缺,但在 django manage.py 测试中出现导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11701677/

相关文章:

python - 更改 *splat 和 **splatty-splat 运算符对我的对象执行的操作

python - 如何找到Python的不同安装并删除它们?

java - Selenium:-browser.helperApps.neverAsk.openFile 和 savetodisk 不起作用

angularjs - 在 Protractor 中使用占位符查找元素?

javascript - 如何在实习生框架中执行带有循环的代码块

python - 从 python 中的 {x,y,z}-散点数据绘制 3D 表面

python - "Cannot deserialize instance of string from START_ARRAY value"Salesforce API问题

javascript - Django 表单用 "This field is required"消息初始化

python - 表级操作和记录级操作有什么区别?

python - 为什么我的 index.html 不能连接到我的 index.css 文件?