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

【pycharm 警告】unittest RuntimeWarning: Parent module ” not found while handling absolute import的更多相关文章

  1. 解决- RuntimeWarning: Parent module '...' not found while handling absolute import

    Pycharm 升级到 2016.3 以后运行 unittest 报警告如下: 网上查资料说是pycharm的一个已知但未修复的bug,解决办法如下: 使用旧的utrunner.py替换新的utrun ...

  2. pycharm 单元测试失败 not found while handling absolute import

    pycharm 单元测试运行错误 RuntimeWarning: Parent module 'tests' not found while handling absolute import impo ...

  3. python中,下级模块引用上级模块:SystemError: Parent module '' not loaded, cannot perform relative import

    当在下级中引用上级时,使用相对导包会出错,SystemError: Parent module '' not loaded, cannot perform relative import 运行test ...

  4. RuntimeWarning: Parent module 'test_project.test_case' not found while handling absolute

    1.Pycharm2016.3.2,导入unittest框架后,运行脚本总是warming,但不影响脚本具体执行 2.通过网上查询,将"C:\Program Files\JetBrains\ ...

  5. [解决方案]SystemError: Parent module '' not loaded, cannot perform relative import的解决方案

    缺陷:__mian__不能使用相对导入 PEP 328 Relative Imports and __name__ 中说明: Relative imports use a module's __nam ...

  6. 关于在pycharm下提示ModuleNotFoundError: No module named 'XXX' 的一种可能

    在pycharm下出现“ModuleNotFoundError: No module named 'XXX' ”提示时, 在网上找大部分的解决方案是重新在pycharm里安装对应的模块,但是这不是我要 ...

  7. Pycharm 中错误ImportError: No module named appium

    Q: Pycharm 中错误ImportError: No module named appium A: Pycharm IDE Preferences -> Project Interpret ...

  8. PyCharm上unittest 测试用例两种执行方式

    每次讲到unittest测试框架的时候,经常有同学在群里反映,为啥我的运行不是按照我往suite里添加的用例顺序执行的,为什么别人的能跑出来报告,我的就不行... 当然,首先确认代码是OK的,如果代码 ...

  9. cocos2dx在Eclipse下编译报错:Cannot find module with tag 'CocosDenshion/android' in import path

    在Eclipse下编译cocos2dx项目,报错如下: Android NDK: jni/Android.mk: Cannot find module with tag 'CocosDenshion/ ...

随机推荐

  1. 如何使用 Visual C# .NET 处理 Excel 事件

    事件处理概述 Visual C# .NET 使用委派处理来自组件对象模型 (COM) 服务器的事件.委派是 Microsoft Visual Studio .NET 中的一个新概念.对于 COM 事件 ...

  2. js原型继承四步曲

    <sctript> //1.创建父类 function Parent(){ this.name = name; } Parent.prototype.age = 20; //2.创建子类 ...

  3. [Ting's笔记Day5]在部署到Heroku之前,将Rails项目从SQLite设定为PostgreSQL

    前情提要: Paas(平台及服务)公司Heroku是个可以把我们写好的App部署到网际网络的好地方.而本篇是我从自己的上一篇文章:将Ruby on Rails项目部署到Heroku遇到的问题,当时困扰 ...

  4. 如何在 ajax 外拿到 ajax 的数据???和ajax的参数

    第一步:  var 变量名 = $.ajax({ url: "发送请求的地址", dataType: 'json', type: 'post', async: false }) 第 ...

  5. DB2(Procedure)存储过程遍历循环!

    有时候一些复杂的业务逻辑将要通过存储过程的循环语句进行处理;以下列出2种DB2存储过程的循环语句,方便以后的查看并使用! 推荐第一种方式的使用,最大的优点就是比较直观;在需要操作很多字段的情况下,不需 ...

  6. python爬虫小说代码,可用的

    python爬虫小说代码,可用的,以笔趣阁为例子,python3.6以上,可用 作者的QQ:342290433,汉唐自远工程师 import requests import refrom lxml i ...

  7. reentrantlocklock实现有界队列

    今天找synchronize和reentrantlock区别的时候,发现有个使用reentrantlock中的condition实现有界队列,感觉挺有趣的,自己顺手敲了一遍 class Queue{ ...

  8. 一个域名下多个Vue项目

    公司写的网站要英文和中文的,所以就写了两个项目,都是用vue写的单页面项目,但是域名只有一个,所以就想把两个vue项目合并到一个域名下面.思考:vue的页面都是单页面应用,说白了就是一个index.h ...

  9. SQL Server 2008 R2 根据WSDL访问WebService

    参考网站:WebService学习整理(一)——客户端三种调用方式整理 自我概括: WebService 通过HTTP通讯,数据以XML格式传输使两个系统进行数据交互 SOAP 是访问协议(注明访问W ...

  10. java项目中VO和DTO以及Entity,各自是在什么情况下应用

    1.entity里的每一个字段,与数据库相对应, 2.dto里的每一个字段,是和你前台页面相对应, 3.VO,这是用来转换从entity到dto,或者从dto到entity的中间的东西.   举个例子 ...