pycharm下运行unittest的问题
环境:
系统:window7 64
软件:pycharm
版本:2016.3.2
问题描述:
使用unittest类的时候出现问题,问题截图如下
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的内容如下(替换文件前请先做好备份):
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 import RuntimeWarning after updating to 2016.2 - 简书
- RuntimeWarning: Parent module ‘YOUR_MODULE_HERE’ not found while handling absolute import : PY-20171
- RuntimeWarning: Parent module ‘settings’ not found while handling absolute import - 常城的专栏 - 博客频道 - CSDN.NET
- http://blog.csdn.net/wirelessqa/article/details/53465854
本人个人博客:巫章鹏的个人博客
pycharm下运行unittest的问题的更多相关文章
- 亲测——pycharm下运行第一个scrapy项目 ©seven_clear
最近在学习scrapy,就想着用pycharm调试,但不知道怎么弄,从网上搜了很多方法,这里总结一个我试成功了的. 首先当然是安装scrapy,安装教程什么的网上一大堆,这里推荐一个详细的:http: ...
- Pycharm下运行程序查看每个变量的值的方法(类似于Spyder和MATLAB)
昨天,用了大量篇幅讲了Spyder的各种问题,之所以要用Spyder,最重要的一个原因就是能够非常方便的查看中间变量的值.类似MATLAB的工作空间,非常方便.如下图所示: 但是Spyder的代码自动 ...
- pycharm下运行和调试scrapy项目
1. 新建项目 默认在本地已经新建了一个scrapy爬虫项目 2. 打开项目 点击open à 选择刚刚那个本地的scrapy项目meijutt100 3. 项目结构 各个py文件的作用不作介绍,不懂 ...
- pycharm右键运行unittest、pytest文件
在实际学习过程中,有时候会出现右键运行文件,但没有任何结果的情况.这就是没有使用unittest/pytest 的方式运行. 解决方法: 添加好
- PyCharm 默认运行 unittest
若文件里面有某个函数名称或模块名称以test为前缀,Pycharm的话,就会自动认为是单元测试: 报错信息:test_file() missing 1 required positional argu ...
- Pycharm取消默认的右击运行unittest方法
Pycharm取消默认的右击运行unittest方法:File-> Settings -> Tools -> Python Integrated Tools -> Defaul ...
- PyCharm下创建并运行我们的第一个Django项目
PyCharm下创建并运行我们的第一个Django项目 准备工作: 假设读者已经安装好python 2x或3x,以及安装好Django,以及Pycharm 1. 创建一个新的工程 第一次运行Pycha ...
- pycharm上运行django服务器端、ip地址访问
安装Django 下载Django包,解压缩. CMD 进入解压路径下. 执行:python setup.py install 增加环境变量: C:\Python27\Scripts 测试djang ...
- pycharm上运行django服务器端、以及创建app方法
快来加入群[python爬虫交流群](群号570070796),发现精彩内容. 安装Django 下载Django包,解压缩. CMD 进入解压路径下. 执行:python setup.py in ...
随机推荐
- cacti-不出图形,cacti.log中出“ERROR: SQL Assoc Failed!
[root@CactiEZ log]# tail cacti.log 2016年04月06日 14:53:16 PM - CMDPHP: Poller[0] ERROR: SQL Cell Faile ...
- Windows查看端口占用及杀掉进程
"到底是谁占用了80端口,该怎么查,怎么终止它?",这里就简单讲解一下,在windows下如何查看80端口占用情况?是被哪个进程占用?如何终止等.这里主要是用到windows下的D ...
- code1796 社交网络
输入描述 Input Description 输入文件中第一行有两个整数,n 和 m,表示社交网络中结点和无向边的数 目.在无向图中,我们将所有结点从 1 到 n 进行编号. 接下来 m 行,每行用三 ...
- Spring Boot☞ 统一异常处理
效果区: 代码区: package com.wls.integrateplugs.exception.dto; public class ErrorInfo<T> { public st ...
- 【Django】Django Debug Toolbar调试工具配置
正在发愁怎么调试Django,就遇到了Django Debug Toolbar这个利器. 先说遇到的问题: 网上也有教程,不过五花八门的,挨个尝试了一遍,也没有成功运行.最后终于找到问题所在: 从开发 ...
- HUST软工1501-1503班第4周作业成绩公布
说明 本次公布的成绩为第四周作业的结果: 第4周小组作业:WordCount优化 如果同学对作业结果存在异议,可以: 在毕博平台讨论区的第4周在线答疑区发帖申诉. 或直接在博客园本帖中进行评论进行申诉 ...
- Yii2 修改 breadcrumb 首页图标
<?=Breadcrumbs::widget([ 'homeLink' => [ 'label' => '<i class="fa fa-home"> ...
- mysql数据库引擎MyISAM与InnoDB的区别浅说
mysql的存储引擎包括:MyISAM.InnoDB.BDB.MEMORY.MERGE.EXAMPLE.NDBCluster.ARCHIVE.CSV.BLACKHOLE.FEDERATED等,其中In ...
- Python之模块二
10>常用模块: 1>os模块: os.getcwd():获取当前工作目录,即当前python脚本工作的目录路径: os.chdir("dirname"):改变当前脚本 ...
- Java文件路径
几大常用的方法 Class.getResource("") 返回的是当前Class这个类所在包开始的位置 getClassLoader().getResource(" ...
