当前位置:网站首页>UnitTest中的Path must be within the project 问题
UnitTest中的Path must be within the project 问题
2022-08-10 19:01:00 【觅梦_feng】
目前在代码中有如下结构:
discover = unittest.defaultTestLoader.discover(test_dir, pattern='Case*.py')
if runType.find("main") >= 0 :
#此处说明是通过执行器启动的单个用例测试,需生成单个用例的测试套件
dirList = runType.split("\\")
index = len(dirList)
testClassName = dirList[index -1 ]
#由于可能传入不同地址的测试用例 这里需要动态生成测试用例目录地址
start_index = dirList.index("main") + 1
test_dir = './'
for i in range(start_index, index - 1) :
test_dir += dirList[i] + '/'
print test_dir
discover = unittest.defaultTestLoader.discover(test_dir, pattern=testClassName)
也就是说 代码开始时需要生成一个 discover 给UnitTest进行run,之后由于不同的测试目的需要运行不同文件夹下的用例 这样就需要再更新一个 discover
运行时实际会报错Path must be within the project
根据代码字面意思 _relpath.startswith(‘…’) 就会报错,但是同样的两次调用为啥会有这样的问题呢?
查看了下discover的代码逻辑发现
unitTest中的 discover 第三个参数 top_level_dir 第一次运行时如果为None 会取当前传入的start_dir所在路径为 top_level_dir
而这个top_level_dir会作为self的参数保存下来,这样第二次运行时 top_level_dir实际取的是上一次的路径,直接影响到了下一次的运行
因此规避此问题的方法
1 要么在一次运行过程中只调用一次 discover
2 如果一定要调用两次,第二次需要手动将 top_level_dir这个参数传给discover方法,这样才能运行不同文件夹下的用例
即修改为
discover = unittest.defaultTestLoader.discover(test_dir, pattern=testClassName, top_level_dir = self.common.getMainPath())
边栏推荐
猜你喜欢
随机推荐
Optimizing Bloom Filter: Challenges, Solutions, and Comparisons论文总结
[Go WebSocket] Your first Go WebSocket server: echo server
MSE 治理中心重磅升级-流量治理、数据库治理、同 AZ 优先
2816. 判断子序列(双指针)
We used 48h to co-create a web game: Dice Crush, to participate in international competitions
【初学必备】3d游戏建模入门基础知识
#yyds干货盘点# 面试必刷TOP101:二分查找-I
799. 最长连续不重复(双指针)
[TAPL] 概念笔记
7-2 乒乓人训练大师(双指针)
Optimization is a habit The starting point is to 'stand close to the critical'
【Knowledge Sharing】What is SEI in the field of audio and video development?
消息队列初见:一起聊聊引入系统mq 之后的问题
西安Biotin-PEG8-IA_IA-PEG8-生物素供应商
JVM内存和垃圾回收-11.执行引擎
新建离线同步节点时选择数据去向-表时报错,数据库类型是adb pg,怎么办?
redis.exceptions.DataError: Invalid input of type: ‘dict‘. Convert to a byte, string or number first
pip3升级到22.2.2
2022杭电多校七 Black Magic (签到)
智能安防产品公司及产品