我有一个功能测试'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
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:
我很困惑它说测试OK,但是有一个错误:
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
答案 0 :(得分:2)
您是否尝试从视图中运行unitest和selenium?您应该考虑启动second process for that。这使您可以更好地分离django模块和测试模块。如果你坚持使用unittest.main(),则传递argv并退出params。
import sys
unittest.main(module=ft.gtest, argv=sys.argv[:1], exit=False)
另见:
(编辑)的