使用selenium的unittest在python中运行完美,但在django manage.py测试中出现导入错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用selenium的unittest在python中运行完美,但在django manage.py测试中出现导入错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3392字,纯文字阅读大概需要5分钟。
内容图文
![使用selenium的unittest在python中运行完美,但在django manage.py测试中出现导入错误](/upload/InfoBanner/zyjiaocheng/792/0b64546a0e774935adfaf796d1afc73f.jpg)
我有这个文件,由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
怎么了?硒文件在那里!在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()
解决方法:
最后我发现了问题:从test调用的文件名为selenium.py,因此它覆盖了库;一个非常新手的错误.
这就是为什么它在selenium找不到webdriver的原因!
内容总结
以上是互联网集市为您收集整理的使用selenium的unittest在python中运行完美,但在django manage.py测试中出现导入错误全部内容,希望文章能够帮你解决使用selenium的unittest在python中运行完美,但在django manage.py测试中出现导入错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。