pycharm 单元测试失败 not found while handling absolute import
pycharm 单元测试运行错误
RuntimeWarning: Parent module 'tests' not found while handling absolute import
import unittest
RuntimeWarning: Parent module 'tests' not found while handling absolute import
import datetime as dt
Pycharm import RuntimeWarning after updating to 2016.2.
用老版本的utrunner.py替换新版本的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)
pycharm 单元测试失败 not found while handling absolute import的更多相关文章
- 【pycharm 警告】unittest RuntimeWarning: Parent module ” not found while handling absolute import
Pycharm 2016.2执行单元测试遇到如下问题: RuntimeWarning: Parent module ‘YOUR_MODULE_HERE’ not found while handlin ...
- 解决- RuntimeWarning: Parent module '...' not found while handling absolute import
Pycharm 升级到 2016.3 以后运行 unittest 报警告如下: 网上查资料说是pycharm的一个已知但未修复的bug,解决办法如下: 使用旧的utrunner.py替换新的utrun ...
- maven执行单元测试失败后,继续生成Jacoco&Sonar报告
为保证生成单元测试覆盖 sonarqube或者jacoco与maven集成时,如果pom文件配置了sonarqube或者Jacoco的相关配置, 那么在pom文件所在目录执行mvn clean ins ...
- maven项目, 单元测试失败提示 Class not found datastorage........
---恢复内容开始--- 单元测试失败: 提示 Class not found datastorage........ 原因: maven 环境变量问题, eclipse 没有自动更新下载 ...
- Eclipse编译运行没问题,但执行mvn clean install跑单元测试失败的原因解析
问题描述:mvn clean install编译工程并运行单元测试出现如下错误 Tests run: 3, Failures: 0, Errors: 2, Skipped: 0, Time elaps ...
- RuntimeWarning: Parent module 'test_project.test_case' not found while handling absolute
1.Pycharm2016.3.2,导入unittest框架后,运行脚本总是warming,但不影响脚本具体执行 2.通过网上查询,将"C:\Program Files\JetBrains\ ...
- 16.用pycharm导入自己写的模块时,import无法识别的解决办法
我们用pycharm打开自己写的代码,当多个文件之间有相互依赖的关系的时候,import无法识别自己写的文件,但是我们写的文件又确实在同一个文件夹中, 这种问题可以用下面的方法解决: 1)打开File ...
- pyhanlp安装成功,import导入失败,出现:importerror: cannot import name 'jvmnotfoundexception'
1.问题描述: pyhanlp成功安装,并且可以正常使用,但是这段时间再去用的时候,发现出问题了,一运行就出现,下面的问题: importerror: cannot import name 'jvmn ...
- pycharm下运行unittest的问题
环境: 系统:window7 64 软件:pycharm 版本:2016.3.2 问题描述: 使用unittest类的时候出现问题,问题截图如下 Pycharm 2016.2执行单元测试遇到如下问题: ...
随机推荐
- jquery ajax 提交信息后等待返回的提示信息
最简单的方法: http://bbs.csdn.net/topics/390584283?page=1 $('#click').click(function){ $('#data').html('&l ...
- BZOJ4435 : [Cerc2015]Juice Junctions
最大流=最小割,而因为本题点的度数不超过3,所以最小割不超过3,EK算法的复杂度为$O(n+m)$. 通过分治求出最小割树,设$f[i][j][k]$表示最小割为$i$时,$j$点在第$k$次分治过程 ...
- 基于Theano的DL的开源小框架:Dragon
Link:https://github.com/neopenx/Dragon 起因 最近看了Hinton的Dropout,发现原来的乱代码只能在Softmax层前面加Dropout.索性把整个Thea ...
- Coder-Strike 2014 - Finals (online edition, Div. 2) B. Start Up
需要满足的条件是 (1)每个字母是对称的 (2)每个字符串是对称的 #include <iostream> #include <algorithm> #include < ...
- 警惕自己,不断学习c++【转】
每天早上起床看一遍,时刻警惕自己,每天至少要浏览http://www.cplusplus.com 1.把C++当成一门新的语言学习(和C没啥关系!真的.):2.看<Thinking In C++ ...
- 洛谷 P1379 八数码难题 Label:判重&&bfs
特别声明:紫书上抄来的代码,详见P198 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给 ...
- 巧用translate设置元素垂直水平居中
之前在做手机项目时,用到很多自定义弹窗,然后要求都垂直水平要居中,最开始的时候想用calc来计算,可是css3 的calc兼容性不是很好,于是后来就借助了js来计算, 今天偶然看到别人的一个方法,瞬间 ...
- 预定义指令之debug
1)根据你必须知道的.NET一书, #define DEBUG class Program { static void Main(string[] args) { #if DEBUG Console. ...
- 20145330《Java程序设计》第五次实验报告
20145330<Java程序设计>第五次实验报告 实验五 Java网络编程及安全 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统 4.结队伙伴 ...
- OSG动画学习
OSG动画学习 转自:http://bbs.osgchina.org/forum.php?mod=viewthread&tid=3899&_dsign=2587a6a9 学习动画,看了 ...