Python 属性错误';模块';对象没有属性';运行服务器&x27;在django
我有一个函数测试“y1.py”,我正试图从python/django函数中调用它。在调用函数中,我有:Python 属性错误';模块';对象没有属性';运行服务器&x27;在django,python,django,Python,Django,我有一个函数测试“y1.py”,我正试图从python/django函数中调用它。在调用函数中,我有: import unittest import ft1.y1 unittest.main(module=ft1.y1.py) y1.py: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys fr
import unittest
import ft1.y1
unittest.main(module=ft1.y1.py)
y1.py:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
class Y1(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.yahoo.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_y1(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_link_text("Weather").click()
driver.save_screenshot('out11.png')
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
Exception Type: AttributeError at /runtest/
Exception Value: 'module' object has no attribute 'runserver'
我怎样才能解决这个问题
编辑:
我试过:
unittest.main(module=ft1.y1, argv=[])
得到:
Traceback:
File "F:\envs\r1\lib\site-packages\django\core\handlers\base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "F:\envs\r1\driver1\driver\views.py" in runtest
31. unittest.main(module=ft1.y1, argv=[])
File "f:\ppython275\App\Lib\unittest\main.py" in __init__
93. self.progName = os.path.basename(argv[0])
Exception Type: IndexError at /runtest/
Exception Value: list index out of range
编辑2:
我很困惑,它说测试正常,但有一个错误:
A server error occurred. Please contact the administrator.
Validating models...
0 errors found
January 31, 2014 - 14:42:50
Django version 1.6.1, using settings 'driver1.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
.
----------------------------------------------------------------------
Ran 1 test in 6.286s
OK
Traceback (most recent call last):
File "f:\ppython275\App\Lib\wsgiref\handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "F:\envs\r1\lib\site-packages\django\contrib\staticfiles\handlers.py", line 67, in __call__
return self.application(environ, start_response)
File "F:\envs\r1\lib\site-packages\dj_static.py", line 59, in __call__
return self.application(environ, start_response)
File "F:\envs\r1\lib\site-packages\django\core\handlers\wsgi.py", line 206, in __call__
response = self.get_response(request)
File "F:\envs\r1\lib\site-packages\django\core\handlers\base.py", line 114, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "F:\envs\r1\driver1\driver\views.py", line 32, in runtest
unittest.main(module=ft1.y1, argv=sys.argv[:1])
File "f:\ppython275\App\Lib\unittest\main.py", line 95, in __init__
self.runTests()
File "f:\ppython275\App\Lib\unittest\main.py", line 234, in runTests
sys.exit(not self.result.wasSuccessful())
SystemExit: False
[31/Jan/2014 14:43:03] "GET /runtest/ HTTP/1.1" 500 59
您正在尝试从视图运行unitest和selenium吗?你应该考虑启动。这使您能够更好地分离django模块和测试模块。如果坚持使用unittest.main(),请传递argv并退出参数
import sys
unittest.main(module=ft.gtest, argv=sys.argv[:1], exit=False)
另见:
(已编辑)Edited:添加了退出参数,但您确实应该使用不同的过程。