discover运行原理

assert not _relpath.startswith(’…’), “Path must be within the project”

遇到assert not _relpath.startswith(’…’), "Path must be within the project"如何解决?

在执行不同文件下的测试用例脚本时,提示执行脚本路径必须在此项目中(直白翻译官)。但是我却没有明白,我执行的用例脚本时在当前项目中,为什么会报错?
在这里插入图片描述
执行部分代码如下
在这里插入图片描述
报错来了
在这里插入图片描述
Traceback (most recent call last):
File “E:/apikuangjiatest/dirver/test_runner_V3.py”, line 56, in
discover = unittest.defaultTestLoader.discover(fdir, pattern=fname)
File “C:\Users\xiaopang\AppData\Local\Programs\Python\Python38-32\lib\unittest\loader.py”, line 349, in discover
tests = list(self._find_tests(start_dir, pattern))
File “C:\Users\xiaopang\AppData\Local\Programs\Python\Python38-32\lib\unittest\loader.py”, line 387, in _find_tests
name = self._get_name_from_path(start_dir)
File “C:\Users\xiaopang\AppData\Local\Programs\Python\Python38-32\lib\unittest\loader.py”, line 371, in _get_name_from_path
assert not _relpath.startswith(’…’), “Path must be within the project”
AssertionError: Path must be within the project

报错原因

discover 没有传 top_level_dir时 第一次运行时如果为None 会取当前传入的fdir所在路径为 top_level_dir,而top_level_dir会作为self的参数保存下来,这样第二次运行时 top_level_dir实际取的是上一次的路径,直接影响到了下一次的运行。
举个栗子:如果程序中先运行ind_xxx里面的脚本,再运行mul_xxx里面的脚本,则运行mul_xxx里面的脚本时就会报上面的错。(不知道客观明白了没有?)

解决方法

我运用到的方法是加 top_level_dir参数并且传递脚本对应的运行路径,改变代码如下,其余不变
p1是我当前文件所在路径的上一级目录,相当于我获取的相对路径,当然也可以加绝对路径(比如:E:\a\script)
p1是我当前文件所在路径的上一级目录,相当于我获取的相对路径,当然也可以加绝对路径(比如:E:\a\script),值得注意的是:这个路径是你的测试脚本所在文件夹的上一级目录(栗子:E:\a\script\ind_xxx\testa.py,写路径时需要写为E:\a\script).添加后再次运行就成功了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值