gpt4 book ai didi

python - 单元测试 - 导入错误

转载 作者:行者123 更新时间:2023-11-28 21:13:34 24 4
gpt4 key购买 nike

我正在关注这个 tutorial在 web2py 上,你可以在那里创建一个测试驱动的环境。但是,当我尝试使用 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'

这是 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])

最佳答案

我通过用完整路径替换 fts 解决了这个问题,即/home/simon/web2py/applications/testapp/fts

希望对你有帮助

关于python - 单元测试 - 导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12350907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com