Python 覆盖率:导入错误:无法导入名称配置
当我尝试运行此代码时,我得到:Python 覆盖率:导入错误:无法导入名称配置,python,python-2.7,testing,coverage.py,Python,Python 2.7,Testing,Coverage.py,当我尝试运行此代码时,我得到:ImportError:无法导入此文件第32行中的名称配置: lib/python2.7/site-packages/tests/functional_tests/fixtures/__init__.py 你知道这个错误的原因吗 import os COV = None if os.environ.get('FLASK_COVERAGE'): import coverage COV = coverage.coverage(branch=True,
ImportError:无法导入此文件第32行中的名称配置
:
lib/python2.7/site-packages/tests/functional_tests/fixtures/__init__.py
你知道这个错误的原因吗
import os
COV = None
if os.environ.get('FLASK_COVERAGE'):
import coverage
COV = coverage.coverage(branch=True, include='app/*')
COV.start()
from flask.ext.script import Manager, Shell
from app import create_app, db
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
@manager.command
def test(coverage=False):
"""Run the unit tests."""
if coverage and not os.environ.get('FLASK_COVERAGE'):
import sys
os.environ['FLASK_COVERAGE'] = '1'
os.execvp(sys.executable, [sys.executable] + sys.argv)
import unittest
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
if COV:
COV.stop()
COV.save()
print('Coverage Summary:')
COV.report()
basedir = os.path.abspath(os.path.dirname(__file__))
covdir = os.path.join(basedir, 'tmp/coverage')
COV.html_report(directory=covdir)
print('HTML version: file://%s/index.html' % covdir)
COV.erase()
if __name__ == '__main__':
manager.run()
错误日志:
python coverage.py test
ERROR: tests.functional_tests.test_providers (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests.functional_tests.test_providers
Traceback (most recent call last):
File "/usr/lib/python2.7/unittest/loader.py", line 252, in _find_tests
module = self._get_module_from_name(name)
File "/usr/lib/python2.7/unittest/loader.py", line 230, in _get_module_from_name
__import__(name)
File "/home/vagrant/myproject/venv/local/lib/python2.7/site-packages/tests/functional_tests/__init__.py", line 1, in <module>
from . import fixtures
File "/home/vagrant/myproject/venv/local/lib/python2.7/site-packages/tests/functional_tests/fixtures/__init__.py", line 32, in <module>
from tests.functional_tests import config
ImportError: cannot import name config
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
python coverage.py测试
错误:tests.functional\u tests.test\u提供程序(unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError:导入测试模块失败:测试。功能测试。测试提供程序
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python2.7/unittest/loader.py”,第252行,在“查找”测试中
module=self.\u从\u name(name)获取\u module\u
文件“/usr/lib/python2.7/unittest/loader.py”,第230行,在_get_module_from_name中
__导入(名称)
文件“/home/vagrant/myproject/venv/local/lib/python2.7/site-packages/tests/functional_-tests/_-init__.py”,第1行,在
从…起进口固定装置
文件“/home/vagrant/myproject/venv/local/lib/python2.7/site-packages/tests/functional_-tests/fixtures/_-init___;.py”,第32行
从tests.functional_测试导入配置
ImportError:无法导入名称配置
----------------------------------------------------------------------
在0.001s内运行1次测试
失败(错误=1)
我不知道这里发生了什么,但不要将您的文件命名为“coverage.py”,这只会干扰已安装的coverage模块。为什么要编写自己的Python文件来运行测试?覆盖率运行-m unittest discover
有什么问题吗?您好@anvd您最终解决了这个问题吗?那是什么?