pytest不同目录下执行相同名的py文件报错

在项目中创建一个dep1目录,该目录下又有两个目录,如下所示:
在这里插入图片描述
在dep1目录下执行pytest -sv命令,报错如下:

D:\pythonProject\dependency\dep1>pytest -sv
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.7.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\python3.7.6\python.exe
cachedir: .pytest_cache
rootdir: D:\pythonProject\dependency\dep1
plugins: allure-pytest-2.9.41, dependency-0.5.1
collected 2 items / 1 error / 1 selected                                                                                                                             

============================================================================== ERRORS ===============================================================================
_______________________________________________________________ ERROR collecting dir2/test_dir_01.py ________________________________________________________________
import file mismatch:
imported module 'test_dir_01' has this __file__ attribute:
  D:\pythonProject\dependency\dep1\dir1\test_dir_01.py
which is not the same as the test file we want to collect:
  D:\pythonProject\dependency\dep1\dir2\test_dir_01.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
====================================================================== short test summary info ======================================================================
ERROR dir2/test_dir_01.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================================================= 1 error in 0.17s ==========================================================================

解决方法一:修改任一目录下的py文件名称
解决方法二:在dir1或dir2目录下添加添加__init__.py文件。例如:
在这里插入图片描述
执行结果:

D:\pythonProject\dependency\dep1>pytest -sv
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.7.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\python3.7.6\python.exe
cachedir: .pytest_cache
rootdir: D:\pythonProject\dependency\dep1
plugins: allure-pytest-2.9.41, dependency-0.5.1
collected 4 items                                                                                                                                                    

dir1/test_dir_01.py::Testna::test_01 function test_01
PASSED
dir1/test_dir_01.py::Testna::test_02 function test_02
PASSED
dir2/test_dir_01.py::Testna::test_01 function test_01
PASSED
dir2/test_dir_01.py::Testna::test_02 function test_02
PASSED

========================================================================= 4 passed in 0.06s =========================================================================

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永远不要矫情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值