[英]unittest - Importerror
I'm following this tutorial on web2py where you get to make a testdriven environment. 我正在关注web2py的本教程 ,你可以在这里制作一个测试驱动的环境。 However when I try to run the test with unittest, selenium I get this error:
但是,当我尝试使用unittest运行测试时,selenium我收到此错误:
$ python functional_tests.py
running tests
Traceback (most recent call last):
File "functional_tests.py", line 56, in <module>
run_functional_tests()
File "functional_tests.py", line 46, in run_functional_tests
tests = unittest.defaultTestLoader.discover('fts')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 202, in discover
raise ImportError('Start directory is not importable: %r' % start_dir)
ImportError: Start directory is not importable: 'fts'
This is how the functional_tests.py looks like: 这就是functional_tests.py的样子:
#!/usr/bin/env python
try: import unittest2 as unittest #for Python <= 2.6
except: import unittest
import sys, urllib2
sys.path.append('./fts/lib')
from selenium import webdriver
import subprocess
import sys
import os.path
ROOT = 'http://localhost:8001'
class FunctionalTest(unittest.TestCase):
@classmethod
def setUpClass(self):
self.web2py = start_web2py_server()
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(1)
@classmethod
def tearDownClass(self):
self.browser.close()
self.web2py.kill()
def get_response_code(self, url):
"""Returns the response code of the given url
url the url to check for
return the response code of the given url
"""
handler = urllib2.urlopen(url)
return handler.getcode()
def start_web2py_server():
#noreload ensures single process
print os.path.curdir
return subprocess.Popen([
'python', '../../web2py.py', 'runserver', '-a "passwd"', '-p 8001'
])
def run_functional_tests(pattern=None):
print 'running tests'
if pattern is None:
tests = unittest.defaultTestLoader.discover('fts')
else:
pattern_with_globs = '*%s*' % (pattern,)
tests = unittest.defaultTestLoader.discover('fts', pattern=pattern_with_globs)
runner = unittest.TextTestRunner()
runner.run(tests)
if __name__ == '__main__':
if len(sys.argv) == 1:
run_functional_tests()
else:
run_functional_tests(pattern=sys.argv[1])
I solved this problem by replacing fts with the full path ie /home/simon/web2py/applications/testapp/fts 我通过用完整路径替换fts解决了这个问题,即/ home / simon / web2py / applications / testapp / fts
Hope this helps 希望这可以帮助
I had the same problem and based on an excellent article unit testing with web2py , I got this to work by doing the following: 我有同样的问题,并基于web2py的优秀文章单元测试 ,我通过执行以下操作来实现此目的:
I am no expert but hopefully this will work for you also. 我不是专家,但希望这对你也有用。
import unittest
from selenium import webdriver
import subprocess
import urllib2
execfile("applications/tukker/controllers/default.py", globals())
ROOT = 'http://localhost:8001'
def start_web2py_server():
return subprocess.Popen([
'python', '/home/alan/web2py/web2py/web2py.py', 'runserver',
'-a "passwd"', '-p 8001' ])
class FunctionalTest(unittest.TestCase):
@classmethod
def setUpClass(self):
self.web2py = start_web2py_server()
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(1)
@classmethod
def tearDownClass(self):
self.browser.close()
self.web2py.kill()
def get_response_code(self, url):
"""Returns the response code of the given url
url the url to check for
return the response code of the given url
"""
handler = urllib2.urlopen(url)
return handler.getcode()
def test_can_view_home_page(self):
# John opens his browser and goes to the home-page of the tukker app
self.browser.get(ROOT + '/tukker/')
# He's looking at homepage and sees Heading "Messages With 300 Chars"
body = self.browser.find_element_by_tag_name('body')
self.assertIn('Messages With 300 Chars', body.text)
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(FunctionalTest))
unittest.TextTestRunner(verbosity=2).run(suite)
First you have to do some changes in wrong paths in fts/functional_tests.py 首先,您必须在fts / functional_tests.py中的错误路径中进行一些更改
search for 搜索
'python', '../../web2py.py', 'runserver', '-a "passwd"', '-p 8001'
and change it to 并将其更改为
'python', '../../../web2py.py', 'runserver', '-a "passwd"', '-p 8001'
then 然后
tests = unittest.defaultTestLoader.discover('fts')
to 至
tests = unittest.defaultTestLoader.discover('.')
then 然后
tests = unittest.defaultTestLoader.discover('fts', pattern=pattern_with_globs)
to 至
tests = unittest.defaultTestLoader.discover('.', pattern=pattern_with_globs)
and 和
sys.path.append('fts/lib')
to 至
sys.path.append('./lib')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.