Pycharm 2016.2执行单元测试遇到如下问题:

RuntimeWarning: Parent module ‘YOUR_MODULE_HERE’ not found while handling absolute import
import unittest

解决方法,使用旧的utrunner.py文件替换当前版本,文件路径为…/PyCharm.app/Contents/helpers/pycharm/utrunner.py(macos平台)或者…\JetBrains\PyCharm 2016.2.2\helpers\pycharm\utrunner.py(windows平台)。utrunner.py的内容如下(替换文件前请先做好备份):

 import sys
import imp
import os
import fnmatch helpers_dir = os.getenv("PYCHARM_HELPERS_DIR", sys.path[0])
if sys.path[0] != helpers_dir:
sys.path.insert(0, helpers_dir) from tcunittest import TeamcityTestRunner
from nose_helper import TestLoader, ContextSuite
from pycharm_run_utils import import_system_module
from pycharm_run_utils import adjust_sys_path
from pycharm_run_utils import debug, getModuleName, PYTHON_VERSION_MAJOR adjust_sys_path() os = import_system_module("os")
re = import_system_module("re") modules = {} def loadSource(fileName):
baseName = os.path.basename(fileName)
moduleName = os.path.splitext(baseName)[0] # for users wanted to run unittests under django
#because of django took advantage of module name
settings_file = os.getenv('DJANGO_SETTINGS_MODULE')
if settings_file and moduleName == "models":
baseName = os.path.realpath(fileName)
moduleName = ".".join((baseName.split(os.sep)[-2], "models")) if moduleName in modules and len(sys.argv[1:-1]) == 1: # add unique number to prevent name collisions
cnt = 2
prefix = moduleName
while getModuleName(prefix, cnt) in modules:
cnt += 1
moduleName = getModuleName(prefix, cnt)
debug("/ Loading " + fileName + " as " + moduleName)
if os.path.isdir(fileName):
fileName = fileName + os.path.sep
module = imp.load_source(moduleName, fileName)
modules[moduleName] = module
return module def walkModules(modulesAndPattern, dirname, names):
modules = modulesAndPattern[0]
pattern = modulesAndPattern[1]
# fnmatch converts glob to regexp
prog_list = [re.compile(fnmatch.translate(pat.strip())) for pat in pattern.split(',')]
for name in names:
for prog in prog_list:
if name.endswith(".py") and prog.match(name):
modules.append(loadSource(os.path.join(dirname, name))) # For default pattern see https://docs.python.org/2/library/unittest.html#test-discovery
def loadModulesFromFolderRec(folder, pattern="test*.py"):
modules = []
# fnmatch converts glob to regexp
prog_list = [re.compile(fnmatch.translate(pat.strip())) for pat in pattern.split(',')]
for root, dirs, files in os.walk(folder):
files = [f for f in files if not f[0] == '.']
dirs[:] = [d for d in dirs if not d[0] == '.']
for name in files:
for prog in prog_list:
if name.endswith(".py") and prog.match(name):
modules.append(loadSource(os.path.join(root, name)))
return modules testLoader = TestLoader()
all = ContextSuite()
pure_unittest = False def setLoader(module):
global testLoader, all
try:
module.__getattribute__('unittest2')
import unittest2 testLoader = unittest2.TestLoader()
all = unittest2.TestSuite()
except:
pass if __name__ == "__main__":
arg = sys.argv[-1]
if arg == "true":
import unittest testLoader = unittest.TestLoader()
all = unittest.TestSuite()
pure_unittest = True if len(sys.argv) == 2: # If folder not provided, we need pretend folder is current
sys.argv.insert(1, ".") options = {}
for arg in sys.argv[1:-1]:
arg = arg.strip()
if len(arg) == 0:
continue if arg.startswith("--"):
options[arg[2:]] = True
continue a = arg.split("::")
if len(a) == 1:
# From module or folder
a_splitted = a[0].split("_args_separator_") # ";" can't be used with bash, so we use "_args_separator_"
if len(a_splitted) != 1:
# means we have pattern to match against
if os.path.isdir(a_splitted[0]):
debug("/ from folder " + a_splitted[0] + ". Use pattern: " + a_splitted[1])
modules = loadModulesFromFolderRec(a_splitted[0], a_splitted[1])
else:
if os.path.isdir(a[0]):
debug("/ from folder " + a[0])
modules = loadModulesFromFolderRec(a[0])
else:
debug("/ from module " + a[0])
modules = [loadSource(a[0])] for module in modules:
all.addTests(testLoader.loadTestsFromModule(module)) elif len(a) == 2:
# From testcase
debug("/ from testcase " + a[1] + " in " + a[0])
module = loadSource(a[0])
setLoader(module) if pure_unittest:
all.addTests(testLoader.loadTestsFromTestCase(getattr(module, a[1])))
else:
all.addTests(testLoader.loadTestsFromTestClass(getattr(module, a[1])),
getattr(module, a[1]))
else:
# From method in class or from function
debug("/ from method " + a[2] + " in testcase " + a[1] + " in " + a[0])
module = loadSource(a[0])
setLoader(module) if a[1] == "":
# test function, not method
all.addTest(testLoader.makeTest(getattr(module, a[2])))
else:
testCaseClass = getattr(module, a[1])
try:
all.addTest(testCaseClass(a[2]))
except:
# class is not a testcase inheritor
all.addTest(
testLoader.makeTest(getattr(testCaseClass, a[2]), testCaseClass)) debug("/ Loaded " + str(all.countTestCases()) + " tests")
TeamcityTestRunner().run(all, **options)

转载:http://blog.csdn.net/yyinhai/article/details/53410246

最新文章

  1. [原创]Centos7 从零配置Nginx+PHP+MySql
  2. 《Git教程-廖雪峰》学习笔记
  3. .net framework环境
  4. 面试题_103_to_124_关于 OOP 和设计模式的面试题
  5. SQL函数经常用到的mark一下
  6. Java 在本地文件中查找固定字符串
  7. OC在终端编写和运行
  8. python之celery的使用(一)
  9. vue传参二
  10. 设置 Confluence 6 外部索引站点
  11. 使用ansible安装docker以及docker-compose
  12. Codeforces Round #258 (Div. 2) C. Predict Outcome of the Game 水题
  13. Stay hungry, Stay foolish 的原义
  14. Pycharm自动换行
  15. [图解tensorflow源码] Graph 图优化 (graph optimizer)
  16. 转:Windows中的命令行提示符里的Start命令执行路径包含空格时的问题
  17. mac系统终端的color scheme配置和vim配置
  18. boost::lexical_cast
  19. LInux中ThreadInfo中的preempt_count字段
  20. git自动更新网站代码

热门文章

  1. 有哪些你不知道的python小工具
  2. 浅谈Cookie与Session技术
  3. 一些值得收藏的MySQL知识链接
  4. Beta冲刺——第一天
  5. sessionStorage实现note的功能
  6. CAFE: a computational tool for the study of gene family evolution
  7. 【Django】RROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)
  8. SVD及其在推荐系统中的作用
  9. FortiGate防火墙对数据包处理流程
  10. [leetcode]46. Permutations全排列(给定序列无重复元素)