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 ...
随机推荐
- 文件操作putc
putc是把一个字符写入到指定文件中,每写一个字符,文件指针自动加1. 我写了一个随机生成255字符到d:/456.txt的程序. int main() { FILE *p; int num[255] ...
- Openssl rsautl命令
一.简介 rsautl指令能够使用RSA算法签名,验证身份,加密/解密数据 二.语法 openssl rsautl [-in file] [-out file] [-inkey file] [-pas ...
- scrapy框架 小知识
持久化 去重规则 深度 cookie start_url 深度和优先级 下载中间件 持久化 步骤 pipeline/items a. 先写pipeline类 class XXXPipeline(obj ...
- jQuery自动截取文字长度,超过部分
<html> <head> <meta charset="utf-8"> <script src="js/jqu ...
- 白盒测试实践--Day4 12.20
累计完成任务情况: 阶段内容 参与人 完成个人情况说明并提交作业 全体 汇总作业,查漏补缺,完成代码测试总结 小靳.小龙 完成测试小结 小黄.小尹 完成静态代码检查结果报告 小靳 完成JUnit脚本编 ...
- python 中面向对象编程简单总结1
1.类的定义方式 class ClassName(object): #object 表示继承的类,默认为object pass 类的使用类似函数的调用,也可以认为是调用了一次模板来创建一个实例. 2. ...
- HDU 6069 Counting Divisors (素数+筛法)
题意:给定 l,r,k,让你求,其中 l <= r <= 1e12, r-l <= 1e6, k <= 1e7. 析:首先这个题肯定不能暴力,但是给定的区间较小,可以考虑筛选, ...
- Demo—标题左右两侧的对等横线
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C# 随机串的生成
/** * 生成随机串,随机串包含字母或数字 * @return 随机串 */ public static string GenerateNon ...
- 编写高质量代码改善C#程序的157个建议——建议65:总是处理未捕获的异常
建议65:总是处理未捕获的异常 处理为捕获的异常是每个应用程序具备的基本功能,C#在APPDomain提供了UnhandledException事件来接收未捕获到的异常的通知.常见的应用如下: sta ...